www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Get VTable pointer as a constant

reply Johan Engelen <j j.nl> writes:
I am trying to get the vtable pointer as a constant (!).
I've found
   auto vptr = typeid(A).vtbl.ptr
gets me the pointer, but because TypeInfo is not immutable 
(another forum thread), this will read the pointer from memory 
instead of loading a direct value.

Does anybody know how to get the class's vtable pointer without 
doing a memory read?

Thanks!
   Johan
Apr 07 2016
parent reply Adam D. Ruppe <destructionator gmail.com> writes:
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?
Apr 07 2016
parent Johan Engelen <j j.nl> writes:
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;
Apr 08 2016