-
Notifications
You must be signed in to change notification settings - Fork 219
Add new shared SelectXXXAsArray helpers #11796
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add new shared SelectXXXAsArray helpers #11796
Conversation
Add helpers that combine Select And Order/OrderBy/OrderDescending/OrderByDescending to avoid a common LINQ anti-pattern: ```C# List<int> list = [1, 2, 3, 4, 5]; var result = list.Select(x => x * 2).OrderDescendingAsArray(); ``` The code above results in extra allocations by the LINQ Select(...) method, even though the expected results in an ImmutableArray<int>. With these helpers in place, the code above can be written more efficiently as: ```C# List<int> list = [1, 2, 3, 4, 5]; var result = list.SelectAndOrderDescendingAsArray(x => x * 2); ```
LINQ provides a Select(...) overload where the selector takes an int representing the item index. This change adds a similar overload for SelectAsArray.
src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/EnumerableExtensions.cs
Outdated
Show resolved
Hide resolved
src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/EnumerableExtensions.cs
Outdated
Show resolved
Hide resolved
Use TryGetCount(...) to retrieve a capacity up front before creating a PooledArrayBuilder<T>.
The type tests for ImmutableArray<T> and IReadOnlyList<T> aren't needed in SelectAndOrderXXXAsArray helpers, since those tests are already performed by their calls to SelectAsArray.
|
@dotnet/razor-compiler: Please take a look. Much of these changes are additions to shared code. However, there are some changes in shared methods used by the compiler (e.g. |
src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/ReadOnlyListExtensions.cs
Outdated
Show resolved
Hide resolved
src/Shared/Microsoft.AspNetCore.Razor.Utilities.Shared/ReadOnlyListExtensions.cs
Outdated
Show resolved
Hide resolved
@ToddGrun pointed out that the IReadOnlyList<T>.SelectAsArray(...) extension methods really didn't need the complexity of pattern matching and a PooledArrayBuilder<T>. Because we have an IReadOnlyList<T>, we already know the length and can construct the final array up front. This commit makes that change and similarly adjusts the IReadOnlyList<T>.ToArray(), ImmutableArray<T>.SelectAsArray(), and IEnumerable<T>.SelectAsArray() extension methods. Comments have been added throughout to explain specific decisions.
| result[index++] = selector(item); | ||
| } | ||
|
|
||
| Debug.Assert(result.Length == count); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
An IndexOutOfRangeException would already be thrown in that case, since result is created with a length of count.
ToddGrun
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
![]()
This adds a handful of new SelectXXXAsArray extension methods and overloads. In particular, there are new
SelectAndOrderXXXAsArraymethods that transform elements and order them in a single pass.