Skip to content

sort: move legacy sort! recursion into private kernels - #63228

Merged
KristofferC merged 1 commit into
masterfrom
kc/legacy-sort-internal-recursion
Sep 19, 2026
Merged

KristofferC merged 1 commit into
masterfrom
kc/legacy-sort-internal-recursion

Conversation

@KristofferC

Copy link
Copy Markdown
Member

Avoids sort! having memory-unsafe behavior while at the same time avoiding the bounds check in every recursive call (cf #63216 (comment))

Assisted-by: Claude Code (Fable 5.1)

@KristofferC KristofferC added the sorting Put things in order label Sep 16, 2026

@LilithHafner LilithHafner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

_quicksort! and _small_sort! are weirdly asymmetric.

@KristofferC

Copy link
Copy Markdown
Member Author

Could you elaborate?

@LilithHafner

Copy link
Copy Markdown
Member

The presence of the algorithm positional arg in _mergesort! but not _smal_lsort! is odd, the only assymmetry I would expect there is for partial quicksort, and even then I'd rather see full symmetry (i.e. all pass algo).

But as I look more, I think it would be better for _quicksort! and friends to call the modern API directly rather than making a new stub that does that. Eliminating the inconsistency by eliminating the function altogether.

@LilithHafner

Copy link
Copy Markdown
Member

I'm fairly neutral on calling modern everywhere vs having a single point of translation, but if we do use a single point of translation it should be consistent with the functions that serve a similar role and include the (unused) alg arg, or all of them should not and partialquicksort should pass a bare int.

The `sort!(v, lo, hi, alg, order)` methods for `QuickSort`, `MergeSort` and
`PartialQuickSort` recursed into themselves, so the `checkbounds` added in
#63216 ran on every recursive call and the public methods still contained
the `@inbounds` recursion. Split each into a public wrapper that checks
`lo:hi` once and a private recursive kernel (`_quicksort!`, `_mergesort!`,
`_partialquicksort!`) that assumes the indices are valid. The small base
case calls `_sort!` with `InsertionSort` directly instead of going back
through the checked public entry point.

Assisted-by: Claude Code (Fable 5.1)
@KristofferC
KristofferC force-pushed the kc/legacy-sort-internal-recursion branch from 27c5594 to 4807a28 Compare September 17, 2026 12:56
@KristofferC

Copy link
Copy Markdown
Member Author

Better now?

@KristofferC
KristofferC merged commit 6a1694d into master Sep 19, 2026
10 checks passed
@KristofferC
KristofferC deleted the kc/legacy-sort-internal-recursion branch September 19, 2026 09:41
@KristofferC KristofferC added the backport 1.13 Change should be backported to release-1.13 label Sep 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

backport 1.13 Change should be backported to release-1.13 sorting Put things in order

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants