Memory allocation purity

via Digitalmars-d digitalmars-d at puremagic.com
Wed May 14 22:51:14 PDT 2014


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.  
Malloc and new are random generators in that sense. So to make 
them pure you will have to put a ban on taking address, comparing 
address etc on the objects...

However mmap to a fixed address is pure if it throws an exception 
on failure because the exception bypass the call site of the pure 
function (pure functions should not catch side effect exceptions).

Returning null does not make a function impure. Pure functions 
may return bottom (like division by zero).

Pure in D seems pointless to me.


More information about the Digitalmars-d mailing list