Purity (D2 standard libraries / object.d)

Steven Schveighoffer schveiguy at yahoo.com
Mon Jan 12 09:11:07 PST 2009


"Walter Bright" wrote
> Andrei Alexandrescu wrote:
>> 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!)
>
> You're right, the "just the bits on the stack" is if the arguments are 
> immutable. Const arguments would have to include whatever was indirectly 
> referenced, which I argue is impractical for the runtime to handle 
> automatically.

Bits on the stack also do not work for immutable reference arguments. 
Imagine two immutable strings that are exactly the same data, but have 
different pointers.  You wouldn't want to memoize only on the pointer values 
(or you'd lose

I think bits on the stack only work for non-reference arguments.  It sounds 
like memoization is a non-automatic feature in any case.  Which is a shame 
because I thought it was a common pure-function optimization by the way it 
was discussed in the past.

-Steve 





More information about the Digitalmars-d mailing list