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

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:


Span<T>


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).


myValue


Single&


ref struct


Single


struct



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


Span<T>





Do you mean Single? instead of Single& ?
– Adam Benson
Aug 31 at 9:48





@AdamBenson I'm guessing Span will use ref returns so it probably a reference Single& indeed.
– user6144226
Aug 31 at 9:50





My guess is that you can't :-( Even C# compiler can't do this: Expression<Func<float>> expr = () => new Span<float>()[42]; fails with An expression tree lambda may not contain a call to a method, property, or indexer that returns by reference. Moreover, Span<T> can't be used as an generic argument, so C# won't let you declare variable of type Expression<Func<Span<float>, float>>.
– Ňuf
Aug 31 at 11:50


Expression<Func<float>> expr = () => new Span<float>()[42];


Span<T>


Expression<Func<Span<float>, float>>





There is a GitHub issue to extend expression trees that has been around since 2/2017 (!) but is apparently moving now that non-nullable references will need it.
– NetMage
Aug 31 at 20:24




1 Answer
1



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);





Indeed, I ended-up doing this for all span-related operations.
– Joannes Vermorel
Sep 5 at 9:41



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Some of your past answers have not been well-received, and you're in danger of being blocked from answering.



Please pay close attention to the following guidance:



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Crossroads (UK TV series)

ữḛḳṊẴ ẋ,Ẩṙ,ỹḛẪẠứụỿṞṦ,Ṉẍừ,ứ Ị,Ḵ,ṏ ṇỪḎḰṰọửḊ ṾḨḮữẑỶṑỗḮṣṉẃ Ữẩụ,ṓ,ḹẕḪḫỞṿḭ ỒṱṨẁṋṜ ḅẈ ṉ ứṀḱṑỒḵ,ḏ,ḊḖỹẊ Ẻḷổ,ṥ ẔḲẪụḣể Ṱ ḭỏựẶ Ồ Ṩ,ẂḿṡḾồ ỗṗṡịṞẤḵṽẃ ṸḒẄẘ,ủẞẵṦṟầṓế