Cook Computing

Exploring Late-Bound Invocation

April 13, 2004 Written by Charles Cook

I recently mentioned Joel Pobar's posting on Late-Bound Invocation and over the weekend I did some investigation into what the performance is like for the various methods he describes. The spur for this was reading about the JVM-based scripting language Groovy, in particular the fact that it currently does late-bound dynamic invocation of methods, as described here by James Strachan, the creator of Groovy, and then wondering what sort of performance you could get for dynamic invocation of .NET methods. Dynamic invocation in this context means making a call on an object given that you know the method name and have the call parameters as an array of type Object[].

I used the sample dynamic method from Joel's blog and wrote code to make the same call using the other techniques he describes. This was on machine running the PDC release of Longhorn/Whidbey so these results should not be taken too seriously but they perhaps give a flavour of what can be achieved.

CallVirt - ~100,000,000 calls/sec

Delegate - ~40,000,000 call/sec

Dynamic Method - ~10,000,000 calls/sec

MethodInfo.Invoke - ~200,000 calls/sec

Type.InvokeMember - ~100,000 calls/sec

The last three cases are relevant to dynamic invocation. Using a dynamic method has the drawback that building the method is very slow - the Dynamic Method and MethodInfo.Invoke results don't include any once-off runtime overhead before making the first call - but this might not be important depending on the context in which it is being used.