Fasterflect (read either "Faster-flect" or "Fast-reflect") provides an alternative to the .NET Reflection API and is optimized for frequently used reflection features. The API addresses two problems of the built-in .NET Reflection API: verbose code and slow performance.
2. Buu Nguyen Director of Technology, KMS Technology, www.kms-technology.com Lecturer, RMIT Vietnam, www.rmit.edu.vn www.buunguyen.net/blog Twitter: buunguyen
3. Fasterflect is an open-source .NET library that makes the task of dynamically constructing objects and invoking methods, properties, fields etc. much simpler and (up to 400x) fasterthan the standard .NET Reflection API
4. Agenda Overview of .NET Reflection Capabilities How Fasterflect Helps with Invocation Using Fasterflect & Benchmark The Design of Fasterflect Future Direction
5. .NET Reflection #1 & #3 are well addressed, can hardly improve That leaves #2…
6. Complicated & Slow How complicated? Lookup metadata before invoking Lookup & invoke require many parameters Hard to get right Poor support for value type How slow? Up to 400 time slower than FF.
9. …Fasterflect Being Simpler varobj = type.Construct(1, "John"); // Chain property & field setting obj.SetProperty("Age", 20).SetField("id", 20); Console.WriteLine(obj.GetField<int>("id")); // Set properties & fields via anonymous type obj.SetProperties(new { Age = 20, NickName = "Jane" }) .SetFields(new { id = 10, name = "Peter" }); Console.WriteLine(obj.GetField<string>("name")); // Chain method calls var calories = obj.Invoke("Eat", 2000) .Invoke("Eat", 3000) .Invoke<int>("GetCaloriesEaten"); Console.WriteLine(calories); // Invoke static method varinstanceCount = type.Invoke<int>("GetInstanceCount"); Console.WriteLine(instanceCount);
10. Fasterflect Being Faster Previous .NET Reflection code executed 2,000,000 iterations 23.2 seconds vs. same run for Fasterflect 2.7 seconds x9 gain isn’t impressive? Fasterflect has a weapon for performance maniacs…
11. Enter Cached Delegates… Step 1. Generate delegates (once) Step 2. Invoke What would the execution time for 2,000,000 iterations be? varctor = type.DelegateForConstruct(new[] { typeof(int), typeof(string) }); var setter = type.DelegateForSetField("name"); var getter = type.DelegateForGetField("name"); varobj = ctor(1, "John"); setter(obj, "Jane"); string name = (string)getter(obj);
12. Time of 2,000,000 Calls 144 ms ~160-time increase in performance compared to .NET Reflection Up to 400-time in some cases (next slide)
15. Fasterflect Feature Set Support both reference type & value type Object construction Get/set static/instance fields Get/set static/instance properties Invoke static/instance methods Get/set indexers Get/set array elements Handle ref/out parameters Cache API available for most operations
16. The Design of Fasterflect Fluent API is implemented with .NET 3.5 extension methods The hard part is huge performance gain Invocation info not available at compile time for early binding Resorting to reflection at runtime is costly Solution: runtime code generation
17. Runtime Code Generation We have enough information at runtime to perform invocation w/o resorting to .NET Reflection, e.g. We can generate code like this, compile, load into memory, and invoke DirectInvoke() as need
20. TDD is Inevitable Why? Code complexity (lots of CIL) Goal of being a user-friendly API Approach Test-first 95%+ test coverage
21. Future Direction Add new invocation utilities Integrate with .NET Reflection metadata Support Silverlight’s CLR and .NET 4.0 CLR Build benchmark against 4.0’s ‘dynamic’ In consideration Build on .NET 4.0’s DynamicObject Add AOP capability
22. Download Fasterflect 1.1 http://fasterflect.codeplex.com/ Download include Binary (.NET 3.5) Source code & CHM doc Sample code Benchmark application Unit test cases Licensed under Apache 2.0