9

I am playing about the static reflection code from Joel Abrahamsson's blog and Daniel Cazzulino's blog. But I found their performance is kind of slow, even comparing with refection using "magic string".

int iterations = 1000000; 
watch.Start();

for (int i = 0; i < iterations; i++)
{
    var propertyOfName = Reflect<Employee>.GetProperty(c => c.Name); 
}

watch.Stop();
Console.WriteLine("[Reflector]: " + watch.ElapsedMilliseconds.ToString());
watch.Reset();
watch.Start();

for (int i = 0; i < iterations; i++)
{
    var propertyName = typeof (Employee).GetProperty("Name"); 
}

watch.Stop();
Console.WriteLine("[Regular Reflection]: " + watch.ElapsedMilliseconds.ToString());
watch.Reset();
watch.Start();

for (int i = 0; i < iterations; i++)
{
    var propertyName = StaticReflection.GetMemberName<Employee>(c => c.Name);
}

watch.Stop();
Console.WriteLine("[StaticReflection]: " + watch.ElapsedMilliseconds.ToString());

Here is the result:

  • [Reflector] : 37823
  • [Regular Reflection]: 780
  • [Static Reflection]: 24362

So why should we prefer Static Reflection? Just remove "magic string"? Or we should add some caching to improve static reflection performance?

2
  • 1
    Cache the result of the StaticReflection. If you ask for Employee.Name it will never change during the life-time of that appdomain so caching will be safe. Commented Aug 16, 2011 at 17:20
  • this isn't static reflection
    – user90843
    Commented May 13, 2014 at 20:11

2 Answers 2

4

The main reason to "prefer" it would be static type checking by the compiler, to ensure you don't make a mess of it (and to ensure it works if you obfuscate, for example). However, IMO it is so rare that a typo here is a significant bug (meaning: I'm not including the brain-dead typo you spot and fix during development / unit-testing); so (since I am a performance nut) I typically advise to use the simplest option (string). A particular example of this is when people are implementing the INotifyPropertyChanged interface using tricks like this. Just pass a string ;p

6
  • Thanks, Marc! it is interesting that Ayende did similar using .NET 2.0 couple years ago. Please see from ayende.com/blog/779/static-reflection His conclusion included performance improvement as well by using static reflection. Since his demo code cannot be downloaded any more. I cannot reproduce it.
    – Liang Wu
    Commented Aug 15, 2011 at 13:47
  • @Liang he was probably inspecting the byte[] impl directly to get the method-handle - probably noticeably faster than Expression. Commented Aug 15, 2011 at 18:31
  • Unfortunately building expression trees are notoriously slow and .NET doesn't cache expression trees automatically. Interestingly enough the overhead of building dynamic expressions in C# (using DynamicObject for instance) is lower than using the Expression<>. Commented Aug 16, 2011 at 17:24
  • 2
    @Fule I imagine that is a feature of being able to parameterise it arbitrarily. This means they can cache an expression that takes a parameter that is the "capture" class. This can then be reused for all callers. In regular LINQ the signature is pre-defined, so the expression tree must be built each time using.l a ConstantExpression of the capture instance. Commented Aug 16, 2011 at 17:33
  • @Fule further: it is the C# compiler, not .NET, that does (or does not do) the expression-tree cache here (but it is .NET via dynamic that caches the IL per resolved type) Commented Aug 16, 2011 at 17:35
2

Have you seen http://ayende.com/blog/779/static-reflection ? He was using just delegates (not expression trees) and had improved compare to normal Reflection performance.

Example of the implementation

 public static MethodInfo MethodInfo<TRet, A0>(Func<TRet, A0> func0)
{
        return func0.Method;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.