This question is partly about delegates, and partly about generics.
Given the simplified code:
internal sealed class TypeDispatchProcessor
{
private
So I did some measurements on this.
var delegates = new List();
var actions = new List>();
const int dataCount = 100;
const int loopCount = 10000;
for (int i = 0; i < dataCount; i++)
{
Action a = d => { };
delegates.Add(a);
actions.Add(o => a((int)o));
}
var sw = Stopwatch.StartNew();
for (int i = 0; i < loopCount; i++)
{
foreach (var action in actions)
action(i);
}
Console.Out.WriteLine("{0:#,##0} Action
I created a number of items to indirectly invoke to avoid any kind of optimisation the JIT might perform.
The results are quite compelling!
1,000,000 Action calls in 47.172 ms 1,000,000 Delegate.DynamicInvoke calls in 12,035.943 ms 1,000,000 Action calls in 44.686 ms 1,000,000 Delegate.DynamicInvoke calls in 12,318.846 ms
So, in this case, substituting the call to DynamicInvoke
for an extra indirect call and a cast was approximately 270 times faster. All in a day's work.