Skip to content

Commit

Permalink
Implement locked iteration for PyList (#4789)
Browse files Browse the repository at this point in the history
* implement locked iteration for PyList

* fix limited API and PyPy support

* fix formatting of safety docstrings

* only define fold and rfold on not(feature = "nightly")

* add missing try_fold implementation on nightly

* Use split borrows for locked iteration for PyList

Inline ListIterImpl implementations by using split borrows and
destructuring let Self { .. } = self destructuring inside
BoundListIterator impls.

Signed-off-by: Manos Pitsidianakis <manos@pitsidianak.is>

* use a function to do the split borrow

* add changelog entries

* fix clippy on limited API and PyPy

* use a macro for the split borrow

* add a test that mutates the list during a fold

* enable next_unchecked on PyPy

* fix incorrect docstring for locked_for_each

* simplify borrows by adding BoundListIterator::with_critical_section

* fix build on GIL-enabled and limited API builds

* fix docs build on MSRV

---------

Signed-off-by: Manos Pitsidianakis <manos@pitsidianak.is>
Co-authored-by: Manos Pitsidianakis <manos@pitsidianak.is>
  • Loading branch information
ngoldbaum and epilys authored Jan 8, 2025
1 parent 4670203 commit c0f08c2
Show file tree
Hide file tree
Showing 3 changed files with 454 additions and 33 deletions.
3 changes: 3 additions & 0 deletions newsfragments/4789.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
* Added `PyList::locked_for_each`, which is equivalent to `PyList::for_each` on
the GIL-enabled build and uses a critical section to lock the list on the
free-threaded build, similar to `PyDict::locked_for_each`.
2 changes: 2 additions & 0 deletions newsfragments/4789.changed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Operations that process a PyList via an iterator now use a critical section
on the free-threaded build to amortize synchronization cost and prevent race conditions.
Loading

0 comments on commit c0f08c2

Please sign in to comment.