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

Skip to content

Commit 7270b7f

Browse files
committed
_pickle: Optimize raw_unicode_escape(), use directly a bytes object, don't use
a temporary bytearray object.
1 parent 88d146b commit 7270b7f

1 file changed

Lines changed: 9 additions & 10 deletions

File tree

Modules/_pickle.c

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2050,7 +2050,7 @@ save_bytes(PicklerObject *self, PyObject *obj)
20502050
static PyObject *
20512051
raw_unicode_escape(PyObject *obj)
20522052
{
2053-
PyObject *repr, *result;
2053+
PyObject *repr;
20542054
char *p;
20552055
Py_ssize_t i, size, expandsize;
20562056
void *data;
@@ -2069,13 +2069,14 @@ raw_unicode_escape(PyObject *obj)
20692069

20702070
if (size > PY_SSIZE_T_MAX / expandsize)
20712071
return PyErr_NoMemory();
2072-
repr = PyByteArray_FromStringAndSize(NULL, expandsize * size);
2072+
repr = PyBytes_FromStringAndSize(NULL, expandsize * size);
20732073
if (repr == NULL)
20742074
return NULL;
20752075
if (size == 0)
2076-
goto done;
2076+
return repr;
2077+
assert(Py_REFCNT(repr) == 1);
20772078

2078-
p = PyByteArray_AS_STRING(repr);
2079+
p = PyBytes_AS_STRING(repr);
20792080
for (i=0; i < size; i++) {
20802081
Py_UCS4 ch = PyUnicode_READ(kind, data, i);
20812082
/* Map 32-bit characters to '\Uxxxxxxxx' */
@@ -2104,12 +2105,10 @@ raw_unicode_escape(PyObject *obj)
21042105
else
21052106
*p++ = (char) ch;
21062107
}
2107-
size = p - PyByteArray_AS_STRING(repr);
2108-
2109-
done:
2110-
result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr), size);
2111-
Py_DECREF(repr);
2112-
return result;
2108+
size = p - PyBytes_AS_STRING(repr);
2109+
if (_PyBytes_Resize(&repr, size) < 0)
2110+
return NULL;
2111+
return repr;
21132112
}
21142113

21152114
static int

0 commit comments

Comments
 (0)