Get VTable pointer as a constant

Johan Engelen via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Fri Apr 8 02:08:37 PDT 2016


On Thursday, 7 April 2016 at 20:49:40 UTC, Adam D. Ruppe wrote:
> On Thursday, 7 April 2016 at 20:43:04 UTC, Johan Engelen wrote:
>> Does anybody know how to get the class's vtable pointer 
>> without doing a memory read?
>
> I don't think you can... why do you want it though?

I have implemented PGO (profile-guided optimization) of virtual 
calls in LDC. Now I am trying to find a way to express it in D, 
such that you can do the optimization without profiling (using 
your knowledge of your program):

         A a = getobject_mostlyA(...); // 80% of the time it will 
be an A object
         int num;
         if (a.__vptr == cast(void*)typeid(A).vtbl.ptr)
         {
             num = a.A.callVirtual(...);
         }
         else
         {
             num = a.callVirtual(...);
         }

This could be wrapped in a template "CallLikelyObject!A(...)" or 
something similar.
However, cast(void*)typeid(A).vtbl.ptr is suboptimal because 
TypeInfo is not constant and so it will always do a memory read. 
Unfortunately, recently LDC had to make TypeInfo mutable because 
of having to support "synchronize(typeid(A))", such that the load 
from memory cannot be optimized to the linker-resolved vtable 
address constant.

Is there a way to read a symbol if I myself specify the mangled 
name?
Pseudo-code:
    pragma(mangle, "LDC vtable mangled named for A")
    immutable void* vptrA;



More information about the Digitalmars-d-learn mailing list