logoalt Hacker News

akoboldfryingyesterday at 11:13 PM2 repliesview on HN

> using the vtable as a hash table instead of a list.

Could you explain this a bit more? The word "list" makes me think you might be thinking that virtual method lookup iterates over each element of the vtable, doing comparisons until it finds a match -- but I'm certain that this is not how virtual method invocation works in C++. The vtable is constructed at compile time and is already the simplest possible "perfect hashtable": a short, dense array with each virtual method mapping to a function pointer at a statically known index.


Replies

hinkleytoday at 12:09 AM

The problem they were trying to solve was multiple inheritance, and by nominal type not by code reuse. So interfaces, basically.

So these guys essentially assigned a hashcode to every function of every interface and then you would do dispatch instead of obj.vtable[12] you would do modular math x = singature.hash % len(obj.vtable) and call that.

I believe this was sometime around 2005-2008 and they found that it was fast enough on hardware of that era to be usable.

show 1 reply
corysamatoday at 2:37 AM

"list" here does not refer to a "linked list". In more academic circles, a "list" referes to any linear container. Such as a Python List. In practice, C++ vtables are effectively structs containing function pointers.