Skip to content

Use AllocateUninitializedArray for primitive types except bool in SharedArrayPool - #121851

Merged
stephentoub merged 5 commits into
mainfrom
copilot/update-array-allocation-method
Nov 21, 2025
Merged

stephentoub merged 5 commits into
mainfrom
copilot/update-array-allocation-method

Conversation

Copilot AI commented Nov 20, 2025 •

Copy link
Copy Markdown
Contributor

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):
    • Primitive types (excluding bool): Uses GC.AllocateUninitializedArray<T>() for performance
    • bool and non-primitive types: Uses new T[] for safety
  • ConfigurableArrayPool<T> already used new T[] and is unchanged

This 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

ArrayPool currently uses GetUninitializedArray. This makes some array allocations faster, but arrays should only be allocated rarely. Please switch it to just using new T[].


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

@github-actions github-actions Bot added the needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners label 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
Copilot AI requested a review from stephentoub November 20, 2025 18:16
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
stephentoub marked this pull request as ready for review November 21, 2025 02:22
Copilot AI review requested due to automatic review settings November 21, 2025 02:22

@jkotas jkotas left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks

@stephentoub
stephentoub enabled auto-merge (squash) November 21, 2025 02:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

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 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, and new 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

@github-actions github-actions Bot locked and limited conversation to collaborators Dec 22, 2025
@EgorBo EgorBo added area-System.Runtime reduce-unsafe and removed needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners labels Jan 5, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants