Repository navigation
Conversation
|
!radar |
|
Benchmark results for 8208e78 against e51752c are in. There are significant results. @kim-em Warning These warnings may indicate that the benchmark results are not directly comparable, for example due to changes in the runner configuration or hardware.
Large changes (2✅)
Medium changes (3🟥)
Small changes (3✅, 86🟥)
|
|
Mathlib CI status (docs):
|
|
Reference manual CI status:
|
|
!radar |
|
Benchmark results for 7a8781f against e51752c are in. There are significant results. @kim-em Warning These warnings may indicate that the benchmark results are not directly comparable, for example due to changes in the runner configuration or hardware.
Large changes (2✅)
Medium changes (2🟥)
Small changes (1✅, 75🟥)
|
|
Bugbot is not enabled for your account, so this pull request was not reviewed. Enable Bugbot in the Cursor dashboard to get automatic reviews on future PRs. |
|
@kim-em I have found that the three-way technique of this PR introduces performance regressions in at least two scenarios. The following are representative examples:
I've got two alternative bona fide qsort implementations that I'd like to propose as solutions to issue #8087 in a separate PR. I have run benchmarking on them and have formalized them. However, these are not the subject of this PR, I just thought I'd mention them, as they are listed in the following visualizations of the above two scenarios. See Scenario 1: See Scenario 2: Lastly, it is possible to repeat these and other experiments at |
|
Okay, but both of these problems are constant factor issues, right? So I think I'm still happier with this than the sometimes-quadratic implementation we have right now. |
|
I have to point out that vanilla quicksort has many pathological cases. Even with the current median-of-3 pivot selection, the arrays produced by def pathological (n : Nat) := Array.range' 1 n |>.push 0With existing implementation: #time #eval (pathological 1000).qsort.size -- 51ms
#time #eval (pathological 10000).qsort.size -- 3750msWith the implementation in this PR: #time #eval (pathological 1000).qsort.size -- 95ms
#time #eval (pathological 10000).qsort.size -- 4636msAs an executable:
That said, I guess we don't want to implement median of medians (which guarantees O(n log n) worst case but introduces high constant factor), and any other improvement (specifically, hybrid sorting techniques) would disqualify the function from being a "quicksort". |
|
I think I'm inclined to just close this and not attempt to make progress on sorting until someone has time to do it properly. Increasingly, I think the right answer here is: take whichever of the Java / Rust / etc algorithms has had the most serious thought into making it practically fast, translate that verbatim into Lean assuming licensing is okay, then prove it correct via AI, no matter how gross the proof has to be. |


This PR makes
Array.qsortefficiently handle inputs with repeated values by grouping pivot-equal elements and excluding them from recursive calls.The partitioner retains the low-overhead scan for distinct inputs and switches to a three-way partition once it encounters an element equal to the pivot. This fixes the quadratic behavior reported in #8087 without regressing the existing random-input benchmark.
qsortOrdnow passes its three-way comparison directly, while the Boolean-comparator API is adapted internally.It adds a regression test for repeated values and a Radar benchmark covering constant and low-cardinality arrays. The suggestion snapshots change because unstable sorting may now choose a different order among equal elements. The obsolete implementation-coupled proof test is removed; stacked follow-up #14658 replaces it with stronger proofs in the library.
Previous attempts and related groundwork:
grind-based verification to the test suite; #12510 later attempted to complete those proofs.Array.qsorthas quadratic runtime on constant arrays #8087, but its Radar runs showed a roughly 39% instruction regression incompiled/io_compute.io_computeperformance baseline.🤖 prepared with assistance from codex and claude