I’m implementing a cache on a function that gets called hundreds of millions of times. Cache size is tens of millions of items.
It is currently implemented using a Dictionary, and lookups in it take a significant amount of time.
Is it possible to get a reference to the whole pair in the Dictionary, not just the value, so I can check if a value exists, check it (and, possibly, update it) if it does using a single lookup?
Currently, I have something like this:
int val;
if (cache.TryGetValue(key, out val))
if (val < newVal) cache[key] = newVal;
else return val;
else
cache.Add(key, newVal);
I would like to get this:
Pair pair = cache.GetPair(key);
if (pair != null)
if (pair.Value < newVal) pair.Value = newVal;
else return pair.Value;
else
cache.Add(key, newVal);
If there is an alternative data structure that allows this, I’d be glad to hear about it too.
Thanks in advance!
This is inspired by Mare Infinitus’ answer. Assuming your
cachevariable is now aDictionary<string, int>you might change it into aDictionary<string, MutableInt32>whereMutableInt32is written like this:Then you could change your code to