Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get a value out of a Span<T> with Linq expression trees?

I would like to use Linq expression trees to call the indexer of a Span<T>. The code looks like:

var spanGetter = typeof(Span<>)
    .MakeGenericType(typeof(float)).GetMethod("get_Item");

var myFloatSpan = Expression.Parameter(typeof(Span<float>), "s");

var myValue = Expression.Call(
    myFloatSpan,
    spanGetter,
    Expression.Constant(42));

var myAdd = Expression.Add(
    myValue,
    Expression.Constant(13f));    

Yet, this code fails because myValue is of type Single& (aka ref struct) instead of type Single (aka struct).

How to evaluate a Span<T> from an expression tree?

like image 200
Joannes Vermorel Avatar asked Aug 31 '18 09:08

Joannes Vermorel


1 Answers

I have a solution, but it's far from being ideal, as you'll see. We re-use C# syntactic sugar engine.

class Program
{
    static void Main(string[] args)
    {
        var spanGetter = typeof(Program).GetMethod("GetItem").MakeGenericMethod(typeof(float));

        var myFloatSpan = Expression.Parameter(typeof(Span<float>), "s");

        var myValue = Expression.Call(
            null,
            spanGetter,
            myFloatSpan,
            Expression.Constant(42));

        var myAdd = Expression.Add(
            myValue,
            Expression.Constant(13f));

        var expr = Expression.Lambda<MyFunc>(myAdd, myFloatSpan).Compile();

        var span = new Span<float>(new float[43]);
        span[42] = 12.3456f;
        Console.WriteLine(expr(span)); // -> 25.3456
    }

    // hopefully, this shouldn't be too bad in terms of performance...
    // C# knows how to do compile this, while Linq Expressions doesn't
    public static T GetItem<T>(Span<T> span, int index) => span[index];

    // we need that because we can't use a Span<T> directly with Func<T>
    // we could make it generic also I guess
    public delegate float MyFunc(Span<float> span);
}
like image 159
Simon Mourier Avatar answered Nov 16 '22 01:11

Simon Mourier