Purity (D2 standard libraries / object.d)

dsimcha dsimcha at yahoo.com
Sat Jan 10 14:33:11 PST 2009


== Quote from Walter Bright (newshound1 at digitalmars.com)'s article
> dsimcha wrote:
> > == Quote from Walter Bright (newshound1 at digitalmars.com)'s article
> >> 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.
> >> The problem is identifying if this would be faster than recomputing the
> >> return value.
> >
> > Wouldn't this also cause threading issues?  The obvious solution would be to use
> > TLS, but his requires duplicating the cache across threads.  Also, using AAs
> > internally like this would lead to very deeply hidden memory allocations, and
> > therefore possibly more frequent GC, etc.
> Immutable data doesn't have threading issues, which is the beauty of them.

You misunderstood the question.  I mean, at the implementation level, even though
it wouldn't be visible to the programmer, the AA that did the memoization would
have to be mutable so it could be modified when a new value was to be memoized.
This is where the threading issues would come in.  What I was saying is that
synchronizing on this would be bad for obvious reasons, and TLS would still not be
great because you wouldn't be able to share memoized values across threads.



More information about the Digitalmars-d mailing list