You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[API Proposal]: Annotate TSource with allows ref struct for both sync/async LINQ #111830
Support IEnumerable<TSource>/IAsyncEnumerable<TSource> where TSource is a ref struct in LINQ methods where appropriate/possible to allow using these in such cases. E.g. there are cases where TSource points to internal state of the enumerable source and one may want to prevent ToArray/ToList but would still like to support transformations like Select or reductions like Count or similar.
In general, all LINQ methods that do not involve storing/caching TSource on the heap should annotated with allows ref struct.
Currently, the lack of allows ref struct prevents LINQ usage in such cases which either means no support or having to reinvent the wheel for this, which also means a less than ideal user experience since standard and known method cannot be used.
Above support likely involves annotating support types like:
namespaceSystem.Runtime.CompilerServices[StructLayout(LayoutKind.Auto)]publicreadonlystructConfiguredCancelableAsyncEnumerable<T>
#if NET9_0_OR_GREATERwhere T : allows refstruct
#endif
API Usage
No new API usage as such just expanded support via annotation.
Alternative Designs
None.
Risks
Given allows ref struct defines a contract, forward compatibility may be a risk if one later decides to rewrite a given LINQ method in a way that is incompatible with this support. Risk should be very low and considered on a method by method basis.
As mentioned in #111685 changing this will no doubt require changes to several other parts incl ConfigureAwait/ WithCancellation/ConfiguredCancelableAsyncEnumerable as in:
I don't believe this would be feasible in practice. Many Linq methods wouldn't be able to support the constraint in principle, resulting in inconsistency between the various methods. Those that could support it currently apply performance optimizations using runtime type checks:
I don't think it would be feasible to keep those while adding the allows ref struct constraint.
The constraint was added to IEnumerable<T> and IAsyncEnumerable<T> in the spirit of unblocking bespoke applications that need to use the abstractions, but I don't believe it signals any intention of extending it everywhere up the stack.
Naturally, ArraySelectIterator or similar optimization that rely on the enumerable being backed by heap memory storing TSource cannot be used given this, but shouldn't this be possible for a enumerable that is not that? If the required "branching" is not expressible in C# I would think this needs to be addressed somehow...
Naturally, ArraySelectIterator or similar optimization that rely on the enumerable being backed by heap memory storing TSource cannot be used given this, but shouldn't this be possible for a enumerable that is not that?
You currently can't express such tests, eg sharplab
Activity
nietras commentedon Jan 25, 2025
As mentioned in #111685 changing this will no doubt require changes to several other parts incl
ConfigureAwait/WithCancellation/ConfiguredCancelableAsyncEnumerableas in:https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Threading/Tasks/TaskAsyncEnumerableExtensions.cs
huoyaoyuan commentedon Jan 25, 2025
Related to #108131
It's currently difficult to implement for many operators.
eiriktsarpalis commentedon Jan 26, 2025
I don't believe this would be feasible in practice. Many Linq methods wouldn't be able to support the constraint in principle, resulting in inconsistency between the various methods. Those that could support it currently apply performance optimizations using runtime type checks:
runtime/src/libraries/System.Linq/src/System/Linq/Select.cs
Lines 26 to 49 in a40f92e
I don't think it would be feasible to keep those while adding the
allows ref structconstraint.The constraint was added to
IEnumerable<T>andIAsyncEnumerable<T>in the spirit of unblocking bespoke applications that need to use the abstractions, but I don't believe it signals any intention of extending it everywhere up the stack.nietras commentedon Jan 26, 2025
Naturally,
ArraySelectIteratoror similar optimization that rely on the enumerable being backed by heap memory storingTSourcecannot be used given this, but shouldn't this be possible for a enumerable that is not that? If the required "branching" is not expressible in C# I would think this needs to be addressed somehow...stephentoub commentedon Jan 26, 2025
You currently can't express such tests, eg
sharplab
eiriktsarpalis commentedon Jan 26, 2025
I think we can close this as not planned, for now. We can revisit assuming future versions of the language permit more versatility.