- Notifications
You must be signed in to change notification settings - Fork5.2k
Derive OrderedEnumerable from Iterator#98874
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Uh oh!
There was an error while loading.Please reload this page.
Conversation
The OrderedEnumerable implementation today doesn't derive from Iterator, and it has a GetEnumerator method implemented with yield. That means the optimization that allows the enumerable to be reused as the enumerator doesn't kick in, and we end up allocating a separate enumerator object in order to iterate through an Order{By} enumerable.ghost commentedFeb 23, 2024
Tagging subscribers to this area: @dotnet/area-system-linq Issue DetailsThe OrderedEnumerable implementation today doesn't derive from Iterator, and it has a GetEnumerator method implemented with yield. That means the optimization that allows the enumerable to be reused as the enumerator doesn't kick in, and we end up allocating a separate enumerator object in order to iterate through an Order{By} enumerable.
usingBenchmarkDotNet.Attributes;usingBenchmarkDotNet.Running;BenchmarkSwitcher.FromAssembly(typeof(Tests).Assembly).Run(args);[MemoryDiagnoser(false)][HideColumns("Job","Error","StdDev","Median","RatioSD")]publicpartialclassTests{privateint[]_ints;privatePerson[]_people;[Params(2,100)]publicintLength{get;set;}[GlobalSetup]publicvoidSetup(){_ints=Enumerable.Range(0,Length).Reverse().ToArray();_people=_ints.Select(i=>newPerson{Age=i}).ToArray();}[Benchmark]publicintOrder(){intsum=0;foreach(intiin_ints.Order())sum+=i;returnsum;}[Benchmark]publicintOrderBy(){intsum=0;foreach(Personpin_people.OrderBy(p=>p.Age))sum+=p.Age;returnsum;}publicstructPerson{publicintAge{get;set;}}}
|
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
The OrderedEnumerable implementation today doesn't derive from Iterator, and it has a GetEnumerator method implemented with yield. That means the optimization that allows the enumerable to be reused as the enumerator doesn't kick in, and we end up allocating a separate enumerator object in order to iterate through an Order{By} enumerable.