[Python-checkins] cpython: marshal: optimize parsing of empty Unicode strings

victor.stinner python-checkins at python.org
Fri Jun 21 19:09:33 CEST 2013


http://hg.python.org/cpython/rev/942a7061e8ad
changeset:   84245:942a7061e8ad
user:        Victor Stinner <victor.stinner at gmail.com>
date:        Fri Jun 21 19:08:06 2013 +0200
summary:
  marshal: optimize parsing of empty Unicode strings

Don't create a temporary buffer of zeroy byte nor call r_string() if the length
is zero, create directly the empty string.

files:
  Python/marshal.c |  29 +++++++++++++++++------------
  1 files changed, 17 insertions(+), 12 deletions(-)


diff --git a/Python/marshal.c b/Python/marshal.c
--- a/Python/marshal.c
+++ b/Python/marshal.c
@@ -979,20 +979,25 @@
             retval = NULL;
             break;
         }
-        buffer = PyMem_NEW(char, n);
-        if (buffer == NULL) {
-            retval = PyErr_NoMemory();
-            break;
+        if (n != 0) {
+            buffer = PyMem_NEW(char, n);
+            if (buffer == NULL) {
+                retval = PyErr_NoMemory();
+                break;
+            }
+            if (r_string(buffer, n, p) != n) {
+                PyMem_DEL(buffer);
+                PyErr_SetString(PyExc_EOFError,
+                    "EOF read where object expected");
+                retval = NULL;
+                break;
+            }
+            v = PyUnicode_DecodeUTF8(buffer, n, "surrogatepass");
+            PyMem_DEL(buffer);
         }
-        if (r_string(buffer, n, p) != n) {
-            PyMem_DEL(buffer);
-            PyErr_SetString(PyExc_EOFError,
-                "EOF read where object expected");
-            retval = NULL;
-            break;
+        else {
+            v = PyUnicode_New(0, 0);
         }
-        v = PyUnicode_DecodeUTF8(buffer, n, "surrogatepass");
-        PyMem_DEL(buffer);
         if (type == TYPE_INTERNED)
             PyUnicode_InternInPlace(&v);
         retval = v;

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list