Repository navigation
Use the span fast path in Enumerable.Average with a selector - #138044
Open
ketanvijayvargiya wants to merge 1 commit into
Open
ketanvijayvargiya wants to merge 1 commit into
ketanvijayvargiya wants to merge 1 commit into
Conversation
Average(source, selector) enumerates arrays and List<T> through IEnumerable<T>. Use TryGetSpan for those, as the other span paths do. Each of the two private helpers becomes a dispatcher plus one method per loop.
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-collections |
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Average(source, selector)enumerates arrays andList<T>throughIEnumerable<T>.Count(predicate),Any,All,FirstandAggregatealready useTryGetSpanfor those; this does the same for theAverageoverloads that take a selector. Each of the two private helpers they forward to becomes a dispatcher, a span loop, and the existing loop moved into its own method without changes.Two details in the span loops:
count == 0check. I first wrote it like the existing one (a loop that finds the first value, then a loop for the rest), but fordecimal?the JIT then copied the selected value to the stack with two stores and read it back across both, and a one-item array went from 13.1 ns to 16.8 ns. With the single loop the value stays in registers and that case is 8.7 ns.Numbers
BenchmarkDotNet, each case in its own process. "Before" is
Average.csfrommain. Times in ns. The source holds reference-type items and the selector reads a field.intdoubledecimalint?double?decimal?ListWhereListWhereListWhereArrays and lists are faster in every cell: 1.4x to 6.1x for
int,doubleand their nullable forms, 1.1x to 2.0x fordecimalanddecimal?.Other sources run the same loop as before, after one extra type check. That costs 1 to 4 ns per call, which shows only on a single item; from 10 items they are level within run-to-run noise.
I do not have a full local build of the runtime. I compiled
Average.csfrommainand from this branch side by side into one console app (.NET SDK 10.0.403, x64, Windows 11). I can ask EgorBot for numbers onmainif preferred.Code size
Fully optimized code for one selector type with a reference-type source, helper before against dispatcher + span loop + enumerable loop after, in bytes:
intdoubledecimalint?double?decimal?Behavior
Results, exceptions and argument checks are unchanged. As with the other span paths, a selector that modifies the
List<T>being averaged no longer gets anInvalidOperationException.Tests
Three tests added to
AverageTests, running every selector overload over every source shape fromCreateSources: the averages (with a leading item that has no value for the nullable overloads), sources with no values, and overflow. They also pass withAverage.csfrommain.With the side-by-side app I compared the branch against
mainfor all ten overloads over an array, aList<T>and an iterator on random inputs, including empty sources, nulls, overflow, NaN, infinities and negative zero fordouble,floatanddecimal: 1,200,000 comparisons, the same bits or the same exception type every time.I compiled the System.Linq sources from this branch into a standalone .NET 10 project and ran
AverageTests.csagainst them: 298 pass. I have not run the full System.Linq suite against a built runtime locally, so I am relying on CI for that.