Purity (D2 standard libraries / object.d)

bearophile bearophileHUGS at lycos.com
Sat Jan 10 07:06:25 PST 2009


Michel Fortin:

> So to determine if a function is worth memoizing, you have to say yes 
> to those two things:
> 1.	is it faster than recomputing the return value?
> 2.	is this function called often with the same inputs?
> While the compiler could take an educated guess at answering 1 given 
> the code of a function, question 2 can only be answered by knowing the 
> usage pattern of various functions.

GCC has profile-driven optimization. I presume it may help in such problems too.


> Perhaps the cache needs to be a little smarter than a regular AA. You
> may not want to keep each and every value that was computed. Depending
> on the situation, keeping only the 100 last results may be enough, in
> which case you can dedicate a fixed amount of memory for caching.

Right. There are time-limited memoization strategies, and other ones as well. In Python I have seen plenty of them:
http://code.activestate.com/recipes/325905/
http://code.activestate.com/recipes/498110/

Bye,
bearophile



More information about the Digitalmars-d mailing list