4 ms·
> They always return the same results so why not memoize? You only want to memoize when the lookup table is "cheap" compared to calculating the function. A loo
by ben509 7y ago
> They always return the same results so why not memoize?
You only want to memoize when the lookup table is "cheap" compared to calculating the function. A lookup table is naturally expensive in terms of space, but can also be expensive in terms of computation; hashing is not free.
Any kind of function that is mostly a constructor, accessor or mutator is a bad candidate for memoization. Most of your primitives should probably not be memoized.
And you generally don't want to memoize memoized functions. Either memoize the constituents, if they're called from many places, or the top level function.
And most memoization evicts "old" entries, so it's possible to be waste all those resources putting things into and out of the table without ever getting hits.
But that problem can be far more insidious when it looks like the memoization works. You can get be getting great performance when there are few enough threads that everything fits in the table. Then you get too much load and misses start to increase, get some more load and memoization starts working against you and you get catastrophic failure.