From 88d146b7b9cece5c3e2e85feedce3417a589e8c3 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Sun, 17 Aug 2014 21:12:18 +0200 Subject: Optimize PyBytes_FromObject(): only overallocate when size=0 to not get the empty string singleton --- Objects/bytesobject.c | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Objects/bytesobject.c b/Objects/bytesobject.c index ca565eb..c5af253 100644 --- a/Objects/bytesobject.c +++ b/Objects/bytesobject.c @@ -3174,10 +3174,12 @@ PyBytes_FromObject(PyObject *x) returning a shared empty bytes string. This required because we want to call _PyBytes_Resize() the returned object, which we can only do on bytes objects with refcount == 1. */ - size += 1; + if (size == 0) + size = 1; new = PyBytes_FromStringAndSize(NULL, size); if (new == NULL) return NULL; + assert(Py_REFCNT(new) == 1); /* Get the iterator */ it = PyObject_GetIter(x); -- cgit v0.12