Skip to content
Open
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
16 changes: 11 additions & 5 deletions Lib/test/test_bytes.py
Original file line number Diff line number Diff line change
Expand Up @@ -629,14 +629,20 @@ def test_join(self):
self.assertEqual(dot_join([b"ab", memoryview(b"cd")]), b"ab.:cd")
self.assertEqual(dot_join([bytearray(b"ab"), b"cd"]), b"ab.:cd")
self.assertEqual(dot_join([b"ab", bytearray(b"cd")]), b"ab.:cd")
# Stress it with many items
seq = [b"abc"] * 100000
expected = b"abc" + b".:abc" * 99999
# Stress it with many items or many views on immutable bytes
N = 0x100000 # threshold for releasing the GIL in join()
seq = [b"abc"] * N
expected = b"abc" + b".:abc" * (N - 1)
self.assertGreater(len(expected), N)
self.assertEqual(dot_join(seq), expected)
views = list(map(memoryview, seq))
self.assertEqual(dot_join(views), expected)
# Stress test with empty separator
seq = [b"abc"] * 100000
expected = b"abc" * 100000
expected = b"abc" * N
self.assertGreater(len(expected), N)
self.assertEqual(self.type2test(b"").join(seq), expected)
views = list(map(memoryview, seq))
self.assertEqual(self.type2test(b"").join(views), expected)
self.assertRaises(TypeError, self.type2test(b" ").join, None)
# Error handling and cleanup when some item in the middle of the
# sequence has the wrong type.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Improve performance of :meth:`bytes.join` when the operands are known to be
buffer views on :class:`bytes` objects (but not subclasses thereof) by
releasing the GIL during the concatenation. Patch by Bénédikt Tran.
5 changes: 4 additions & 1 deletion Objects/stringlib/join.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ STRINGLIB(bytes_join)(PyObject *sep, PyObject *iterable)
* races anyway, but this is a conservative approach that avoids
* changing the behaviour of that data race.
*/
drop_gil = 0;
PyObject *bufobj = buffers[i].obj;
if (!bufobj || !PyBytes_CheckExact(bufobj)) {
drop_gil = 0;
}
Comment on lines +84 to +87
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
PyObject *bufobj = buffers[i].obj;
if (!bufobj || !PyBytes_CheckExact(bufobj)) {
drop_gil = 0;
}
if (drop_gil) {
PyObject *bufobj = buffers[i].obj;
if (!bufobj || !PyBytes_CheckExact(bufobj)) {
drop_gil = 0;
}
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor speedup

Copy link
Copy Markdown
Member Author

@picnixz picnixz Apr 12, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it'll matter much honestly. I'll wait for benchmarks in this case. Most of the time will be spent in memcpy() and the rest.

}
nbufs = i + 1; /* for error cleanup */
itemlen = buffers[i].len;
Expand Down
Loading