Skip to content

API Addition: Add generic Array.Reverse<T> #14843

Closed
@justinvp

Description

@justinvp

Add generic Array.Reverse<T>.

Rationale

Array.Reverse is non-generic. It includes a fast path for a known set of primitive types via a call into the runtime (see TrySZReverse), otherwise it falls back to slower code paths that involve boxing for value types. This is a significant performance cost for non-primitive value types, ~22x slower.

Callers of Array.Reverse (like List<T>.Reverse and ImmutableArray<T>.Builder.Reverse) are affected by this performance issue. As a workaround, List<T>.Reverse and ImmutableArray<T>.Builder.Reverse are being updated to not use the non-generic Array.Reverse method until the generic Array.Reverse<T> method becomes available.

Other non-generic methods on Array have generic counterparts like Array.Sort and Array.Sort<T>, so this would just be making Reverse consistent with those.

Proposed API

public abstract class Array : ...
{
    // Proposed
    public static void Reverse<T>(T[] array);
    public static void Reverse<T>(T[] array, int index, int length);

    // Existing
    public static void Reverse(Array array);
    public static void Reverse(Array array, int index, int length);
}

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions