https://github.com/python/cpython/commit/9f7bbafffe1e2c025bb54b20388c9eb45edaebf5
commit: 9f7bbafffe1e2c025bb54b20388c9eb45edaebf5
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2025-09-18T09:52:16Z
summary:
gh-129813, PEP 782: Optimize byteswriter_resize() (#139101)
There is no need to copy the small_buffer in PyBytesWriter_Create().
files:
M Objects/bytesobject.c
diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c
index cd314fdd5b1e14..07237ceaa647e6 100644
--- a/Objects/bytesobject.c
+++ b/Objects/bytesobject.c
@@ -3801,7 +3801,7 @@ byteswriter_allocated(PyBytesWriter *writer)
static inline int
-byteswriter_resize(PyBytesWriter *writer, Py_ssize_t size, int overallocate)
+byteswriter_resize(PyBytesWriter *writer, Py_ssize_t size, int resize)
{
assert(size >= 0);
@@ -3810,7 +3810,7 @@ byteswriter_resize(PyBytesWriter *writer, Py_ssize_t
size, int overallocate)
return 0;
}
- if (overallocate & writer->overallocate) {
+ if (resize & writer->overallocate) {
if (size <= (PY_SSIZE_T_MAX - size / OVERALLOCATE_FACTOR)) {
size += size / OVERALLOCATE_FACTOR;
}
@@ -3834,25 +3834,29 @@ byteswriter_resize(PyBytesWriter *writer, Py_ssize_t
size, int overallocate)
if (writer->obj == NULL) {
return -1;
}
- assert((size_t)size > sizeof(writer->small_buffer));
- memcpy(PyByteArray_AS_STRING(writer->obj),
- writer->small_buffer,
- sizeof(writer->small_buffer));
+ if (resize) {
+ assert((size_t)size > sizeof(writer->small_buffer));
+ memcpy(PyByteArray_AS_STRING(writer->obj),
+ writer->small_buffer,
+ sizeof(writer->small_buffer));
+ }
}
else {
writer->obj = PyBytes_FromStringAndSize(NULL, size);
if (writer->obj == NULL) {
return -1;
}
- assert((size_t)size > sizeof(writer->small_buffer));
- memcpy(PyBytes_AS_STRING(writer->obj),
- writer->small_buffer,
- sizeof(writer->small_buffer));
+ if (resize) {
+ assert((size_t)size > sizeof(writer->small_buffer));
+ memcpy(PyBytes_AS_STRING(writer->obj),
+ writer->small_buffer,
+ sizeof(writer->small_buffer));
+ }
}
#ifdef Py_DEBUG
Py_ssize_t allocated = byteswriter_allocated(writer);
- if (overallocate && allocated > old_allocated) {
+ if (resize && allocated > old_allocated) {
memset(byteswriter_data(writer) + old_allocated, 0xff,
allocated - old_allocated);
}
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]