Pure dynamic casts?

Daniel Keep daniel.keep.lists at gmail.com
Tue Sep 22 18:24:10 PDT 2009



Steven Schveighoffer wrote:
> On Tue, 22 Sep 2009 05:24:11 -0400, Daniel Keep
> <daniel.keep.lists at gmail.com> wrote:
> 
>>
>>
>> Jason House wrote:
>>> Dynamic casts are pure. They don't use global state, and have the
>>> same output for the same reference as input. Interestingly, dynamic
>>> cast results are independent of intervening mutable calls... So
>>> there's even greater opportunity for optimization.
>>
>> What if the GC just happens to re-use that address for a different
>> object?
> 
> That's only if memoization is used.

Well, if you're not looking at memoization, why is this discussion even
happening?

> I think what Jason said is correct -- you can view dynamic cast as
> taking 2 arguments, one is the reference which is simply echoed as the
> return value, and one is the classinfo, which is an immutable argument
> that causes a decision to be made.

The reference *isn't* echoed as a return value.  If it was, then casting
wouldn't do anything.

> In fact, you could use memoization on a dynamic cast subfunction that
> memoizes on the target type and the classinfo of the source, regardless
> of the reference value, and returns an offset to add to the return value.

Yes, that would work, provided both references are immutable.

> It's pretty easy for the compiler to prove that o has not be reassigned,
> so even without memoization, the compiler can logically assume that the
> result from the first dynamic cast can be reused.  I think this is the
> major optimization for pure functions anyways, not memoization.

Pretty easy?  So you're ignoring threads, then?

The ONLY way I can see object casting being treated as a pure function
is if all references passed in are immutable and even then only if the
compiler can prove the reference cannot be changed (i.e. the reference
is always kept on the stack AND you assume you never manually delete it).

> -Steve

  -- Daniel



More information about the Digitalmars-d mailing list