Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Use powersort for list.sort() and drop rust-timsort
Wire list sorting through crate::sorting::timsort and remove the
rust-timsort dependency. Fixes the O(N^2) behavior on random input
(1M random floats: ~16min -> ~0.8s), now within ~4x of CPython.
  • Loading branch information
kangdora committed Jul 31, 2026
commit 5b52036263f9d6a5b9b6c22d01216ecd5eb4b732
7 changes: 0 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,6 @@ textwrap = { version = "0.16.2", default-features = false }
termios = "0.3.3"
thiserror = "2.0"
thin-vec = "0.2.14"
timsort = "0.1.2"
tk-sys = { git = "https://github.com/arihant2math/tkinter.git", tag = "v0.2.0" }
icu_casemap = "2"
icu_locale = "2"
Expand Down
1 change: 0 additions & 1 deletion crates/vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ half = { workspace = true }
psm = { workspace = true }
optional = { workspace = true }
result-like = { workspace = true }
timsort = { workspace = true }

[target.'cfg(unix)'.dependencies]
exitcode = { workspace = true }
Expand Down
13 changes: 6 additions & 7 deletions crates/vm/src/builtins/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crate::{
recursion::ReprGuard,
sequence::{MutObjectSequenceOp, OptionalRangeArgs, SequenceExt, SequenceMutExt},
sliceable::{SequenceIndex, SliceableSequenceMutOp, SliceableSequenceOp},
sorting::timsort,
types::{
AsMapping, AsSequence, Comparable, Constructor, Initializer, IterNext, Iterable,
PyComparisonOp, Representable, SelfIter,
Expand Down Expand Up @@ -642,13 +643,11 @@ fn do_sort(
) -> PyResult<()> {
// CPython uses __lt__ for all comparisons in sort.
// try_sort_by_gt expects is_gt(a, b) = true when a should come AFTER b.
let cmp = |a: &PyObjectRef, b: &PyObjectRef| {
let mut is_lt = |a: &PyObjectRef, b: &PyObjectRef| {
if reverse {
// Descending: a comes after b when a < b
a.rich_compare_bool(b, PyComparisonOp::Lt, vm)
} else {
// Ascending: a comes after b when b < a
b.rich_compare_bool(a, PyComparisonOp::Lt, vm)
} else {
a.rich_compare_bool(b, PyComparisonOp::Lt, vm)
}
};
Comment thread
coderabbitai[bot] marked this conversation as resolved.

Expand All @@ -657,10 +656,10 @@ fn do_sort(
.iter()
.map(|x| Ok((x.clone(), key_func.call((x.clone(),), vm)?)))
.collect::<Result<Vec<_>, _>>()?;
timsort::try_sort_by_gt(&mut items, |a, b| cmp(&a.1, &b.1))?;
timsort(&mut items, &mut |a, b| is_lt(&a.1, &b.1))?;
*values = items.into_iter().map(|(val, _)| val).collect();
} else {
timsort::try_sort_by_gt(values, cmp)?;
timsort(values, &mut is_lt)?;
}

Ok(())
Expand Down