Array toHash()
    Ali Çehreli via Digitalmars-d-learn 
    digitalmars-d-learn at puremagic.com
       
    Sat Nov 29 15:59:15 PST 2014
    
    
  
On 11/29/2014 12:45 PM, David Held wrote:
 > On 11/26/2014 4:40 PM, Ali Çehreli wrote:
 >> [...]
 >>      override
 >>      size_t toHash() @trusted pure const nothrow
 >>      {
 >>          auto func = assumePure(&(typeid(importantStuff).getHash));
 >>          return func(&importantStuff);
 >>      }
 >
 > Am I right in assuming that there is some runtime
 > cost here, as we are calling through a function pointer?
I think so. However, I think the compiler can eliminate dereferencing 
the function pointer if it can prove that it is not necessary.
 > Is the lack of inlining the only cost, or is getting at the
 > typeinfo also costly?
typeid() is a runtime function. I think it will be costly every time 
toHash is called. The function pointer can be initialized once.
     // I've "deduced" the type from an error message. ;)
     static const ulong delegate(const(void*)) const pure nothrow 
@trusted func;
     // This will be executed once per thread:
     static this() {
         func = assumePure(&(typeid(Foo.init.importantStuff).getHash));
     }
     override
     size_t toHash() @trusted pure const nothrow
     {
         return func(&importantStuff);
     }
Now there is no TypeInfo lookup after thread initialization time.
Ali
    
    
More information about the Digitalmars-d-learn
mailing list