Memory allocation purity

Jonathan M Davis via Digitalmars-d digitalmars-d at puremagic.com
Mon May 19 20:21:57 PDT 2014


On Mon, 19 May 2014 14:33:55 -0400
Steven Schveighoffer via Digitalmars-d <digitalmars-d at puremagic.com>
wrote:

> The whole POINT of pure functions is that it will return the same
> thing. The fact that it lives in a different piece of memory or not
> is not important. We have to accept that. Any code that DEPENDS on
> that being in a different address is broken.

Except that if you're talking about reference types, and the reference points
to two different objects, then they're _equal_ rather than being the same
object. That's the whole point of the is operator - to check whether two
objects are in fact the same object. I agree that relying on things like
whether one address in memory is larger or smaller than another address in
unrelated memory is just plane broken, but it's perfectly legitimate for code
to depend on whether a particular object is the same object as another rather
than equal. And memoizing the result of a function - be it pure or otherwise -
will therefore have an effect on the semantics of perfectly valid programs.

And honestly, even if equality were all that mattered for memoization, the
fact that the compiler only does it on a very, very restrictive basis (since
it never saves the result, only optimizes out extraneous calls) makes it so
that memoization is kind of useless from the standpoint of the compiler. It's
only really useful when the programmer does it, and they can decide whether
it's okay to memoize a function based on the requirements of their program
(e.g. whether reference equality matters or just object equality).

- Jonathan M Davis


More information about the Digitalmars-d mailing list