summaryrefslogtreecommitdiffstats
path: root/Python/marshal.c
diff options
context:
space:
mode:
authorBenjamin Peterson <benjamin@python.org>2011-05-27 14:08:01 (GMT)
committerBenjamin Peterson <benjamin@python.org>2011-05-27 14:08:01 (GMT)
commit43b068648e81d951f78d9f1cf1cd8f42731e164e (patch)
tree0273084c3e9fb5d29656265d0c59ccbccb73a99d /Python/marshal.c
parentd408503b2c74b53f734adab6cd35866be460ce5e (diff)
downloadcpython-43b068648e81d951f78d9f1cf1cd8f42731e164e.zip
cpython-43b068648e81d951f78d9f1cf1cd8f42731e164e.tar.gz
cpython-43b068648e81d951f78d9f1cf1cd8f42731e164e.tar.bz2
try to use the same str object for all code filenames when compiling or unmarshalling (#12190)
This should reduce memory usage.
Diffstat (limited to 'Python/marshal.c')
-rw-r--r--Python/marshal.c19
1 files changed, 19 insertions, 0 deletions
diff --git a/Python/marshal.c b/Python/marshal.c
index f66b765..7b327ad 100644
--- a/Python/marshal.c
+++ b/Python/marshal.c
@@ -58,6 +58,7 @@ typedef struct {
int depth;
/* If fp == NULL, the following are valid: */
PyObject *str;
+ PyObject *current_filename;
char *ptr;
char *end;
int version;
@@ -976,6 +977,18 @@ r_object(RFILE *p)
filename = r_object(p);
if (filename == NULL)
goto code_error;
+ if (PyUnicode_CheckExact(filename)) {
+ if (p->current_filename != NULL) {
+ if (!PyUnicode_Compare(filename, p->current_filename)) {
+ Py_DECREF(filename);
+ Py_INCREF(p->current_filename);
+ filename = p->current_filename;
+ }
+ }
+ else {
+ p->current_filename = filename;
+ }
+ }
name = r_object(p);
if (name == NULL)
goto code_error;
@@ -1037,6 +1050,7 @@ PyMarshal_ReadShortFromFile(FILE *fp)
RFILE rf;
assert(fp);
rf.fp = fp;
+ rf.current_filename = NULL;
rf.end = rf.ptr = NULL;
return r_short(&rf);
}
@@ -1046,6 +1060,7 @@ PyMarshal_ReadLongFromFile(FILE *fp)
{
RFILE rf;
rf.fp = fp;
+ rf.current_filename = NULL;
rf.ptr = rf.end = NULL;
return r_long(&rf);
}
@@ -1106,6 +1121,7 @@ PyMarshal_ReadObjectFromFile(FILE *fp)
RFILE rf;
PyObject *result;
rf.fp = fp;
+ rf.current_filename = NULL;
rf.depth = 0;
rf.ptr = rf.end = NULL;
result = r_object(&rf);
@@ -1118,6 +1134,7 @@ PyMarshal_ReadObjectFromString(char *str, Py_ssize_t len)
RFILE rf;
PyObject *result;
rf.fp = NULL;
+ rf.current_filename = NULL;
rf.ptr = str;
rf.end = str + len;
rf.depth = 0;
@@ -1214,6 +1231,7 @@ marshal_load(PyObject *self, PyObject *f)
if (data == NULL)
return NULL;
rf.fp = NULL;
+ rf.current_filename = NULL;
if (PyBytes_Check(data)) {
rf.ptr = PyBytes_AS_STRING(data);
rf.end = rf.ptr + PyBytes_GET_SIZE(data);
@@ -1282,6 +1300,7 @@ marshal_loads(PyObject *self, PyObject *args)
s = p.buf;
n = p.len;
rf.fp = NULL;
+ rf.current_filename = NULL;
rf.ptr = s;
rf.end = s + n;
rf.depth = 0;