John Lam 1997 — 2026

Interface dispatch vs. object reference dispatch

Interface dispatch vs. object reference dispatch

2002-01-26

This is the system that I was interested in investigating:

interface I1 { void M1(); } class C : I1 { public virtual void M1() {} }

There are two ways of calling the M1() virtual method in an instance of type C:

`C objRef = new C(); I1 itfRef = (I1)objRef;

objRef.M1(); itfRef.M1();`

The optimized x86 instructions[1] generated by the JIT for object reference dispatch are:

mov ecx, esi mov eax, [ecx] ;get method table call dword ptr [eax + zzz] ;call via method table

The optimized x86 instructions generated by the JIT for interface reference dispatch are:

mov ecx, esi mov eax, [ecx] mov eax, [eax + zzz] ;get global itf table address mov eax, [eax + zzz] ;get local itf table address call dword ptr [eax + zzz] ;call via local itf table

If you look at the above code sequence, you would conclude that there should be a performance penalty for dispatching a virtual method call using an interface reference due to the extra two instructions required by interface reference dispatch. So did I.

Earlier, I reported the results of investigations using a simple managed benchmark. Those results indicated a difference of 13.5% between virtual method dispatch and interface method dispatch. I mistakenly concluded that the 13.5% difference was due to those two additional instructions. Silly me.

Later, I noticed some unusual observations when measuring the effects of CPU architectures on my benchmark results. Now, in the words of former my Ph.D supervisor, Professor Ronald Kluger: “When you have an unusual observation one of two things has happened: you have made a discovery or you have made a mistake.” Now since I only made one truly unique discovery during the nearly 5 years that I spent studying Physical Organic Chemistry at the University of Toronto, I’m painfully aware of how easily one can be mislead when measuring rates of anything. So I decided to break things down further and run the appropriate control experiments to rule out other explanations for my results.

Now, since it is really difficult to measure how quickly a 1+GHz CPU executes these instruction sequences, I had to place them in a loop. I experimented with different loop sizes, and found that 100,000,000 iterations of a loop on my 1.13GHz Pentium III-M system provided optimal results.[2]

The first thing that I ruled out was the Heisenberg effects caused by running under control of a debugger. If you run your benchmark code under control of a debugger the JIT will not generate optimized x86 code. That one was easy to fix: don’t run your benchmark code under control of the debugger, and always use release builds

Next, as I discovered later, P5 and P6 processors are very sensitive to the alignment of instructions in memory. In order for me to measure the effects of how instructions are aligned in memory, I needed to create a model system where I could manually control the alignment of the instruction sequences under investigation using nop instructions. To do this, I created a model system using inline assembler in Visual C++.

The first control experiment I did was to measure the overhead incurred by the loop instructions. I summarized those results in another web log entry. This experiment not only looked at the cost of looping, it also looked at the effects of the size of the loop. The results showed that if your loop is aligned such that the loop entry point is aligned on a 16-byte boundary and your loop is less than or equal to 16 bytes in length, you will pay no additional penalties. If your loop exceeds 16 bytes in length you will pay a 1 cycle penalty. This turns out to be very significant.

Next, I expanded my model system so that I could accurately simulate the cost of dispatching a virtual method. I constructed some fake method tables and interface dispatch tables using C++:

`// Fake data structures to simulate IMD unsigned long localItfTable[] = { (unsigned long)&NOP }; unsigned long globalItfTable[] = { 0, (unsigned long)localItfTable };

// Fake data structures to simulate VMD unsigned long methodTable[] = { 0, ((unsigned long)&NOP), (unsigned long)&globalItfTable }; unsigned long obj = (unsigned long)methodTable; unsigned long objptr = (unsigned long)&obj;`

Here’s the target NOP method that would be called in my model system:

__declspec( naked ) void NOP() { __asm { ret } }

This is the loop that calls the NOP() method using the object reference instruction sequence:

loopx: ;entire loop fits in 16 bytes mov ecx, esi mov eax, [ecx] call dword ptr [eax + 4] inc edi cmp edi, 100000000 jl loopx

This is the loop that calls the NOP() method using the interface reference instruction sequence:

loopx: ;entire loop is > 16 bytes mov ecx, esi mov eax, [ecx] mov eax, [eax + 8] ;get global itf table address mov eax, [eax + 4] ;get local itf table address call dword ptr [eax] ;call via local itf table inc edi cmp edi, 100000000 jl loopx

Both of these loops are setup by the following x86 instructions:

mov esi, objptr xor edi, edi

Both of these loops are aligned using NOP instructions so that the loopx label sits at the beginning of a 16-byte boundary.

The key observation here is that the second loop is > 16 bytes in length. Based on my earlier experiments, this causes the second loop to pay a 1 cycle penalty!

Now, if you run the model system, you will find that the it takes 0.644 +/- 0.003 seconds to complete 100,000,000 iterations of the call using an object reference, and 0.737 +/- 0.005 seconds to complete 100,000,000 iterations of the call using an interface reference.

The interesting result is the difference in time between these two loops: 0.092 seconds. If you multiply this amount of time by the processor frequency (1.13GHz) and divide by the number of iterations of the loop (100,000,000), you will find that the difference between these two loops is exactly one clock cycle. Hmmm … doesn’t this look suspiciously like the penalty that one pays for having a loop that is > 16 bytes in length? I think so too.

What’s a good experiment to test this? The object reference loop is exactly 16 bytes. If you insert a nop instruction into the loop, you will find that the time taken to complete both loops is the same!

So, what can we conclude based on these experiments? There is no measurable difference in performancebetween calling a virtual method using an object reference vs. calling the same method using an interface reference on a Pentium III-M CPU under the conditions used to run this test. However, if any of the mov instructions were to result in a cache miss, you would most certainly find a performance difference between these two instruction sequences.

It would be interesting to measure these effects under more real-world scenarios. I would love to hear your suggestions about how to go about running such an experiment.

[1] If you are interested in seeing the generation of these instructions for yourself, you will need to read this other entry that I made regarding Heisenberg effects in JIT systems. I highly encourage you to look for yourself. Don’t believe anything that I (or anyone else for that matter) says if it seems unreasonable. Even if it seems reasonable, check for yourself. Always be skeptical.

[2] On a multi-tasking operating system, you have to be sensitive to the fact that the OS will sometimes need to do something other than run your code. I found that keeping the total amount of time for each iteration of the loops to just under 1 second provided optimal results. If the OS had to do something during one of the runs, you will clearly see it in your results- one of the results will take significantly longer than the others. However, if you use longer runs of 3-10 seconds in duration, it is almost certain that the OS will do something else time-consuming during all of your runs, leading to much less reproducible results.