Saturday, August 20, 2005

What Reflection APIs Are Slow and What Are Not?

Fast and Light Functions:
  • typeof
  • Object.GetType
  • typeof == Object.GetType
  • Type equivalence APIs (including typehandle operator overloads)
  • get_Module
  • get_MemberType
  • Some of the IsXX predicate APIs
  • New token/handle resolution APIs in the .NET Framework 2.0

Costly Functions:

  • GetXX APIs (MethodInfo, PropertyInfo, FieldInfo, and so on
  • GetCustomAttributes
  • Type.InvokeMember
  • Invoke APIs (MethodInfo.Invoke, FieldInfo.GetValue, and so on)
  • get_Name (Name property)Activator.CreateInstance

Generally, lightweight functions are fast for one of two reasons: either the reflection infrastructure has the information it needs already stored in super-fast runtime data structures, or the just-in-time (JIT) compiler detects these method calls and code patterns and generates special optimizations to speed things up a bit.

Adopted from Reflection: Dodge Common Performance Pitfalls to Craft Speedy Applications by Joel Pobar.

0 Comments:

Post a Comment

<< Home