John Lam 1997 — 2026

Athlon interface method dispatch cost

Yesterday, I examined the cost of virtual method dispatch (VMD) vs. interface method dispatch (IMD). I did my investigations on my Thinkpad T-23, which uses a Pentium III-M processor. Today, I am curious about the effects of CPU architecture on the relative cost of VMD vs. IMD. While I was investigating these effects, I encountered a number of Heisenberg effects that caused me to spend some performing control experiments so that I could actually determine what it was that I was looking at.

I have a large number of different CPU’s running in various computers around my house. I have a Pentium 166 MMX, a Pentium Pro 150, a Pentium 233 MMX, a dual Celeron 400, a Celeron 600, a Pentium III-M 1.13GHz, an Athlon 1200, and a Pentium 4 1.6GHz. Eventually, I might decide to examine the progress that Intel has made with respect to hiding indirection costs, but for the time being, I decided to look at the differences between my 1.13GHz Pentium III-M and my Athlon 1200.

Since I am now comparing results between different computers, I needed to update my benchmark software so that it would report results in seconds, instead of ticks of the high-resolution CPU counter. Here is what the test rig looks like in its current form:

`const int MAX_COUNT = 100000000; static long _performanceFrequency;

static double TimeVirtualMethodDispatch( C c ) { long vmdStart, vmdEnd; QueryPerformanceCounter( out vmdStart ); for( int i = 0; i < MAX_COUNT; i++ ) c.M1(); QueryPerformanceCounter( out vmdEnd ); return ((double)(vmdEnd - vmdStart)) / _performanceFrequency; }

static double TimeInterfaceMethodDispatch( I1 i1 ) { long imdStart, imdEnd; QueryPerformanceCounter( out imdStart ); for( int i = 0; i < MAX_COUNT; i++ ) i1.M1(); QueryPerformanceCounter( out imdEnd ); return ((double)(imdEnd - imdStart)) / _performanceFrequency; }

static void Main(string[] args) { C c = new C(); I1 i1 = (I1)c;

QueryPerformanceFrequency( out _performanceFrequency );

// Force JIT-compilation TimeVirtualMethodDispatch( c ); TimeInterfaceMethodDispatch( i1 );

// Calls to output results omitted for clarity }`

To jog your memory, these are the x86 instruction sequences for:

Virtual Method Dispatch:

0440 mov ecx,edi ;esi contains objref 0442 mov eax,[ecx] ;get method table 0444 call dword ptr [eax+0x38] ;invoke via slot in MT

Interface Method Dispatch:

04af mov ecx,edi ;edi contains objref 04b1 mov eax,[ecx] ;get method table 04b3 mov eax,[eax+0xc] ;get global IT 04b6 mov eax,[eax+0x2c] ;get local IT 04b9 call dword ptr [eax] ;invoke via slot in IT

Recall that we are attempting to measure the cost of the two additional levels of indirection introduced by the interface method dispatch mechanism used by the CLR. Here are the raw benchmark results:

| Athlon 1200 Results — Release build | | | |

| Run | VMD | IMD | %Diff |

| 1 | 0.610 | 0.870 | 29.9% |

| 2 | 0.613 | 0.869 | 29.5% |

| 3 | 0.608 | 0.877 | 30.7% |

| 4 | 0.611 | 0.868 | 29.6% |

| 5 | 0.607 | 0.869 | 30.1% |

| Avg | 0.610 | 0.871 | 30.0% |

| +/- | 0.002 | 0.004 | 0.5% |

| Pentium III-M Results — Release build | | | |

| Run | VMD | IMD | %Diff |

| 1 | 0.629 | 0.715 | 12.0% |

| 2 | 0.623 | 0.715 | 12.9% |

| 3 | 0.631 | 0.723 | 12.7% |

| 4 | 0.629 | 0.719 | 12.5% |

| 5 | 0.631 | 0.719 | 12.2% |

| Avg | 0.629 | 0.718 | 12.5% |

| +/- | 0.003 | 0.003 | 0.3% |

The Pentium III-M executes the IMD instruction sequence only 12.5% slower than the VMD instruction sequence. The Athlon 1200, on the other hand, executes the IMD instruction sequence 30% slower than the VMD instruction sequence. The key observation here is that the Pentium III-M totally kicks the Athlon 1200’s ass in hiding the cost of the indirection used by IMD!

The take-home message here is that your mileage will vary significantly if your code is executed on different platforms. However, keep in mind the following:

  1. This is a totally contrived benchmark; we are measuring the ping cost of invoking methods via VMD and IMD.
  2. These are relative numbers, we are still invoking a method 100,000,000 times in well under a second. Expressed differently, the Pentium III-M is dispatching 159 million VMD calls per second and the Athlon 1200 is dispatching 164 million VMD calls per second!

It would be interesting to see whether the x86 instruction sequence for IMD could be optimized specifically for Athlon processors to try and narrow the performance gap.