Memory allocation purity
via Digitalmars-d
digitalmars-d at puremagic.com
Thu May 15 04:31:32 PDT 2014
On Thursday, 15 May 2014 at 05:51:16 UTC, Ola Fosheim Grøstad
wrote:
> On Thursday, 15 May 2014 at 02:49:28 UTC, Adam Sakareassen via
> Digitalmars-d wrote:
>> The more D allows 'pure' functions to diverge from functional
>> purity the less relevant the flag is for compiler
>> optimisations.
> ...
>> By the same reasoning cacheability is important. A pure
>> function might be called within a loop with a parameter that
>> is not altered during the loop. If the compiler knows the
>> function is pure, it can perform the calculation before the
>> loop and simply reuse the cached result for each iteration.
>
> Yep, purity implies memoing. Malloc and new are not pure
> because they return objects that can be differentiated by
> address.
There's an important difference between malloc and new: malloc
returns a pointer, but new returns a typed object. This is
crucial IMO, because the returned objects are equal to each
other. They aren't identical, but then different int variables
with the same value aren't identical either, and a function
returning int is still considered pure. So it's not identity (~
address) that matters.
>
> Pure in D seems pointless to me.
Not at all: Don't think of it in terms of low-level optimization
opportunities, but in terms of semantics. For example, you get
the concept of uniqueness. And the optimizations can still be
done, because strongly pure functions can be recognized by their
signatures.
More information about the Digitalmars-d
mailing list