File this under the category of “handy coding hints”…
With the disparity between CPU performance, the benefits of CPU-cache memory and system RAM, it’s not always best to retain large caches of relatively trivial generatable data – a histogram of the last 3-4 entries might be all you need especially if you can very cheaply keep them ordered so that if 1 in 3 calls produces the same result, that result will be handy.
On the other hand, a cache might be useful if you have a comparatively small result set but a lot of users of the same results. You could then give out pointers to the result sets so that when the data changes you only have to update the cache and everyone’s effective data changes immediately. That could be quite the saving.
Just keep in mind that you gave out pointers a few minutes later when you write code that periodically expires said data. And more important still, if a few minutes later you write some code that periodically clears out the cache and rebuilds it, don’t act all surprised when the data you were pointing to seems to keep going away…
Boy do I feel like an idiot :) At least my brain caught up with me before I ever tried to actually run the thing :)
Recent Comments