Skip to content

[DRAFT] Use Rust's sort_by instead of timsort::try_sort_by_gt #6096

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
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
30 changes: 26 additions & 4 deletions vm/src/builtins/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,6 +504,8 @@ impl Representable for PyList {
}
}

use crate::exceptions::types::PyBaseExceptionRef;
use std::cmp::Ordering;
fn do_sort(
vm: &VirtualMachine,
values: &mut Vec<PyObjectRef>,
Expand All @@ -515,20 +517,40 @@ fn do_sort(
} else {
PyComparisonOp::Gt
};
let cmp = |a: &PyObjectRef, b: &PyObjectRef| a.rich_compare_bool(b, op, vm);

// If a PyException is encountered stop swapping elements and store the error.
let mut error_slot: Option<PyBaseExceptionRef> = None;
let mut cmp = |a: &PyObjectRef, b: &PyObjectRef| match a.rich_compare_bool(b, op, vm) {
Ok(res) => {
if res {
Ordering::Greater
} else {
Ordering::Less
}
}
Err(e) => {
error_slot = Some(e);
Ordering::Equal
}
};

if let Some(ref key_func) = key_func {
let mut items = values
.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::try_sort_by_gt(&mut items, |a, b| cmp(&a.1, &b.1))?;
items.sort_unstable_by(|a, b| cmp(&a.1, &b.1));
*values = items.into_iter().map(|(val, _)| val).collect();
} else {
timsort::try_sort_by_gt(values, cmp)?;
values.sort_unstable_by(cmp);
}

Ok(())
// After the sort is done, check if an error was captured.
match error_slot {
Some(e) => Err(e),
None => Ok(()),
}
}

#[pyclass(module = false, name = "list_iterator", traverse)]
Expand Down
Loading