Skip to content

Block inlining of IntroSort #89310

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

Merged
merged 2 commits into from
Jul 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Array.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2207,6 +2207,9 @@ private void IntrospectiveSort(int left, int length)
}
}

// IntroSort is recursive; block it from being inlined into itself as
// this is currenly not profitable.
[MethodImpl(MethodImplOptions.NoInlining)]
Copy link
Member

Choose a reason for hiding this comment

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

Can we include a comment as to why this is here? I imagine the calculus around whether this is valuable could change in the future.

Copy link
Member Author

Choose a reason for hiding this comment

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

Added.

Note IntroSort is nominally doubly recursive but has already had the tail recursive call transformed into a loop manually. If we were ever tempted to undo that and rely on the JIT to do something similar, we might find the NoInline annotation would inhibit that transformation too.

@EgorBo we might consider special heuristics for recursion, generally if the method is large and doesn't have a frequent fast path that avoids recursion, then there's not much to be gained.

private void IntroSort(int lo, int hi, int depthLimit)
{
Debug.Assert(hi >= lo);
Expand Down Expand Up @@ -2421,6 +2424,9 @@ private void IntrospectiveSort(int left, int length)
}
}

// IntroSort is recursive; block it from being inlined into itself as
// this is currenly not profitable.
[MethodImpl(MethodImplOptions.NoInlining)]
private void IntroSort(int lo, int hi, int depthLimit)
{
Debug.Assert(hi >= lo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ internal static void IntrospectiveSort(Span<T> keys, Comparison<T> comparer)
}
}

// IntroSort is recursive; block it from being inlined into itself as
// this is currenly not profitable.
[MethodImpl(MethodImplOptions.NoInlining)]
private static void IntroSort(Span<T> keys, int depthLimit, Comparison<T> comparer)
{
Debug.Assert(!keys.IsEmpty);
Expand Down Expand Up @@ -402,6 +405,9 @@ private static void Swap(ref T i, ref T j)
j = t;
}

// IntroSort is recursive; block it from being inlined into itself as
// this is currenly not profitable.
[MethodImpl(MethodImplOptions.NoInlining)]
private static void IntroSort(Span<T> keys, int depthLimit)
{
Debug.Assert(!keys.IsEmpty);
Expand Down