Skip to content
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
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
18 changes: 10 additions & 8 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 @@ -641,14 +642,15 @@ fn do_sort(
reverse: bool,
) -> 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| {
// `timsort` expects is_lt(a, b) = true when a must be placed BEFORE b.
// For reverse=True, swapping the operands yields a descending order that is
// still stable in the original relative order, matching CPython's
// reverse-sort-reverse approach.
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 +659,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
1 change: 1 addition & 0 deletions crates/vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ pub mod scope;
pub mod sequence;
pub mod signal;
pub mod sliceable;
pub mod sorting;
pub mod stdlib;
pub mod suggestion;
pub mod types;
Expand Down
Loading
Loading