Out of curiosity: how does the CLR dispatch virtual method calls to interface members to the correct implementation?
I know about the VTable that the CLR maintains for each type with method slots for each method, and the fact that for each interface it has an additional list of method slots that point to the associated interface method implementations. But I don't understand the following: how does the CLR efficiently determine which interface method slot list to pick from the type's VTable?
The article Drill Into .NET Framework Internals to See How the CLR Creates Runtime Objects from the May 2005 issue of the MSDN Magazine talks about a process-level mapping table IVMap indexed by interface ID. Does this mean that all types in the same process have the same pointer to the same IVMap?
It also states that:
If
MyInterface1
is implemented by two classes, there will be two entries in the IVMap table. The entry will point back to the beginning of the sub-table embedded within theMyClass
method table.
How does the CLR know which entry to pick? Does it do a linear search to find the entry that matches the current type? Or a binary search? Or some kind of direct indexing and have a map with possibly many empty entries in it?
I've also read the chapter on Interfaces in CLR via C# 3rd edition but it does not talk about this. Therefore, the answers to this other question do not answer my question.
From the first article that you linked:
and
This suggests to me that the interface's IVMap has entries keyed by the class name (or some equivalent) pointing to a subsection of the class's vtable, which essentially has duplicate implementations of each of the class's methods that implement that interface, backed by pointers to the same physical implementation as the class's own vtable entries.
Could be completely wrong though.
That article is more than 10 years old, and a lot has changed since then.
IVMaps have now been superseded by Virtual Stub Dispatch.
Go read that article, it has more detail you'll ever need to know. It comes from the Book of the Runtime, which was documentation originally written by the CLR devs for CLR devs but has now been published for everyone. It basically describes the guts of the runtime.
There's no point for me to duplicate the article here, but I'll just state the main points and what they imply:
And here's an important consideration, straight from the article:
The runtime is really optimistic about monomorphic call sites, which makes a lot of sense in real code, and it will try hard to avoid resolve stubs as much as possible.
If you take a look at diagram that was on the linked site, it may make it easier to understand.
Yes, since it is at the domain level, it means everything in that AppDomain has the same IVMap.
The classes are laid out with offsets, so everything has a relatively set area on where it would be. That makes things easier when looking for methods. It would search the IVMap table and find that method from the interface. From there, it goes to the MethodSlotTable and uses that class' implementation of the interface. The inteface map for the class holds the metadata, however, the implementation is treated just like any other method.
Again from the site you linked:
This means that each time an interface is implemented it has a unique record in the IVMap which points to the MethodSlotTable which in turn points to the implementation. So it knows which implementation to pick based on the class that is calling it as that IVMap record points to the MethodSlotTable in the class calling the method. So I imagine it is just a linear search through the IVMap to find the correct instance and then they are off and running.
EDIT: To provide more info on the IVMap.
Again, from the link in the OP:
So here we have a table where the number is the offset of bytes. This is just one record in the IVMap:
Suppose there are 100 interface records in this AppDomain and we need to find the implementation for each one. We just compare the 5th byte to see if it matches our current class and if it does, we jump to the code in the 6th byte. Since, each record is 8 bytes long, we would need to do something like this: (Psuedocode)
While it is still a linear search, in reality, it isn't going to take that long as the size of data being iterated isn't huge. I hope that helps.
EDIT2:
So after looking at the diagram and wondering why there is no Slot 1 in the IVMap for the class in the diagram I re-read the section and found this:
So the IVMap for a class is only loaded with the interfaces that the specific class inherits. It looks like it copies from the Domain IVMap but only keeps the interfaces that are pointed to. This brings up another question, how? Chances are it is the equivalent of how C++ does vtables where each entry has an offset and the Interface Map provides a list of the offsets to include in the IVMap.
If we look at the IVMap that could be for this entire domain:
Assume there are only 4 implementations of Interface Map in this domain. Each slot would have an offset (similar to the IVMap record I posted earlier) and the IVMap for this class would use those offsets to access the record in the IVMap.
Assume each slot is 8 bytes with slot 1 starting at 0 so if we wanted to get slot 2 and 3 we would do something like this:
Please excuse my x86 as I'm not that familiar with it but I tried to copy what they have in the article that was linked to.