mirror of
https://github.com/python/cpython.git
synced 2025-10-28 01:00:34 +00:00
try to use the same str object for all code filenames when compiling or unmarshalling (#12190)
This should reduce memory usage.
This commit is contained in:
parent
d408503b2c
commit
43b068648e
5 changed files with 57 additions and 18 deletions
|
|
@ -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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue