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>
@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.
Do you mean Single? instead of Single& ?
– Adam Benson
Aug 31 at 9:48