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

Harry Parker said...

great post you have done here. i wish you to check Sky Customer Service Contact Number

soniya rawat 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

Chang bui thi said...

great article, I was very impressed about it, wish you would have stayed next share
baixar facebook
baixar whatsapp
unblocked games

Shariq Waseem said...

http://b2bad.in/Packers-and-Movers-in-Delhi/ @ Movers and packers in Delhi http://b2bad.in/Packers-and-Movers-in-Delhi/ @ Delhi packers and movers

saki said...

this is really too useful and have more ideas from yours. keep sharing many techniques. eagerly waiting for your new blog and useful information. keep doing more.
PHP Training in Chennai

Naviya Nair 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

Naviya Nair 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

Philips Huges said...



Thanks admin for sharing the unique content, you have done a great job

Installment loans in Mississippi
Payday loans in Mississippi
Title loans in Mississippi

Priya Kannan said...

This information really worth saying, i think you are master of the content and thank you so much sharing that valuable information and get new skills after refer that post.
Dot Net Training in Chennai

Alice Taylor said...

Your topic is very great and useful for us…thank you

http://word-cookies-answers.com

Thu LĂȘ said...

I was very impressed by this post, this site has always been pleasant news. Thank you very much for such an interesting post. Keep working, great job! In my free time, I like play game: douchebagworkout2.org. What about you?

Games 2 Girls said...

Great post,Thanks for providing us this great knowledge,Keep it up.
www.baixar-whatsapp.com.br