Dynamic programming languages are historically slow, right? So we could never use Python or Ruby for any kind of heavy-duty calculations, for example. Before Microsoft introduced the Dynamic Language Runtime (DLR), that may have been the obvious conclusion. However, in this highly interactive discussion, we're going to measure the performance of several computationally intensive operations in a static programming language and in a dynamic one. Thanks to the brilliance of the DLR team at Microsoft, you'll be surprised at the kinds of optimizations you get using those wonderfully expressive dynamic languages, making them truly fly on the .NET platform. You'll also learn some handy language integration tricks along the way. If you've ever thought that a dynamic programming language would help to solve a business problem in an elegant way but worried about the performance of the system, this presentation is for you.
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Dynamic Language Performance
1. Dynamic Language Performanceor How I Learned to Love Metaprogramming W. Kevin Hazzard, C# MVP NoVA Code Camp October 2009
2. Agenda MetaObjects and MetaProgramming Dynamic Language Runtime Architecture The dynamic keyword in C# 4 The CallSite<T> Class The CSharpRuntimeBinder DynamicMetaObjects Adaptive Inline Caching Demonstrations Fluent XML Parsing Performance of Python to C# Integration
3. Metaobject Visualization Metaobject control create invoke Backing Objects Users and programs interact with metaobjects, not directly with their backing objects.
5. The dynamic Keyword [Dynamic] objectDX = someFunction(); dynamic DX = someFunction(); DX( ‘A’ ); DX.Foo( 1 ); DX.Bar = ‘X’; int V = DX.Baz; DX[0] = 100; string S = DX[0]; // invoke the object // invoke the Foo member // set the Bar member // convert the Baz member to integer // set index 0 to 100 // convert index 0 to string Is dynamic a new type? No The dynamic type is totally static This type is a signal to the compiler to perform late binding
7. The CallSite<T> Class In the System.Runtime.CompilerServices namespace Created and managed in a static container class for each venue Key members: Create – creates and assign a binder Target – a delegate that invokes the binder Binder – connects an appropriate metaobject to the underlying call implementation
8. The CSharpRuntimeBinder What might these describe? Semantic Checker Symbol Table Expression Tree Creator Expression Factory Context Info Controller Dispatcher
9. What is a metaobject? An object that: May contain info on the: Creates Controls Invokes Describes Implements Interfaces of Type of Methods of Fields of Properties of Attributes of … another object
12. IDynamicMetaObjectProvider public interface IDynamicMetaObjectProvider { DynamicMetaObject GetMetaObject( Expression parameter ); } Pattern for implementing a custom metaobject The returned DynamicMetaObject must implement all twelve binding methods (and more) Lots of Expression Tree knowledge is required Not a trivial exercise
13. DynamicObject to the rescue! public class DynamicObject : IDynamicMetaObjectProvider { protected DynamicObject(); public virtual DynamicMetaObjectGetMetaObject(...); public virtual boolTryBinaryOperation(...); public virtual boolTryConvert(...); public virtual boolTryCreateInstance(...); public virtual boolTryDeleteIndex(...); public virtual boolTryDeleteMember(...); public virtual boolTryGetIndex(...); public virtual boolTryGetMember(...); public virtual boolTryInvoke(...); public virtual boolTryInvokeMember(...); public virtual boolTrySetIndex(...); public virtual boolTrySetMember(...); public virtual boolTryUnaryOperation(...); }
14. Example – Fluent XML Parsing Wouldn't it be nice if we could read and write XML like a set of nested objects without deep knowledge of the XML DOM or LINQ to XML? <books pubdate="2009-06-15"> <book price="45.99" title="Surgery for Dummies"> <id isbn10="4389880339"/> <authors> <author> <name> <first>Mortimer</first> <last>Snerdly</last> </name> <email address="mort@surgery.com"/> </author> </authors> </book> </books> WriteLine( books.book[0].authors.author[0].name.first);
16. Adaptive Inline Caching Each CallSite uses 3 levels of caching Level 0 – a dynamically generated delegate for some of the call patterns encountered at the site Level 1 – a read-only set of rules for all of the call patterns that the site has seen so far Level 2 – a read-only set of rules that spans multiple call sites
17. What about level 2 cache misses? A level 2 cache miss causes: The creation of a new rule The insertion of the new rule into the level 2 cache (on the ActionBinder) The insertion of the new rule into the level 1 cache (on the CallSite rule set) Possible insertion into the level 0 cache (on the CallSite dynamic delegate)
19. For More Information www.gotnet.biz/Blog www.voidspace.org.uk blogs.msdn.com/samng blogs.msdn.com/curth blogs.msdn.com/cburrows blogs.msdn.com/mmaly