Repository navigation
Use AllocateUninitializedArray for primitive types except bool in SharedArrayPool - #121851
Merged
Merged
Conversation
Co-authored-by: stephentoub <[email protected]>
jkotas
reviewed
Nov 20, 2025
Copilot
AI
changed the title
[WIP] Switch array allocation to new T[] for ArrayPool
Replace GC.AllocateUninitializedArray with new T[] in SharedArrayPool
Nov 20, 2025
Co-authored-by: stephentoub <[email protected]>
Copilot
AI
changed the title
Replace GC.AllocateUninitializedArray with new T[] in SharedArrayPool
Use AllocateUninitializedArray for primitive types except bool in SharedArrayPool
Nov 21, 2025
stephentoub
reviewed
Nov 21, 2025
stephentoub
approved these changes
Nov 21, 2025
stephentoub
marked this pull request as ready for review
November 21, 2025 02:22
stephentoub
enabled auto-merge (squash)
November 21, 2025 02:32
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR optimizes array allocation in SharedArrayPool<T> by using uninitialized arrays for primitive types (except bool) while maintaining safety for other types. Previously, all array allocations used GC.AllocateUninitializedArray, which could produce invalid bit patterns for bool and non-primitive types.
Key Changes:
- Added conditional allocation logic that uses
GC.AllocateUninitializedArray<T>()for primitive types excluding bool, andnew T[]for bool and all other types - Improves safety by ensuring bool arrays and reference/value types are properly initialized
- Maintains performance benefits of uninitialized allocation for most primitive types
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
ArrayPool now uses a conditional allocation strategy to balance performance and safety. Primitive types (excluding bool) use uninitialized arrays for better performance, while bool and non-primitive types use initialized arrays.
Changes
SharedArrayPool<T>.Rent(): Changed to use conditional allocation (lines 110-112):GC.AllocateUninitializedArray<T>()for performancenew T[]for safetyConfigurableArrayPool<T>already usednew T[]and is unchangedThis approach optimizes performance for primitive types where any bit pattern is valid, while ensuring proper initialization for bool (which has problematic bit patterns) and all reference/value types.
Original 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.