]> granicus.if.org Git - python/commitdiff
Optimize PyBytes_FromObject(): only overallocate when size=0 to not get the
authorVictor Stinner <victor.stinner@gmail.com>
Sun, 17 Aug 2014 19:12:18 +0000 (21:12 +0200)
committerVictor Stinner <victor.stinner@gmail.com>
Sun, 17 Aug 2014 19:12:18 +0000 (21:12 +0200)
empty string singleton

Objects/bytesobject.c

index ca565eb5666de86de2f1bcf60f7858259047ef21..c5af25350e97b12bfb9ac6bfb819844f82380932 100644 (file)
@@ -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);