Saturday, June 30, 2012

Dynamic Expression Evaluation In .NET

Many applications need to evaluate expressions at run-time. This can be done using a dynamic expression evaluator. Examples of such applications are data analysis applications that perform calculations from user-defined expressions, spreadsheet software like Excel which allows users to define formulae, etc.

There are several ways of doing it in .NET, each one having its own pros and cons. This post covers the various approaches and at the end compares their performance along with code examples for each one of them. These approaches can be categorized as follows

Parsing

One of the most common ways of achieving this is by writing a parser. The parser can parse the input expression, generate data structures and eventually evaluate the sub-expressions to find the result of the expression.

Developers can either write their parser or use one of the already available parsers. The biggest challenge with developers writing their own parser is that building a parser is quite complicated and requires a lot of coding effort. The advantage of using parsers for expression evaluation is that it doesn't require any external processes and the implementation can be pure managed .NET code.

Here is an expression evaluator from Code Project written in 100% Managed .NET. As the article points out, the expression goes through various phases like tokenization, parsing and interpretation before the final result is obtained. Fast Lightweight Expression Evaluator (Flee) is another expression evaluator from CodePlex. It uses a custom compiler, strongly-typed expression language and lightweight codegen to compile expressions directly to IL.

Runtime Compilation

Instead of building a custom parser, another alternative is using the .NET compilers. They can parse expressions in all the .NET languages and generate IL code. The best thing about compiled code is that execution of the code is quite fast but on the other hand it takes a significant time to compile the code.

The .NET framework includes a mechanism called the Code Document Object Model (CodeDOM) for this requirement. The System.CodeDom.Compiler namespace defines types for generating source code from CodeDOM graphs and managing the compilation of source code in supported languages. The .NET framework includes code generators and code compilers for C#, JScript and Visual Basic. Here is a tutorial on how CodeDom can be used for compiling C# code.

CodeDOM uses the compiler to compile the code, so it expects the compiler to exist on the clients system. The problem with CodeDom is that a new assembly is created and loaded into the AppDomain; and since we cannot unload an assembly in .NET, we end up with several unnecessary assemblies in memory. There are two solutions for this

  • Another temporary AppDomain can be created where the assemblies are loaded and the AppDomain can be disposed at a later time
  • Using DynamicMethod to create a method at runtime. The IL code for this can be obtained from the CodeDOM. Here is a tutorial for this

The Eval Function

Eval is a function which evaluates a string as though it was an expression and returns a result. Most of the languages that support eval are scripting languages like JavaScript, Python, etc. This is because most of the scripting languages are interpreted, not compiled. This provides these languages enormous capability to evaluate dynamic expressions since an eval call is nothing but another line to interpret at runtime. Compiled languages like Java and the .NET languages (like C# and VB.NET) don't have this flexibility since they have to generate IL/Byte code before they run. However some of the .NET language do support eval even though they are compiled.

JScript.NET is Microsoft's implementation of ECMAScript in the .NET initiative. JScript.NET contains an eval function which can evaluate dynamic expression. It compiles the code every time eval called and then runs it, so there definitely is a performance hit. The downside of eval has been explained in this article.

IronPython and IronRuby are the .NET implementations of the Python and Ruby scripting languages. These are dynamic scripting languages unlike C# and VB.NET which run on the Dynamic Language Runtime. With the DLR, several dynamic constructs like eval could be implemented and since it is based on .NET, it is interoperable with C# and VB.NET. Here is a tutorial on embedding IronPython in C#.

Microsoft Roslyn

Microsoft Roslyn is Microsoft's biggest change in compilers since the advent of .NET. However this is currently in CTP and isn't available in .NET 4.0. Roslyn provides the compiler as a service (library). This is better than CodeDOM since it won't create new processes. Roslyn has wonderful features especially for Dynamic Compilations, Code Reviewers, etc. Here is a tutorial on using Roslyn with C#.

The performance benchmarks of these approaches are as follows. The code used to obtain these benchmarks is available here.

19 comments:

maulikCE said...

Spring Framework is worth evaluating

maulikCE said...

Would you please explain why Jscript.Net takes minimal time compared to IronPython despite both being DLR based and hit of "eval" in JS.

lalala said...

Dear Gautam,

We recently found your blog, loved it, and would like to invite you to join us at Storylane! We know you are passionate about technology so I know you will feel right at home with us at Storylane!

We aim to create a community where people can share stories and opinions or views about topics that may inspire or connect fellow members. At the end we are aiming for a collection of human opinions and experiences and I feel like with your help and contribution, we would be much closer to reaching our goal!

Our site can also help you in promoting your content, while in return you’re helping us in our goal to promote meaningful content many people can benefit from. You can do this by joining us, sharing your new, or already existing stories/opinions.

I really hope you'll give it a try! Thank you so much for your time and please feel free to visit our site at Storylane.com.

Please let me know if this is something you would be interested in and don't hesitate to contact me if you have any further questions at Luba@storylane.com!

Best Regards,

Luba

what is video said...

Thanks for the great read. Good blog.

Galvin Alvarado said...

The eval() function evaluates or executes an argument.If the argument is an either Expression or JavaScript statements, eval() evaluates simultaneously.

Tech Blog said...

For many years, technology has induced new innovations and gadgets to make our lives easier. It has changed the way people think and today it offers almost every other equipment with low cost and utmost usage. Today the vast usage of technology can be understood by going through the IT blog or Tech blog.

Tech Blog

Unknown said...

very nice post, thank you for posting this.
for Latest Tech News check my blog Latest News Update 24x7.

Reset hotmail password said...

thanks, your post very Nice and Great. It is very helpful for me. I wish you to check Reset Hotmail password

navya said...

I have read your blog its very attractive and impressive. I like it your blog.

.Net Training in Chennai | .Net Online Training | Dot Net Training in Chennai

Dot Net Online Training | LINQ Online Training

navya said...

I have read your blog its very attractive and impressive. I like it your blog.

.Net Training in Chennai | .Net Online Training | Dot Net Training in Chennai

Dot Net Online Training | LINQ Online Training

candy lol said...

This is so fun! What a great idea. Also I love how authentic you seem to be. Your style and passion for blogging is contagious. Thank you for sharing your life!
run3.me
Basketballlegendsgame.com

jeeva said...

I found your blog while searching for the updates, I am happy to be here. Very useful content and also easily understandable providing.. Believe me I did wrote an post about tutorials for beginners with reference of your blog. 

rpa training in chennai
rpa training in bangalore
rpa course in bangalore
best rpa training in bangalore
rpa online training

Mounika said...

Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.
python training in rajajinagar
Python training in bangalore
Python training in usa

Unknown said...

Well Said, you have furnished the right information that will be useful to anyone at all time. Thanks for sharing your Ideas.

Java training in Bangalore | Java training in Marathahalli

Java training in Bangalore | Java training in Btm layout

Java training in Bangalore |Java training in Rajaji nagar

Java training in Bangalore | Java training in Kalyan nagar

nilashri said...

Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
Data Science Training in Chennai | Data Science Training institute in Chennai
Data Science course in anna nagar
Data Science course in chennai | Data Science Training institute in Chennai | Best Data Science Training in Chennai
Data science course in Bangalore | Data Science Training institute in Bangalore | Best Data Science Training in Bangalore
Data Science course in marathahalli | Data Science training in Bangalore

rocky said...

Useful information!!!. Great post!!!.
Python Training in Chennai | Certification | Online Course Training | Python Training in Bangalore | Certification | Online Course Training | Python Training in Hyderabad | Certification | Online Course Training | Python Training in Coimbatore | Certification | Online Course Training | Python Training in Online | Certification | Online Course Training

Kanika said...

Good Post! , it was so good to read and useful to improve my knowledge as an updated one, keep blogging. After seeing your article I want to say that also a well-written article with some very good information which is very useful for the readers....thanks for sharing it and do share more posts like this.
Data Science Online Training

Mrbk30 said...

Very Informative blog thank you for sharing. Keep sharing.

Best software training institute in Chennai. Make your career development the best by learning software courses.

msbi training in chennai
devops training in chennai
php training in chennai

Joseph Vijay said...

Much obliged for sharing this brilliant substance. its extremely fascinating. Numerous web journals I see these days don't actually give whatever pulls in others however the manner in which you have plainly clarified everything it's truly awesome. There are loads of posts But your method of Writing is so Good and Knowledgeable. continue to post such helpful data and view my site too...
Fold n fly | Classic dart paper airplane | how to make a paper airplane that flies far and straight step by step | windfin | stable paper airplane | nakamura paper airplane | paper airplane templates for distance