Pure dynamic casts?

Steven Schveighoffer schveiguy at yahoo.com
Tue Sep 22 10:19:28 PDT 2009


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.

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.

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.

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.

-Steve



More information about the Digitalmars-d mailing list