Ultra Fast Dynamic Invocation in .NET with Fasterflect<br />Buu Nguyen<br />
Buu Nguyen<br />Director of Technology, KMS Technology, www.kms-technology.com<br />Lecturer, RMIT Vietnam, www.rmit.edu.v...
Fasterflect is an open-source .NET library that makes the task of dynamically constructing objects and invoking methods, p...
Agenda<br />Overview of .NET Reflection Capabilities<br />How Fasterflect Helps with Invocation<br />Using Fasterflect & B...
.NET Reflection<br />#1 & #3 are well addressed, can hardly improve<br />That leaves #2…<br />
Complicated & Slow<br />How complicated?<br />Lookup metadata before invoking<br />Lookup & invoke require many parameters...
classPerson<br />{<br />privateint id;<br />privateString name;<br />privateint calories;<br />privatestaticintinstanceCou...
Fasterflect Being Simpler<br />.NET Reflection<br />// Invoke constructor<br />ConstructorInfoctor = type.GetConstructor(<...
…Fasterflect Being Simpler<br />varobj = type.Construct(1, &quot;John&quot;);<br />// Chain property & field setting<br />...
Fasterflect Being Faster<br />Previous .NET Reflection code executed 2,000,000 iterations<br />23.2 seconds<br />vs. same ...
Enter Cached Delegates…<br />Step 1.  Generate delegates (once)<br />Step 2.  Invoke<br />What would the execution time fo...
Time of 2,000,000 Calls<br />144 ms<br />~160-time increase in performance compared to .NET Reflection<br />Up to 400-time...
Method Invocation Benchmark<br />
Portion of Full Benchmark<br />
Fasterflect Feature Set<br />Support both reference type & value type<br />Object construction<br />Get/set static/instanc...
The Design of Fasterflect<br />Fluent API is implemented with .NET 3.5 extension methods<br />The hard part is huge perfor...
Runtime Code Generation<br />We have enough information at runtime to perform invocation w/o resorting to .NET Reflection,...
Code Generation Approaches<br />
3000-Feet Workflow<br />API<br />Delegate Cache<br />Client<br />invokes<br />lookups delegate<br />builds & <br />invokes...
TDD is Inevitable<br />Why?<br />Code complexity (lots of CIL)<br />Goal of being a user-friendly API<br />Approach<br />T...
Future Direction<br />Add new invocation utilities<br />Integrate with .NET Reflection metadata<br />Support Silverlight’s...
Download Fasterflect 1.1<br />http://fasterflect.codeplex.com/<br />Download include<br />Binary (.NET 3.5)<br />Source co...
Upcoming SlideShare
Loading in …5
×

Fasterflect

3,653 views

Published on

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.

Published in: Technology
0 Comments
3 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,653
On SlideShare
0
From Embeds
0
Number of Embeds
1,463
Actions
Shares
0
Downloads
39
Comments
0
Likes
3
Embeds 0
No embeds

No notes for slide

Fasterflect

  1. 1. Ultra Fast Dynamic Invocation in .NET with Fasterflect<br />Buu Nguyen<br />
  2. 2. Buu Nguyen<br />Director of Technology, KMS Technology, www.kms-technology.com<br />Lecturer, RMIT Vietnam, www.rmit.edu.vn<br />www.buunguyen.net/blog<br />Twitter: buunguyen<br />
  3. 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<br />
  4. 4. Agenda<br />Overview of .NET Reflection Capabilities<br />How Fasterflect Helps with Invocation<br />Using Fasterflect & Benchmark<br />The Design of Fasterflect<br />Future Direction<br />
  5. 5. .NET Reflection<br />#1 & #3 are well addressed, can hardly improve<br />That leaves #2…<br />
  6. 6. Complicated & Slow<br />How complicated?<br />Lookup metadata before invoking<br />Lookup & invoke require many parameters<br />Hard to get right<br />Poor support for value type<br />How slow? <br />Up to 400 time slower than FF.<br />
  7. 7. classPerson<br />{<br />privateint id;<br />privateString name;<br />privateint calories;<br />privatestaticintinstanceCount;<br />publicint Age { get; set; }<br />publicStringNickName { get; set; }<br />public Person(int id, string name) {<br />this.id = id;<br />this.name = name;<br />instanceCount++;<br /> }<br />publicvoid Eat(int calories) {<br />this.calories += calories;<br /> }<br />publicintGetCaloriesEaten() {<br />return calories;<br /> }<br />publicstaticintGetInstanceCount() {<br />returninstanceCount;<br /> }<br />}<br />
  8. 8. Fasterflect Being Simpler<br />.NET Reflection<br />// Invoke constructor<br />ConstructorInfoctor = type.GetConstructor(<br />BindingFlags.Instance | BindingFlags.Public, null,<br />CallingConventions.HasThis, new [] {typeof(int), typeof(string)}, null);<br />objectobj = ctor.Invoke(newobject[]{10, &quot;John&quot;});<br />// Update value of &apos;name&apos; & print out &apos;name&apos;<br />FieldInfo field = type.GetField(&quot;name&quot;, BindingFlags.Instance | BindingFlags.NonPublic);<br />field.SetValue(obj, &quot;Jane&quot;);<br />string name = (string)field.GetValue(obj);<br /><ul><li>Fasterflect</li></ul>varobj = type.Construct(10, &quot;John&quot;);<br />string name = obj.SetField(&quot;name&quot;, &quot;Jane“) <br /> .GetField&lt;string&gt;(&quot;name&quot;);<br />
  9. 9. …Fasterflect Being Simpler<br />varobj = type.Construct(1, &quot;John&quot;);<br />// Chain property & field setting<br />obj.SetProperty(&quot;Age&quot;, 20).SetField(&quot;id&quot;, 20);<br />Console.WriteLine(obj.GetField&lt;int&gt;(&quot;id&quot;));<br />// Set properties & fields via anonymous type<br />obj.SetProperties(new { Age = 20, NickName = &quot;Jane&quot; })<br /> .SetFields(new { id = 10, name = &quot;Peter&quot; });<br />Console.WriteLine(obj.GetField&lt;string&gt;(&quot;name&quot;));<br />// Chain method calls<br />var calories = obj.Invoke(&quot;Eat&quot;, 2000)<br /> .Invoke(&quot;Eat&quot;, 3000)<br /> .Invoke&lt;int&gt;(&quot;GetCaloriesEaten&quot;);<br />Console.WriteLine(calories);<br />// Invoke static method<br />varinstanceCount = type.Invoke&lt;int&gt;(&quot;GetInstanceCount&quot;);<br />Console.WriteLine(instanceCount);<br />
  10. 10. Fasterflect Being Faster<br />Previous .NET Reflection code executed 2,000,000 iterations<br />23.2 seconds<br />vs. same run for Fasterflect<br />2.7 seconds<br />x9 gain isn’t impressive?<br />Fasterflect has a weapon for performance maniacs…<br />
  11. 11. Enter Cached Delegates…<br />Step 1. Generate delegates (once)<br />Step 2. Invoke<br />What would the execution time for 2,000,000 iterations be?<br />varctor = type.DelegateForConstruct(new[] { typeof(int), typeof(string) });<br />var setter = type.DelegateForSetField(&quot;name&quot;);<br />var getter = type.DelegateForGetField(&quot;name&quot;);<br />varobj = ctor(1, &quot;John&quot;);<br />setter(obj, &quot;Jane&quot;);<br />string name = (string)getter(obj);<br />
  12. 12. Time of 2,000,000 Calls<br />144 ms<br />~160-time increase in performance compared to .NET Reflection<br />Up to 400-time in some cases (next slide)<br />
  13. 13. Method Invocation Benchmark<br />
  14. 14. Portion of Full Benchmark<br />
  15. 15. Fasterflect Feature Set<br />Support both reference type & value type<br />Object construction<br />Get/set static/instance fields<br />Get/set static/instance properties<br />Invoke static/instance methods<br />Get/set indexers<br />Get/set array elements<br />Handle ref/out parameters<br />Cache API available for most operations<br />
  16. 16. The Design of Fasterflect<br />Fluent API is implemented with .NET 3.5 extension methods<br />The hard part is huge performance gain<br />Invocation info not available at compile time for early binding<br />Resorting to reflection at runtime is costly<br />Solution: runtime code generation<br />
  17. 17. Runtime Code Generation<br />We have enough information at runtime to perform invocation w/o resorting to .NET Reflection, e.g. <br />We can generate code like this, compile, load into memory, and invoke DirectInvoke() as need<br />
  18. 18. Code Generation Approaches<br />
  19. 19. 3000-Feet Workflow<br />API<br />Delegate Cache<br />Client<br />invokes<br />lookups delegate<br />builds & <br />invokes<br />stores delegate<br />Emitter 1<br />Emitter 1<br />Engine<br />Emitter X<br />generates CIL<br />
  20. 20. TDD is Inevitable<br />Why?<br />Code complexity (lots of CIL)<br />Goal of being a user-friendly API<br />Approach<br />Test-first<br />95%+ test coverage<br />
  21. 21. Future Direction<br />Add new invocation utilities<br />Integrate with .NET Reflection metadata<br />Support Silverlight’s CLR and .NET 4.0 CLR<br />Build benchmark against 4.0’s ‘dynamic’<br />In consideration<br />Build on .NET 4.0’s DynamicObject<br />Add AOP capability<br />
  22. 22. Download Fasterflect 1.1<br />http://fasterflect.codeplex.com/<br />Download include<br />Binary (.NET 3.5)<br />Source code & CHM doc<br />Sample code<br />Benchmark application<br />Unit test cases<br />Licensed under Apache 2.0<br />

×