-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Add ArrayBuilder.AddRange and use it instead of List.ToArray pattern #122209
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
base: main
Are you sure you want to change the base?
Conversation
…Info, and Timer to use it Co-authored-by: 333fred <[email protected]>
Co-authored-by: 333fred <[email protected]>
src/libraries/Common/tests/Tests/System/Collections/Generic/ArrayBuilderTests.cs
Show resolved
Hide resolved
Co-authored-by: 333fred <[email protected]>
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.
Pull request overview
This PR adds an AddRange method to ArrayBuilder<T> to improve performance in locations that previously used the new List<T>(enumerable).ToArray() pattern. The implementation optimizes for ICollection<T> inputs by pre-allocating capacity, while falling back to incremental growth for general IEnumerable<T> inputs.
Key changes:
- Added
AddRange(IEnumerable<T>)method to both Common and NativeAOTArrayBuilder<T>implementations withICollection<T>optimization - Refactored I/O methods in
Directory.cs,DirectoryInfo.cs, andTimer.csto useArrayBuilder<T>.AddRange()instead ofList<T>.ToArray() - Added comprehensive test coverage including tests for the ICollection optimization path, empty collections, and incremental growth
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/Common/src/System/Collections/Generic/ArrayBuilder.cs | Adds AddRange method with ICollection optimization for pre-allocating capacity |
| src/coreclr/tools/Common/System/Collections/Generic/ArrayBuilder.cs | Adds AddRange method to NativeAOT's ArrayBuilder with same optimization |
| src/libraries/System.Private.CoreLib/src/System/IO/Directory.cs | Refactors three methods (GetFiles, GetDirectories, GetFileSystemEntries) to use ArrayBuilder.AddRange instead of List.ToArray pattern |
| src/libraries/System.Private.CoreLib/src/System/IO/DirectoryInfo.cs | Refactors three methods (GetFiles, GetFileSystemInfos, GetDirectories) to use ArrayBuilder.AddRange instead of List.ToArray pattern |
| src/libraries/System.Private.CoreLib/src/System/Threading/Timer.cs | Refactors debugger proxy property to use ArrayBuilder.AddRange instead of List.ToArray pattern |
| src/libraries/Common/tests/Tests/System/Collections/Generic/ArrayBuilderTests.cs | Adds comprehensive test coverage for AddRange including ICollection optimization, empty collections, and non-ICollection paths |
AddRangemethod toArrayBuilder<T>in/src/libraries/Common/src/System/Collections/Generic/ArrayBuilder.csDirectory.csto useArrayBuilder<T>withAddRangeinstead ofnew List<T>(enumerable).ToArray()DirectoryInfo.csto useArrayBuilder<T>withAddRangeinstead ofnew List<T>(enumerable).ToArray()Timer.csto useArrayBuilder<T>withAddRangeinstead ofnew List<T>(enumerable).ToArray()ArrayBuilder<T>.AddRangemethodAddRangeto pre-allocate forICollection<T>inputsAddRangemethod to NativeAOT'sArrayBuilder<T>in/src/coreclr/tools/Common/System/Collections/Generic/ArrayBuilder.csICollection<T>optimization:AddRange_ICollection_PreallocatesCapacity- verifies capacity is pre-allocated for ICollection inputsAddRange_ICollection_EmptyCollection- verifies empty ICollection works correctlyAddRange_ICollection_AfterExistingItems- verifies ICollection optimization works when items already existAddRange_NonICollection_GrowsIncrementally- verifies non-ICollection path grows incrementallyOriginal prompt
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.