Templatize dynamic cast. better user customized RTTI

DavidL davidl at 126.com
Sun Nov 11 21:39:58 PST 2007


Jarrett Billingsley Wrote:

> "davidl" <davidl at 126.com> wrote in message news:op.t1nqmwn5eb62bo at lzg...
> 
> > It would make the code size smaller with a very neglectable runtime cost.
> 
> At the call site, yes, it will become slightly smaller.  But you've 
> forgotten that you then need to have a separate copy of the dynamic cast 
> function for every class that you do a dynamic cast to.  With the current 
> way of doing things, there's only one function. 
> 
> 

If you grep your source thoroughly, you will find you only cast to very limited set of classes. while each cast(Certain_type) might have been called dozens of times.

That depends on a balance point which can be controlled by compiler, how much function would be benefit from making it a template.

e.g. cast(Type1) has been referenced over 50 times,  in this case, it would be better to make it template. (better runtime efficiency, and better in size)
cast(Type2) has been only referenced 10 times, then making it as it's now by using a generic dynamic cast.

The times of templatizing a function depends on how much code does a dynamic cast function take, if the binary size of a dynamic cast function takes 50 bytes, and each non-template caller takes 5bytes more than template caller,  then if the cast is referenced over 50/5 = 10 times , then this specific type cast should be templatized.

And if users prefers speed over the size of the binary size, then all this kind of stuffs should be templates.





More information about the Digitalmars-d mailing list