Introduction to open instance delegates

Creating a delegate to a method is normally quite a cheap operation. However, there are some reflection-based situations where you have to create a delegate dynamically using Delegate.CreateDelegate. As you can expect, such a method is many times slower than using the type-safe delegate constructor. Using open instance delegates can help alleviate this performance penalty.

For this post, I’ll be using these classes in the examples:

Creating a delegate

The standard way of creating a delegate to an instance method is the following (I’ve expanded out the syntax for clarity):

This simply calls the compiler-created constructor on the AddDelegate type. In situations where you need to create the delegate dynamically, you need to use the Delegate.CreateDelegate methods instead:

In this example, in the arguments to CreateDelegate, we’re specifying the type of delegate to create, the instance of Adder the method is to be run on, and the MethodInfo of the method to run as the delegate.

The hidden ‘this’ pointer

The reason we need to specify the firstArgument parameter to CreateDelegate is that every instance method invocation requires a reference to the object we’re calling the method on (the ‘this’ pointer) to be the first argument it is called with, before all the declared method arguments (for Add, the value parameter). What happens if we don’t specify this argument when creating the delegate?

Not a lot it would seem; trying to invoke such a delegate throws a NullReferenceException if it’s a virtual method or accesses the ‘this’ instance (which most instance methods do). However, things get a lot more interesting if we change the delegate signature slightly:

Here, we’re explicitly specifying the hidden ‘this’ pointer of the Add method as part of the delegate signature. A delegate to an instance method created in this way is called an open instance delegate, and by specifying the ‘this’ argument explicitly we can use this delegate instance to call the same method on multiple instances:

So?

Such a delegate may not seem immediately useful, but this can be crucial when performance is critical. Creating a delegate with CreateDelegate is an expensive operation, and if you have to call the same delegated method on many thousands of objects it can be a killer to create a new delegate instance for each object. To demonstrate this, I ran 3 tests that called a delegate of the Add method on 1,000,000 Adder objects, creating the using the normal delegate constructor, a closed CreateDelegate call for each object, and a single open instance delegate:

Such results speak for themselves. Although rather a niche feature, when calling the same method on many thousands of separate instances, open instance delegates can improve the performance of such code many times over.

Note: This also works as expected for delegates to virtual and interface methods. You can also use the generic Func and Action delegates if you wish, rather than defining your own delegate type, as long as the type of the first argument of the delegate is compatible with the type containing the instance method you’re calling.