Repository navigation
gh-158803: Borrow bytes items in bytes.join() while the list is locked - #159096
Open
christianaurichzm wants to merge 1 commit into
Open
christianaurichzm wants to merge 1 commit into
christianaurichzm wants to merge 1 commit into
Conversation
… locked bytes.join() and bytearray.join() took a new reference to every exact bytes item. In the free-threaded build that is an atomic operation on objects shared between threads, and since pythonGH-158910 it runs while the list's lock is held. The critical section keeps the items alive, so borrow them, as _PyUnicode_JoinArray() does. References are taken only before something can suspend the critical section: PyObject_GetBuffer() on an item that is not bytes, or releasing the thread state to copy a large result.
Contributor
|
@christianaurichzm We can do better by not using the Due to some binary layout changes the uncommon case becomes 10% slower. I was still investigating whether we can resolve that. (if not, I think the PR as is works also). (feel free to take the branch) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Follow-up to #158910 (see #158910 (comment)).
join() increfs every bytes item, and on the free-threaded build that's an atomic op when the items are shared between threads. Since the list is now locked for the whole join, the items can be borrowed instead, like
str.join()does. They only need a real reference before the critical section can be suspended: inPyObject_GetBuffer()for non-bytes items, and beforePyEval_SaveThread()for results of 1 MiB or more.Release builds,
b",".join(seq)with 8-byte items, median of 5 runs:For reference, the 8-thread case was 1.10 s before #158910. A list where half the items are bytearrays is ~3% slower on both builds.
The existing
__buffer__mutation test used an immortalb'a', so it couldn't catch a freed item; it now usesbytes(2). The new FT test covers the 1 MiB path. Each test fails if its incref is removed.-R 3:3passes on debug default and FT builds, and TSan is clean for test_free_threading, test_bytes and test_capi.test_bytes.