Static Reflection in .NET, part 2

A few weeks ago, I talked about static reflection and its advantages. You’ll remember that the main advantages, compared to the normal reflection API’s, are the compile time checking of parameters and IntelliSense support.

How does it compare at other levels, performance for example? Before we dive into that question, let me state that performance may or may not be important to you. A program that is fast enough is, well, fast enough. It’s unlikely that a (single) reflection call will have a significant impact on, say, the response time of a graphical user interface, and so performance doesn’t matter. If you’re algorithm requires millions of reflection operations, I’m sure you can rewrite it somehow to reduce that number significantly, and then performance again probably doesn’t matter anymore. That being said, we still want to know, right?

First of all, let’s compare code.

Take this line (using the Example class from the last post):

PropertyInfo pi = typeof(Example).GetProperty("Description");

This line compiles to the following IL (simplified for readability):

ldtoken Example 
call class Type Type::GetTypeFromHandle(valuetype RuntimeTypeHandle) 
ldstr "Description" 
call instance class PropertyInfo Type::GetProperty(string)

Compare that to the following line:

PropertyInfo pi = StaticReflector.Create<Example>().PropertyInfo(e => e.Description);

Which compiles to:

call class IStaticReflector`1<!!0> StaticReflector::Create<class Example>()
ldtoken Example
call class Type Type::GetTypeFromHandle(valuetype RuntimeTypeHandle)
ldstr "e"
call class ParameterExpression Expression::Parameter(class Type, string)
stloc.0 
ldloc.0 
ldtoken instance string Example::get_Description()
call class MethodBase MethodBase::GetMethodFromHandle(valuetype RuntimeMethodHandle)
castclass MethodInfo
call class MemberExpression Expression::Property(class Expression, class MethodInfo)
ldc.i4.1 
newarr ParameterExpression
stloc.1 
ldloc.1 
ldc.i4.0 
ldloc.0 
stelem.ref 
ldloc.1 
call class Expression`1<!!0> Expression::Lambda<class System.Func`2<class Example, string>>(class Expression, class ParameterExpression[])
call class PropertyInfo StaticReflectorExtensions::PropertyInfo<class Example, string>(class IStaticReflector`1<!!0>, class Expression`1<class System.Func`2<!!0, !!1>>)

As you can see, this code doesn’t load the “Description” string, it uses the ldtoken instruction instead. Some bloggers have suggested that this would make it more efficient. Unfortunately, even if the ldtoken instruction is efficient, it is largely offset by the construction of the lambda expression. I ran a little benchmark, in which I compare execution time (in ticks) and memory usage (in generation 0 garbage collection runs) of both approaches, executing each one a million times. This is the result (on my laptop):

Using Reflection       Time:    1089308 Collections:    45
Using StaticReflection Time:   13513777 Collections:   264

As you can see, the Static Reflection approach is about 13.5 times slower than the good old dynamic reflection, and it uses a lot more memory. That should be no surprise either: both cases allocate a PropertyInfo object, but the static case also allocates the expression, which is nothing but food for the garbage collector.

So, one approach seems good at compile time, and the other is good at run time. It seems we’re stuck between a rock and a hard place. But the situation isn’t so bad: we have two options to choose from, each with their pro’s and con’s. What the best one is depends on your requirements, and what you value the most: compile time checking (which may result in productivity and maintainability benefits), or performance.

And who knows, maybe there is a third option, giving the best of both worlds. But that’s for next time.


Comments

January 6. 2010 07:53 AM

burn the fat feed the muscle

Valuable information and excellent design you got here! I would like to thank you for sharing your thoughts and time into the stuff you post!! Thumbs up

burn the fat feed the muscle

January 9. 2010 07:41 AM

http://www.rugsreviews.com

While this subject can be very touchy for most people, my opinion is that there has to be a middle or common ground that we all can find. I do appreciate that youve added relevant and intelligent commentary here though. Thank you!

http://www.rugsreviews.com

January 12. 2010 12:39 AM

tatuaggi sole

Nice information, many thanks to the author. It is incomprehensible to me now, but in general, the usefulness and significance is overwhelming. Thanks again and good luck!

tatuaggi sole

January 12. 2010 03:23 PM

Ed Hardy

I do appreciate that youve added relevant and intelligent commentary here though. Thank you!

Ed Hardy

January 13. 2010 03:09 AM

valentine puzzle

Couldn?t be written any better. Reading this post reminds me of my old room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thanks for sharing!

valentine puzzle

January 15. 2010 05:47 AM

diet pills that work

The blog was absolutely fantastic! Lots of great information and inspiration, both of which we all need!

diet pills that work

January 15. 2010 07:44 AM

Astral Travel

Thanks for taking the time to discuss this, I feel strongly about it and love learning more on this topic. If possible, as you gain expertise, would you mind updating your blog with more information? It is extremely helpful for me.

Astral Travel

January 15. 2010 08:57 AM

Abdominal Exercises

The beauty of these blogging engines and CMS platforms is the lack of limitations and ease of manipulation that allows developers to implement rich content and 'skin' the site in such a way that with very little effort one would never notice what it is making the site tick all without limiting content and effectiveness.

Abdominal Exercises

January 15. 2010 04:10 PM

Stainless Steel Sinks

I’m not yet familiar with your article, but then I want to try it in my own computer. Thanks for sharing this information; I’ll keep it as homework and stud the following codes as well. Thanks a lot!

Stainless Steel Sinks

January 17. 2010 01:01 AM

chiropractic marketing

Hi webmaster, commenters and everybody else !!! The blog was absolutely fantastic! Lots of great information and inspiration, both of which we all need!b Keep 'em coming... you all do such a great job at such Concepts... can't tell you how much I, for one appreciate all you do!

chiropractic marketing

January 18. 2010 08:47 PM

computer hjælp

Dude.. I am not much into reading, but somehow I got to read lots of articles on your blog. Its amazing how interesting it is for me to visit you very often.

computer hjælp

January 18. 2010 09:02 PM

home insurance cincinnati

You may have not intended to do so, but I think you have managed to express the state of mind that a lot of people are in. The sense of wanting to help, but not knowing how or where, is something a lot of us are going through.

home insurance cincinnati

January 22. 2010 11:59 PM

rosacea treatment

Keep 'em coming... you all do such a great job at such Concepts... can't tell you how much I, for one appreciate all you do!

rosacea treatment

January 23. 2010 12:00 AM

FBF

Keep 'em coming... you all do such a great job at such Concepts... can't tell you how much I, for one appreciate all you do!

FBF

January 23. 2010 11:54 PM

roulette system

I personally have embraced the new technologies and the CMS platforms, I think the new tools only make the web designs better. I am glad that new technologies are coming out in web design that make things easier, improved, and better looking for design.

roulette system

January 24. 2010 07:21 PM

Business Promotion Techniques

From low-cost business promotion techniques through web site promotion, ... Want to attract new business but have a small marketing budget or none at all?

Business Promotion Techniques

January 25. 2010 12:20 AM

money making opportunities

I am not much of a guy who thinks in so deeply about web design but I think your post had some valid points in it. Like designers are forced to design stuff within the limited code available and not go beyond it, their innovation is somewhat limited but still I think Web Design won't die! I agree that Amazon and other some big sites won't have a blog but now a days it's very important to have some sort of option available so people can quickly communicate their thoughts. I think Amazon if wants to shift it to that, they can get a customized CMS for themselves.

money making opportunities

January 26. 2010 08:53 AM

Pro Male Choice

nice

Pro Male Choice

February 11. 2010 02:35 AM

KonnieuG

I opine that you do a good contribution writing your interesting thought. After this the writing services will be able to use it for creating the <a href="quality-papers.com/.../classification_essay">classification essay writing</a>. Thence university students will buy papers easier.  

KonnieuG

February 12. 2010 04:49 PM

REISEN NACH JORDANIEN

niteer posten

REISEN NACH JORDANIEN

February 22. 2010 12:35 AM

chiropractic marketing

Do you accept guest posts? I would love to write couple articles here.
I was wondering what is up with that weird gravatar??? I know 5am is early and I'm not looking my best at that hour, but I hope I don't look like this! I might however make that face if I'm asked to do 100 pushups. lol

chiropractic marketing

February 24. 2010 12:37 AM

make money online

Excellent read, I just passed this onto a colleague who was doing a little research on that. And he actually bought me lunch because I found it for him smile So let me rephrase that: Thanks for lunch!

make money online

February 25. 2010 09:07 PM

electronic cigarette

You really make it seem so easy with your presentation but I find this topic to be really something which I think I would never understand. It seems too complicated and very broad for me. I am looking forward for your next post, I will try to get the hang

electronic cigarette

February 26. 2010 01:00 AM

stock loan

Couldn?t be written any better. Reading this post reminds me of my old room mate! He always kept talking about this. I will forward this article to him. Pretty sure he will have a good read. Thanks for sharing!

stock loan

February 28. 2010 01:47 AM

dr. britt borden

There are certainly a lot of details like that to take into consideration. That is a great point to bring up. I offer the thoughts above as general inspiration but clearly there are questions like the one you bring up where the most important thing will be working in honest good faith. I don?t know if best practices have emerged around things like that, but I am sure that your job is clearly identified as a fair game.

dr. britt borden

February 28. 2010 04:09 AM

umbrella payroll

I admire what you have done here. I like the part where you say you are doing this to give back but I would assume by all the comments that this is working for you as well.

umbrella payroll

March 3. 2010 09:01 PM

Meyersville NJ Plumbing

There are certainly a lot of details like that to take into consideration. That is a great point to bring up. I offer the thoughts above as general inspiration but clearly there are questions like the one you bring up where the most important thing will be working in honest good faith. I don?t know if best practices have emerged around things like that, but I am sure that your job is clearly identified as a fair game.

Meyersville NJ Plumbing

March 6. 2010 03:17 AM

boards

This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.

boards

March 9. 2010 08:57 AM

gary gregory

This is a really good read for me, Must admit that you are one of the best bloggers I ever saw.Thanks for posting this informative article.

gary gregory

March 10. 2010 01:18 AM

Garmin gps watches

This is my first time i visit here. I found so many interesting stuff in your blog especially its discussion. From the tons of comments on your articles, I guess I am not the only one having all the enjoyment here! keep up the good work.

Garmin gps watches

March 10. 2010 06:10 AM

oregon divorce lawyer

I personally have embraced the new technologies and the CMS platforms, I think the new tools only make the web designs better. I am glad that new technologies are coming out in web design that make things easier, improved, and better looking for design.

oregon divorce lawyer

Add comment


(Will show your Gravatar icon)

  Country flag

biuquote
  • Comment
  • Preview
Loading