• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Fasterflect
 

Fasterflect

on

  • 3,894 views

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 ...

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.

Statistics

Views

Total Views
3,894
Views on SlideShare
2,540
Embed Views
1,354

Actions

Likes
1
Downloads
27
Comments
0

10 Embeds 1,354

http://www.buunguyen.net 1220
http://buunguyen.net 74
http://cms.kms-technology.com 32
http://cms.kms.com.vn 13
http://sotayblog.com 6
http://www.slideshare.net 3
http://localhost:5200 2
http://webcache.googleusercontent.com 2
http://static.slidesharecdn.com 1
http://localhost:8730 1
More...

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Fasterflect Fasterflect Presentation Transcript

    • Ultra Fast Dynamic Invocation in .NET with Fasterflect
      Buu Nguyen
    • Buu Nguyen
      Director of Technology, KMS Technology, www.kms-technology.com
      Lecturer, RMIT Vietnam, www.rmit.edu.vn
      www.buunguyen.net/blog
      Twitter: buunguyen
    • 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
    • Agenda
      Overview of .NET Reflection Capabilities
      How Fasterflect Helps with Invocation
      Using Fasterflect & Benchmark
      The Design of Fasterflect
      Future Direction
    • .NET Reflection
      #1 & #3 are well addressed, can hardly improve
      That leaves #2…
    • 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.
    • classPerson
      {
      privateint id;
      privateString name;
      privateint calories;
      privatestaticintinstanceCount;
      publicint Age { get; set; }
      publicStringNickName { get; set; }
      public Person(int id, string name) {
      this.id = id;
      this.name = name;
      instanceCount++;
      }
      publicvoid Eat(int calories) {
      this.calories += calories;
      }
      publicintGetCaloriesEaten() {
      return calories;
      }
      publicstaticintGetInstanceCount() {
      returninstanceCount;
      }
      }
    • Fasterflect Being Simpler
      .NET Reflection
      // Invoke constructor
      ConstructorInfoctor = type.GetConstructor(
      BindingFlags.Instance | BindingFlags.Public, null,
      CallingConventions.HasThis, new [] {typeof(int), typeof(string)}, null);
      objectobj = ctor.Invoke(newobject[]{10, "John"});
      // Update value of 'name' & print out 'name'
      FieldInfo field = type.GetField("name", BindingFlags.Instance | BindingFlags.NonPublic);
      field.SetValue(obj, "Jane");
      string name = (string)field.GetValue(obj);
      • Fasterflect
      varobj = type.Construct(10, "John");
      string name = obj.SetField("name", "Jane“)
      .GetField<string>("name");
    • …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);
    • 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…
    • 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);
    • 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)
    • Method Invocation Benchmark
    • Portion of Full Benchmark
    • 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
    • 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
    • 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
    • Code Generation Approaches
    • 3000-Feet Workflow
      API
      Delegate Cache
      Client
      invokes
      lookups delegate
      builds &
      invokes
      stores delegate
      Emitter 1
      Emitter 1
      Engine
      Emitter X
      generates CIL
    • TDD is Inevitable
      Why?
      Code complexity (lots of CIL)
      Goal of being a user-friendly API
      Approach
      Test-first
      95%+ test coverage
    • 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
    • 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