About the MSDN sample - search for the following lines:
HelloDelegate hi =
// Use the delegate to execute the dynamic method.
Console.WriteLine("\r\nUse the delegate to execute the dynamic method:");
int retval = hi("\r\nHello, World!", 42);
And right after that, there's use of invoke and a comment:
// Invoke the dynamic method using the arguments. This is much
// slower than using the delegate, because you must create an
// array to contain the arguments, and value-type arguments
// must be boxed.
object objRet = hello.Invoke(null, BindingFlags.ExactBinding, null, invokeArgs, new CultureInfo("en-us"));
So it should be faster to invoke directly, even if marginally. Also, I think even with Invoke it should be faster than using Activator.
About not seeing perf improvements. Note that in your version you lookup the delegate in the dictionary - this is substantial effort. Additionally the dictionary lookup is not efficient. First, use of "if dict.Contains(key) then v=dict[key]" pattern requires
2 dictionary lookups - better use TryGetValue which requires a single lookup. Secondly, using AstNode as a key in a dictionary - this requires looking at AstNode.GetHashCode() implementation. If you use the one inherited from Object, then it is really slow
and not very good hash. So the perf results show that the cost of using Activator is about the same as two dictionary lookups
So let's get rid of the dictionary. Please declare a delegate, add a field (of delegate type) to BnfTerm, assign it at parser startup for all terms, and then use it for creating nodes. Let's see if it brings any visible perf improvement.
Overall, I wouldn't worry even if we don't see perf differences. First, the perf of this piece is obscured by the load of all other parser activities. But mainly I would prefer to do this change anyway, because this old code using Activator is an "itching
point", some obviously "can be done better" spot for any person reading the code, and making bad impression of the project as a whole. So let's go for it. Let me know the results please