Showing posts with label DLR. Show all posts
Showing posts with label DLR. Show all posts

Expression Trees, dynamic, and Multithreading in C# 3.0/4.0

I just had a brain fart the the other day: the dynamic keyword in C# 4.0 is more than just a way to plug interact dynamic languages. It can be used as a mechanism to evaluate any expression tree at runtime. One such example is that any arbitrary data source can be generically fashioned into dynamic objects that developers can interact with at run time. Take for example the following XML document:

<People>
<Person>
<Name>Koushik</Name>
<Age>27</Age>
</Person>
<Person>
<Name>Bobo</Name>
<Age>12</Age>
</Person>
</People>

Given a proper dynamic binding implementation for XML, the following pseudo code could be used to interact with this document:

XmlDocument doc = new XmlDocument();
doc.Load("file.xml");
dynamic dynamicDoc = DynamicXmlDocument.From(doc);

foreach (dynamic person in dynamicDoc.Person)
{
Console.WriteLine("Name: {0}", person.Name);
// modify the person's age in the document
person.Age++;
}
doc.Save("file.xml");

This approach can be used to access or modify any potential data source, such as a databases, web services, RPC, etc. What would normally be done with code generation tools (like xsd.exe, LINQ to SQL, wsdl.exe, etc) at design time can now be done at runtime. [0]

This train of thought continued until it derailed into another area I find interesting: multithreading (and parallel computing). Consider the following simple program (disregard the [Future] attribute for now, more on that later):

[Future]
static int LongOperation(int result)
{
Thread.Sleep(1000);
return result;
}
static void Main(string[] args)
{
int normalResult = LongOperation(LongOperation(1) + LongOperation(2)) + LongOperation(LongOperation(3) + LongOperation(4));
}

LongOperation is an operation that takes 1 second and returns the value it was passed. It is a pseudo function that simulates an operation that takes a significant amount of time to complete and return a result. Examine the expression that you see above: How long do you think it would take to complete? If you guessed 6 seconds, you guessed correctly.

Those operations are begging to be run asynchronously by way of threads. A friend of mine, Jared Parsons, released his implementation of “Futures” in his BclExtras library, which makes building concurrent operations fairly easy. A Future is an object that wraps a long running operation in a thread, and blocks only when the Value of that operation is necessary. That way, a developer can define several values they will need in the Future, and start computing the results concurrently. For example, to do the above asynchronously using Futures, it would be expressed as follows:

var op1 = Future.Create<int>(() => LongOperation(1));
var op2 = Future.Create<int>(() => LongOperation(2));
var op3 = Future.Create<int>(() => LongOperation(3));
var op4 = Future.Create<int>(() => LongOperation(4));
var outer1 = Future.Create<int>(() => LongOperation(op1.Value + op2.Value));
var outer2 = Future.Create<int>(() => LongOperation(op4.Value + op3.Value));
int futureResult = outer1.Value + outer2.Value;

This implementation, which uses multithreading, will take only 2 seconds to complete (1 second to complete op1-op4 simultaneously, 1 second to compute outer1 and outer2 simultaneously). But it is not very pretty; it is difficult to look at this code and understand what is going on.

And although this is easy enough to write, I had the thought that migrating code towards a concurrent model can be done really easily with “dynamic” types in C# by implementing a dynamic runtime to handle futures. Consider following pseudo code:

public class Foo
{
[Future]
public int LongOperation(int value)
{
Thread.Sleep(1000);
return value;
}
}

dynamic future = new FutureObject(new Foo());
int result = future.LongOperation(future.LongOperation(1) + future.LongOperation(2)) + future.LongOperation(LongOperation(3) + future.LongOperation(4));

This would work because the implementation of the dynamic runtime would create expression trees which handle method calls with the [Future] attribute specially: Instead of calling the method directly, it would create all Future<T> objects up front when the expression is initially evaluated or created. Since I do not have the Visual Studio 2010 CTP, I decided to forego implementing this for the moment (although the implementation of a Future<T> dynamic runtime would provide this nice syntactic support for DLR languages like IronPython and IronRuby).

However, C# 3.0 does have a very powerful Expression support that can be used to achieve similar results. To paraphrase from IanG’s blog post about Expressions:

Consider the following code:

Expression<Func<int, bool>> exprLambda = x => (x & 1) == 0;

This takes that Func delegate, and uses it as the type parameter for a generic type called Expression<T>. It then proceeds to initialize it in exactly the same way, so you'd think it was doing much the same thing. But it turns out that the compiler knows about this Expression<T> type, and behaves differently. Rather than compiling the lambda into IL that evaluates the expression, it generates IL that constructs a tree of objects representing the expression.

This was the point at which I went: "what the?.."

To be more explicit about this, here's roughly what that second line compiles into:

ParameterExpression xParam = Expression.Parameter(typeof(int), "x");
Expression<Func<int, bool>> exprLambda = Expression.Lambda<Func<int, bool>>(
Expression.EQ(
Expression.BitAnd(xParam, Expression.Constant(1)),
Expression.Constant(0)),
xParam);

Basically, given a normal C# expression, we can see the expression tree at runtime! If we walk the expression tree, and modify all the MethodCallExpressions that have the [Future] attribute, we can achieve similar results to the previous solution:

// Notice that I am not changing the contained expression below at all! It is the same as the normal code.
// The futures are created automatically.
int futureResult = FutureExpression.Process<int>(() =>
LongOperation(LongOperation(1) + LongOperation(2)) + LongOperation(LongOperation(3) + LongOperation(4))
);

Remember, the contents of the expression with LongOperation are not actually being executed; only an expression tree for that expression is being created. It is not analyzed until FutureExpression.Process is called with that expression.

This is pretty awesome! Syntactic support for automatic multithreading with little to no code changes:

  • Tag any long running methods with [Future]
  • Wrap your original expression in a FutureExpression.Process

But, there are still an issue with Expressions, in that they are somewhat limited. Expressions that are parseable at compile time must be single statements. For example, the following would not compile:

// does not compile: A lambda expression with a statement body cannot be converted to an expression tree
Expression<Func<int>> expr = () => { if (someValue == 2) return 0; return 3; };

However, it can be rewritten as the following to make it compile:

Expression<Func<int>> expr2 = () => someValue == 2 ? 0 : 3;

 

It is possible to alleviate some of this problem, albeit somewhat sloppily:

  • FutureExpression.Process<T> can return a Future<T> rather than T.
  • Future<T> can implement all the operators to return further Future<T> which executes an expression tree of that operator on T (this would fail if T does not support that operator) .
  • The result of an operator on two Futures is a Future, so the actual result is never analyzed until the Value is explicitly retrieved.

Anyhow, the code samples containing the FutureExpression analyzer is available for download. I may resume working on the dynamic runtime for Futures, as that seems rather interesting. But don’t hold your breath for another post. :)

 

[0] Obviously this would be at the cost of runtime performance. Another issue with dynamic objects is that there is no “strong” typing. This can be addressed by duck typing.

Duck-Typing (or Duct Taping) Dynamic Objects in C#

I've been delving into DLR and dynamic types as of late; in particular, how to enforce a static "contract" (an interface per se) on a dynamic type. During my perusal of blogs and articles, I came across an article by Tobi that delved into what I was trying to do, but not in a generic fashion. The interesting bit of it is that Tobi referenced another open source project called the Castle Project. This project, among other things, has some utility methods that allow developers to create class proxies and implement interfaces at runtime.The proxied objects could then have their method and property calls specially handled by an IInterceptor implementation. So, after reviewing of the code and a little bit of hacking, I came up with runtime duck typing solution:

Python Code:

import clr 

class Dragon:
def Quack(self):
print "ROAR!"

dragon = Dragon()

C# Code:

public interface Duck
{
void Quack();
}

public class Cow
{
public void Quack()
{
Console.WriteLine("Moo?");
}
}

class Program
{
static void Main(string[] args)
{
ScriptEngine pythonEngine = Python.CreateEngine();
ScriptRuntime runtime = pythonEngine.Runtime;
ScriptScope scope = runtime.ExecuteFile("hello.py");
IDynamicObject dynamicObject = (IDynamicObject)scope.GetVariable("dragon");

// I can't figure out how to do this without the runtime?
// Using GetMetaObject and building an expression tree?
Duck dragon = DuctTapeHelper.DuctTape<Duck>(dynamicObject, runtime);
Duck cow = DuctTapeHelper.DuctTape<Duck>(new Cow());

// Duck Type!
dragon.Quack();
cow.Quack();
}
}

Notice that neither "Cow" or "Dragon" implement the "Duck" interface, but at runtime, an interface proxy is generated to allow them to become Ducks!

Incidentally, another potential way to do this, without the dependency on the Castle Project utilities, is to use reflection to generate a IronRuby/IronPython script that implements an interface proxy.

Here's the full source code to my version of the DuctTape project. I am packaging the various projects from DLR and Castle into it as well. The code is nowhere near complete; it is just a functional prototype to make sure the concepts actually work! I feel that it could be done better through expression trees, but I couldn't figure out how to use an Expression tree to call into a dynamic method on IDynamicObject. Please let me know if you have more insight into this, as I am still a DLR noob.

 

Completely Unrelated:

Does anyone know of a good way to embed code into a blog? As you can see, my line breaks become all messed up with whatever tool I use (I use Live Writer with Paste from Visual Studio or Source Code Snippet).

Solved:

Blogger has some silly "Convert line breaks" option that is defaulted to Yes. As a result, it completely mangles all line breaks if you have a <pre> tag in your post body. Turn that off, and all is well.

Microsoft's DLR and Mono bring Python and Ruby to Android

device

With my recent work on porting Mono to Android, I began needing a way to create Java classes and invoke their various methods from C#. I had read briefly about the Dynamic Language Runtime and upcoming C# support for dynamic types; so that naturally led to me considering implementing a JNI-P/Invoke interop layer that allows access to Java/Dalvik code through C# and vice versa. However, I wasn't sure if the DLR would even run on Mono, but I was pleasantly surprised. Support for the DLR was added to Mono around a year and a half ago!

After copying the necessary DLLs over to the phone, I was able to run the simple ipy.exe console application that comes with the DLR source code. The neat thing I discovered while perusing through the documentation is that IronRuby and IronPython are actually compiled into CIL byte code and thus eventually native code! Anyhow, as you can see, IronPython is working dandily on the phone. [1]

The Dynamic Language Runtime is probably the coolest new toy in the .NET arsenal since lambda expressions. Couple the DLR with the new dynamic keyword in C# 4.0, and you can invoke dynamic languages from C# as easily as you can access .NET classes from IronPython or IronRuby![0]

Now to get back to implementing Java interop...

 

[0] The dynamic keyword basically provides compile time parsing of an expression tree, but the expression isn't actually validated until runtime.

[1] Though Mono + DLR is clocking in at a hefty 13mb in files. But, applications will soon be installable onto the SD card so the storage constraints will be a moot point.