More DynamicMethod Fun: Easy DBCommand Parameter Creation

I saw this post a couple of days ago, and I really liked the idea. I suggested to Matt that he changes his implementation to emit a DynamicMethod, thinking that it would give a performance increase. Here's a link to my implementation - it's rather lengthy so if you want to see how it works feel free to dive into the code.

Tests show that this is faster than the original approach:

  • At 10,000 iterations, Reflection = 0.3002694 seconds, DynamicMethod = 0.0541085 seconds
  • At 50,000 iterations, Reflection = 1.4428920 seconds, DynamicMethod = 0.2020902 seconds
  • At 250,000 iterations, Reflection = 7.0968963 seconds, DynamicMethod = 0.9447661 seconds

More and more I'm finding using a DynamicMethod to hard-code the code paths one finds using Reflection usually leads to a win in terms of performance. Not so much in terms of code maintainability (trying to read ILGenerator-based code in a .NET language is nasty, and debugging a DynamicMethod is not easy) but once it's rock-solid it's clear sailing from that point on.

* Posted at 07.09.2008 04:26:47 PM (Last Update: 07.10.2008 12:12:27 PM) | 4 comments | Link | RSS *

Comments

# Nice!, from Matt Hamilton at 07.09.2008 06:55:20 PM

I'm still not 100% convinced that this particular case warrants the perf boost, but this is extremely cool nonetheless. Great work, Jason!

# perfect example, from Justin Chase at 07.10.2008 11:13:40 AM

This is the perfect example why dynamic code is not bad. It's sort of scary to think of how it can be misused but there are certain situations where it is necessary for any sort of performant solution.

I think this is where the DLR is going to come in real handy, I can't wait to see dynamic features in C#! What would be really awesome is if they build us some sort of dynamic method builder using raw code like maybe on the IL generator:

ilGenerator.Parse("{0} = {1}", propertyName, value);

Of course it would then have to have some notion of the language to parse... maybe a CShapILGenerator?

# idea, from Justin Chase at 07.10.2008 11:29:24 AM

So here's a question. Your previous commenter mentioned that it might not be worth it for this particular case, so my question is if it was "easier" to generate IL (as in through some sort of actual code parsing rather than raw IL emittance, how much more likely would it be to be worthwhile for all circumstances?

I mean is the only reason doing a dynamic method like this simply it's unmaintainability?

# ILGenerator Generation, from Jason Bock at 07.10.2008 12:11:08 PM

Justin, I'm not sure if this is what you were asking for, but this is a cool add-in to make DM creation/development easier: http://www.codeplex.com/reflectoraddins/Wiki/View.aspx?title=ReflectionEmitLanguage&referringTitle=Home

Add a Comment

(*) = Required field
Name (*):

E-Mail (*):

Web Site:

Title (*):

Comments (*):

Enter the code you see (*)



Quote
"We must seek our salvation in this world, because there is no other." Matt Young
Twitter History
follow me on Twitter
Blog History