try to use the same str object for all code filenames when compiling or unmarshalling (#12190)
This should reduce memory usage.
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 @@
int depth;
/* If fp == NULL, the following are valid: */
PyObject *str;
+ PyObject *current_filename;
char *ptr;
char *end;
int version;
@@ -976,6 +977,18 @@
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 @@
RFILE rf;
assert(fp);
rf.fp = fp;
+ rf.current_filename = NULL;
rf.end = rf.ptr = NULL;
return r_short(&rf);
}
@@ -1046,6 +1060,7 @@
{
RFILE rf;
rf.fp = fp;
+ rf.current_filename = NULL;
rf.ptr = rf.end = NULL;
return r_long(&rf);
}
@@ -1106,6 +1121,7 @@
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 @@
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 @@
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 @@
s = p.buf;
n = p.len;
rf.fp = NULL;
+ rf.current_filename = NULL;
rf.ptr = s;
rf.end = s + n;
rf.depth = 0;