Skip to content

Commit

Permalink
Optimize nth and nth_back for BoundListIterator (#4810)
Browse files Browse the repository at this point in the history
* Optimize nth and nth_back for BoundListIterator. Add unit test and benchmarks

* Fix fmt and newsfragment CI

* Fix clippy and changelog CI

* Revise Impl of nth and nth_back. Impl advance_by

* Fix failing fmt

* Fix failing ruff test

* branch out nth, nth_unchecked, nth_back, nth_back_unchecked.

* Fix fmt

* Revise advance_by impl. add advance_by unittest.

* Fix fmt

* Fix clippy unused function warning

* Set appropriate Py_LIMITED_API flag

* Rewrite nth & nth_back using conditional compilation. Rearrange flags for proper compilation

* fix fmt

* fix failing CI

* Impl advance_back_by. Remove cfg flag for with_critical_section

* refactor advance_by and advance_back_by. Add back cfg for with_critical_section

* Put allow deadcode for with_critical_section

* Remove use of get_item. Revise changelog
  • Loading branch information
Owen-CH-Leung authored Feb 7, 2025
1 parent 18e8b16 commit 903afcd
Show file tree
Hide file tree
Showing 4 changed files with 313 additions and 8 deletions.
1 change: 1 addition & 0 deletions newsfragments/4810.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Optimizes `nth`, `nth_back`, `advance_by` and `advance_back_by` for `BoundListIterator`
30 changes: 29 additions & 1 deletion pyo3-benches/benches/bench_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,33 @@ fn list_get_item(b: &mut Bencher<'_>) {
});
}

#[cfg(not(any(Py_LIMITED_API, Py_GIL_DISABLED)))]
fn list_nth(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 50;
let list = PyList::new_bound(py, 0..LEN);
let mut sum = 0;
b.iter(|| {
for i in 0..LEN {
sum += list.iter().nth(i).unwrap().extract::<usize>().unwrap();
}
});
});
}

fn list_nth_back(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 50;
let list = PyList::new_bound(py, 0..LEN);
let mut sum = 0;
b.iter(|| {
for i in 0..LEN {
sum += list.iter().nth_back(i).unwrap().extract::<usize>().unwrap();
}
});
});
}

#[cfg(not(Py_LIMITED_API))]
fn list_get_item_unchecked(b: &mut Bencher<'_>) {
Python::with_gil(|py| {
const LEN: usize = 50_000;
Expand All @@ -66,6 +92,8 @@ fn sequence_from_list(b: &mut Bencher<'_>) {
fn criterion_benchmark(c: &mut Criterion) {
c.bench_function("iter_list", iter_list);
c.bench_function("list_new", list_new);
c.bench_function("list_nth", list_nth);
c.bench_function("list_nth_back", list_nth_back);
c.bench_function("list_get_item", list_get_item);
#[cfg(not(any(Py_LIMITED_API, Py_GIL_DISABLED)))]
c.bench_function("list_get_item_unchecked", list_get_item_unchecked);
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![warn(missing_docs)]
#![cfg_attr(
feature = "nightly",
feature(auto_traits, negative_impls, try_trait_v2)
feature(auto_traits, negative_impls, try_trait_v2, iter_advance_by)
)]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
// Deny some lints in doctests.
Expand Down
Loading

0 comments on commit 903afcd

Please sign in to comment.