Purity (D2 standard libraries / object.d)
Andrei Alexandrescu
SeeWebsiteForEmail at erdani.org
Fri Jan 9 21:10:11 PST 2009
Walter Bright wrote:
> Michel Fortin wrote:
>> Hum, could the compiler be trusted to add the memoization code to pure
>> functions so they can stay pure?
>
> If the compiler does general memoization on pure functions, all it has
> to do is use the bits of the arguments passed on the stack to the
> function as an index into an associative array of the return values.
Not the bits, the full objects if they have indirection. (Pure functions
should accept const parameters!)
And it's not only simple associative array. It could also be a dense
array, a little interpolation engine etc.
> The problem is identifying if this would be faster than recomputing the
> return value.
I used memoizers for exp and log (the most called functions in some code
I wrote) and it made the original version feel like it was driving in
reverse.
Andrei
More information about the Digitalmars-d
mailing list