Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 88d146b

Browse files
committed
Optimize PyBytes_FromObject(): only overallocate when size=0 to not get the
empty string singleton
1 parent cfcde8c commit 88d146b

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

Objects/bytesobject.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3174,10 +3174,12 @@ PyBytes_FromObject(PyObject *x)
31743174
returning a shared empty bytes string. This required because we
31753175
want to call _PyBytes_Resize() the returned object, which we can
31763176
only do on bytes objects with refcount == 1. */
3177-
size += 1;
3177+
if (size == 0)
3178+
size = 1;
31783179
new = PyBytes_FromStringAndSize(NULL, size);
31793180
if (new == NULL)
31803181
return NULL;
3182+
assert(Py_REFCNT(new) == 1);
31813183

31823184
/* Get the iterator */
31833185
it = PyObject_GetIter(x);

0 commit comments

Comments
 (0)