Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
justinvp opened this issue Jul 14, 2015 · 10 comments
Closed

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

justinvp opened this issue Jul 14, 2015 · 10 comments
Assignees
Labels
api-approved API was approved in API review, it can be implemented area-System.Runtime
Milestone

Comments

@justinvp
Copy link
Contributor

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);
}
@justinvp
Copy link
Contributor Author

cc: @terrajobst

@whoisj
Copy link

whoisj commented Jul 14, 2015

I like it. :shipit:

@JonHanna
Copy link
Contributor

How fast a path is the primitive fast-path?
Is it fast enough to make if (TrySZReverse(array, index, length)) return; worth adding to the generic form?
Is it fast enough to consider even adding (or coercing) other blittable types to work with it? I'm imagining it should work with value types that don't have fields that aren't either primitive or also such value types.

@JonHanna
Copy link
Contributor

Actually, that should be if (default(T) != null && TrySZReverse(array, index, length)) return; so the jitter can consider all reference type attempts to be dead code and not jit the path that tries it for any of them.

@mikedn
Copy link
Contributor

mikedn commented Jul 15, 2015

@hackcraft

How fast a path is the primitive fast-path?

Not very fast, I did a quick test in the related CoreCLR PR. And for very small arrays TrySZReverse will always loose due to the cost of another call and dispatching to the correct native implementation.

@KrzysztofCwalina
Copy link
Member

That would be a very nice addition. It's also a very simple API, and so I marked it "ready for API review" where we can discuss how to make this happen (mainly logistically).

@KrzysztofCwalina
Copy link
Member

Assigned to @terrajobst to take it though the API review process.

@joshfree joshfree assigned ianhays and unassigned terrajobst Dec 2, 2015
@terrajobst
Copy link
Contributor

I like the APIs as well.

@weshaggard, what are your thoughts? If there is no more pushback I'd mark it as api-approved.

@weshaggard
Copy link
Member

I like the proposed API.

@justinvp
Copy link
Contributor Author

dotnet/coreclr#7132
dotnet/corert#1842

This issue can now be tagged as "api-needs-exposed"

@msftgits msftgits transferred this issue from dotnet/corefx Jan 31, 2020
@msftgits msftgits added this to the 2.0.0 milestone Jan 31, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Jan 5, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
api-approved API was approved in API review, it can be implemented area-System.Runtime
Projects
None yet
Development

No branches or pull requests

9 participants