Skip to content

ArrayPool.Shared.Rent should support returning a zero-inited array #24416

Description

@GrabYourPitchforks

Currently calling ArrayPool<T>.Shared.Rent does not guarantee that the returned buffer is zero-inited. While good for performance, this could cause correctness issues in applications, especially those that simply use byte[] xyz = ArrayPool<byte>.Shared.Rent(...) as a drop-in replacement for byte[] xyz = new byte[...].

I propose changing the signature of ArrayPool.Rent(int minimumLength) to ArrayPool.Rent(int minimumLength, bool clearArray = true). Thus the default behavior is to zero-init the returned array, and consumers who want the highest possible performance and don't care about the initial values can pass false for this parameter. (I know we shouldn't default optional arguments to anything other than default(T). Haven't fully reconciled this API design mentally yet.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Buffersdesign-discussionOngoing discussion about design without consensus

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions