Repository navigation
libc/stdlib: recurse into the smaller qsort() partition - #7652
hyunjongkim123 wants to merge 1 commit into
Conversation
|
Could you link the reference you mentioned in commit? |
18f765c to
abe6073
Compare
The reference I can point to directly is the upstream FreeBSD qsort.c I also cited Hoare 1962 / Sedgewick 1978 as the textbook source in the |
There was a problem hiding this comment.
Could you update hrere?
There was a problem hiding this comment.
Updated the description.
abe6073 to
1bfc6fa
Compare
qsort() always recursed into the left partition and iterated over the right one, so the recursion depth depends on how the pivots fall and can approach nmemb / 2 in the worst case. Compare the two partitions, recurse into the smaller one and loop on the larger one instead. Each recursion then handles at most half of the remaining elements, so the depth is bounded by log2(nmemb) for any input order and the stack qsort() needs stays at a few hundred bytes. This is the form the upstream FreeBSD qsort.c, which this file is derived from, uses today: https://github.com/freebsd/freebsd-src/blob/main/lib/libc/stdlib/qsort.c The number of comparisons and swaps is unchanged. Verified against glibc qsort() on random, sorted, reversed, alternating and a 100k-element adversarial permutation. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
1bfc6fa to
3bee4cf
Compare
🤖 AI Code Review ·
|
mangeshraut712
left a comment
There was a problem hiding this comment.
Reviewed against the merge-base (f1a9dc97); the branch is 17 commits behind master but the change is a single self-contained file. Not an approval — that's for a maintainer.
The claim holds, and it's dramatic under an adversarial comparator
I built lib/libc/stdlib/lib_qsort.c from master and from this branch on the host, instrumented the recursive call with a depth counter, and drove both with a McIlroy-style median-of-3 killer (values are "gas" until chosen as a pivot, then frozen so each split is 1 | n−1):
n master depth PR depth log2(n)
1,000 217 5 10.0
4,000 875 6 12.0
16,000 3,419 7 14.0
64,000 13,613 9 16.0
256,000 STACK OVERFLOW 11 18.0
On master the depth grows linearly (about 0.21·n with my killer) and the process dies with a segfault at 256k elements. On this branch it grows logarithmically and tops out at 11. That's exactly the bound you describe.
One precision note on the wording: the description says the old depth "can approach nmemb / 2". I measured about 0.21·n rather than 0.5·n — my killer isn't optimal, so the constant differs, but the order of growth (linear, unbounded) is the same and that's what matters for the stack. "Can grow linearly with nmemb" would be the safest phrasing.
No behaviour change on ordinary inputs
On 200k elements across five shapes — sorted, reverse-sorted, all-equal, organ-pipe, random — both versions sort correctly and produce byte-identical output to each other and to libc qsort. Random-input depth drops from 21 (master) to 11 (this branch) as a bonus, though master is already shallow on all five (the med3 pivot does its job there), so the real value of this change is entirely in the adversarial case above.
The diff itself
rl = pb - pa/rr = pd - pcare the left and right partition byte sizes; recursing on the smaller andgoto loopon the larger is the standard form.- Right-partition base is
pn - rr, left keepsbase— both correct, and consistent with the oldpn - r. - Both
rl > size/rr > sizeguards are preserved in each arm, so 0- and 1-element partitions are skipped exactly as before.
One nit, not a blocker
The old comment on the right-hand branch ("Iterate rather than recurse to save stack space") is gone, which is right — it was only half true. The new block comment explains the invariant well.
Nice fix; this closes a genuine stack-exhaustion vector in a libc primitive.
qsort() always recursed into the left partition and iterated over the right one, so the recursion depth depends on how the pivots fall and can approach nmemb / 2 in the worst case.
Compare the two partitions, recurse into the smaller one and loop on the larger one instead. Each recursion then handles at most half of the remaining elements, so the depth is bounded by log2(nmemb) for any input order and the stack qsort() needs stays at a few hundred bytes. This is the form the upstream FreeBSD qsort.c.