Sunday, May 5, 2013

Sasa.Operators<T> Overhaul - Now With More Generic Operator Goodness

Sasa.Operators<T> was covered in a previous post, and was useful in its own right, but was still somewhat limited in the operators it could expose. Since it abstracted only over a single type parameter T, it exposed those operators defined only on T. For instance, addition has signature "T add(T, T)", negation is "T negate(T)", and so on.

But not all operators are defined on only a single type. For instance, System.Decimal provides addition operators that work on integers, both signed and unsigned. Sasa.Operators<T> couldn't handle that. It can now.

Sasa.Operators is now a fully generic operator framework, exposing static generic class types Operators<T> as before, Operators<T0, T1> which exposes operators defined over two type parameters, like equals whose signature is "bool equals(T0, T1)", and finally, Operators<T0,T1,T2> for operators defined over three possible types, like addition "T2 add(T0, T1)". Furthermore, all overloadable operators are now accessible, including True/False and explicit and implicit conversions.

The delegates accessible via the above Operators classes are also now more efficient, and don't rely on LINQ expression compilation. This was all possible due to a new function I introduced under Sasa.Func called "Operator". It takes a delegate signature as a type parameter, and a Sasa.Operator value designating the operator type, and it searches for that static operator method that matches the delegate signature needed. Then it creates a direct delegate to that method of the given signature, so invocation via Operators is simply a direct virtual dispatch into a static method.

The only exception is for primitive types, like Int32 and Int64, because they don't provide static method operators. When the arguments are all primitives and no operator method is available, a small stub function is dynamically generated that implements the operation in efficient bytecode. Can't get much faster than this.

All of this was precipitated by my implementation of a LINQ expression interpreter in the Sasa.Linq assembly. You can now easily evaluate almost any LINQ expression with a single call:

var z = CLR.Eval(() => 3 * 2.0 + 1); // z = 7.0
var x = CLR.Eval(() => new Func<int, int>(z => z + 1)(3)); // x=4

This is in alpha status obviously, but it passes a few tests, and more will come. Obviously LINQ expressions already have compilation built-in, but sometimes dynamic compilation either isn't available, or is too costly to perform. For instance, consider the case of compiling a LINQ to SQL query. You don't want to dynamically generate code every time you want to simplify a LINQ expression. An interpreter is the right choice here due to its much smaller overhead.


David said...

Very cool! Was trying to build a version of this when I was in grad school, and ran into the expression problem, which you'd solved with some excellent visitor-fu.

Can you comment on which platforms and profiles are supported in Sasa? Your comments on dynamic compilation had me wondering if this would work with things like portable libraries, Silverlight, or the Xamarin iOS AOT compiler...or even MS's own pre-JITer (forget the name).

Sandro Magi said...

Dynamic compilation is only used for operators involving only CLR primitives, because primitives don't have operators. Big mistake on MS's part IMO. Objects that have actual operators simply create delegates, so those should safe to use in any context.

I haven't tested against other profiles though, other than client profiles. Dynamic compilation is certainly a concern in some of those other scenarios, but in principle all the necessary overloads could be pre-compiled. All I can say is, give it a try!