Open Emik03 opened 6 months ago
Tagging subscribers to this area: @dotnet/area-system-linq See info in area-owners.md if you want to be subscribed.
This would require converting the cited implementations to use iterators. It seems doable to me, but out of size concerns we've been reserving these for methods we deem to be high impact.
Would a compromise of just a single iterator class be acceptable then?
Presumably that would involve encoding Index
on top of Select
? That might improve Index().TryGetNonEnumeratedCount()
but the hidden delegate invocation feels like it might regress more common use cases. We'd be happy to consider a PR, provided that it's accompanied with comprehensive benchmarking.
I was thinking about that, yeah. I'll get to it some time later today then, and post a PR with a link back to this issue once I find the best balance. Thanks!
Is size really a concern now that trimming and NativeAOT exist (or, will with .NET 9)?
Consider the following code:
All three enumerables produce identical output, yet it is only the last one that is responsible for printing
True
, the rest printFalse
.Looking at the source code, this is to be expected, since the implementations of
Select(..., Func<T, int, TResult>)
andIndex
use iterator methods.My proposal is to create dedicated
Iterator
classes internally just asSelect(..., Func<T, TResult>)
does. I'm very willing to write that myself and contribute a PR for that, if this suggestion is approved.The main advantage with this change comes from the fact that
Select(..., Func<T, int, TResult>)
is a very common operation — hence whyIndex
has gotten a dedicated method in the first place — and allowing both to be countable without enumeration would result in a good amount of optimizations both withinSystem.Linq
(subsequent calls toConcat
,ElementAt
,Take
,ToDictionary
, andReverse
) and from any other library that makes use ofTryGetNonEnumeratedCount
.Potential drawbacks include:
OPTIMIZE_FOR_SIZE
#if
directive to fallback on iterator methods.TryGetNonEnumeratedCount
returningfalse
to force proper enumeration.