[Python-checkins] cpython: _pickle: Optimize raw_unicode_escape(), use directly a bytes object, don't use

victor.stinner python-checkins at python.org
Sun Aug 17 21:15:30 CEST 2014


http://hg.python.org/cpython/rev/70b9b55fd915
changeset: 92142:70b9b55fd915
user: Victor Stinner <victor.stinner at gmail.com>
date: Sun Aug 17 21:14:46 2014 +0200
summary:
 _pickle: Optimize raw_unicode_escape(), use directly a bytes object, don't use
a temporary bytearray object.
files:
 Modules/_pickle.c | 21 ++++++++++-----------
 1 files changed, 10 insertions(+), 11 deletions(-)
diff --git a/Modules/_pickle.c b/Modules/_pickle.c
--- a/Modules/_pickle.c
+++ b/Modules/_pickle.c
@@ -2050,7 +2050,7 @@
 static PyObject *
 raw_unicode_escape(PyObject *obj)
 {
- PyObject *repr, *result;
+ PyObject *repr;
 char *p;
 Py_ssize_t i, size, expandsize;
 void *data;
@@ -2069,13 +2069,14 @@
 
 if (size > PY_SSIZE_T_MAX / expandsize)
 return PyErr_NoMemory();
- repr = PyByteArray_FromStringAndSize(NULL, expandsize * size);
+ repr = PyBytes_FromStringAndSize(NULL, expandsize * size);
 if (repr == NULL)
 return NULL;
 if (size == 0)
- goto done;
-
- p = PyByteArray_AS_STRING(repr);
+ return repr;
+ assert(Py_REFCNT(repr) == 1);
+
+ p = PyBytes_AS_STRING(repr);
 for (i=0; i < size; i++) {
 Py_UCS4 ch = PyUnicode_READ(kind, data, i);
 /* Map 32-bit characters to '\Uxxxxxxxx' */
@@ -2104,12 +2105,10 @@
 else
 *p++ = (char) ch;
 }
- size = p - PyByteArray_AS_STRING(repr);
-
-done:
- result = PyBytes_FromStringAndSize(PyByteArray_AS_STRING(repr), size);
- Py_DECREF(repr);
- return result;
+ size = p - PyBytes_AS_STRING(repr);
+ if (_PyBytes_Resize(&repr, size) < 0)
+ return NULL;
+ return repr;
 }
 
 static int
-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list

AltStyle によって変換されたページ (->オリジナル) /