Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 20, 2025

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
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
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 finished work on behalf of stephentoub November 20, 2025 18:16
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
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
Copilot finished work on behalf of stephentoub November 21, 2025 02:20
@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
Copilot finished reviewing on behalf of stephentoub November 21, 2025 02:26
Copy link
Member

@jkotas jkotas left a comment

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
Copy link
Contributor

Copilot AI left a 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 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

@stephentoub stephentoub merged commit 73c8ac2 into main Nov 21, 2025
140 of 142 checks passed
@akoeplinger akoeplinger deleted the copilot/update-array-allocation-method branch November 21, 2025 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs-area-label An area label is needed to ensure this gets routed to the appropriate area owners

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants