]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-95756: Lazily created cached co_* attrs (GH-97791)
authorKen Jin <kenjin@python.org>
Tue, 11 Oct 2022 03:26:08 +0000 (20:26 -0700)
committerGitHub <noreply@github.com>
Tue, 11 Oct 2022 03:26:08 +0000 (11:26 +0800)
Include/cpython/code.h
Misc/NEWS.d/next/C API/2022-10-03-20-33-24.gh-issue-95756.SSmXlG.rst [new file with mode: 0644]
Objects/codeobject.c
Objects/frameobject.c
Tools/scripts/deepfreeze.py

index 7ce69022557af07544be6a9c88dcc2989efa9546..893ff934d3d87921f2b6ae83f44e38ddfc245060 100644 (file)
@@ -32,6 +32,13 @@ typedef uint16_t _Py_CODEUNIT;
 #define _Py_SET_OPCODE(word, opcode) \
     do { ((unsigned char *)&(word))[0] = (opcode); } while (0)
 
+typedef struct {
+    PyObject *_co_code;
+    PyObject *_co_varnames;
+    PyObject *_co_cellvars;
+    PyObject *_co_freevars;
+} _PyCoCached;
+
 // To avoid repeating ourselves in deepfreeze.py, all PyCodeObject members are
 // defined in this macro:
 #define _PyCode_DEF(SIZE) {                                                    \
@@ -90,7 +97,7 @@ typedef uint16_t _Py_CODEUNIT;
     PyObject *co_qualname;        /* unicode (qualname, for reference) */      \
     PyObject *co_linetable;       /* bytes object that holds location info */  \
     PyObject *co_weakreflist;     /* to support weakrefs to code objects */    \
-    PyObject *_co_code;           /* cached co_code object/attribute */        \
+    _PyCoCached *_co_cached;      /* cached co_* attributes */                 \
     int _co_firsttraceable;       /* index of first traceable instruction */   \
     char *_co_linearray;          /* array of line offsets */                  \
     /* Scratch space for extra data relating to the code object.               \
diff --git a/Misc/NEWS.d/next/C API/2022-10-03-20-33-24.gh-issue-95756.SSmXlG.rst b/Misc/NEWS.d/next/C API/2022-10-03-20-33-24.gh-issue-95756.SSmXlG.rst
new file mode 100644 (file)
index 0000000..b5c78c1
--- /dev/null
@@ -0,0 +1 @@
+Lazily create and cache ``co_`` attributes for better performance for code getters.
index 14d1d00684aedfddea59c2256b8786c04d901427..7a0080c08c7b54732c2c85de523e39ea954352ed 100644 (file)
@@ -150,7 +150,22 @@ validate_and_copy_tuple(PyObject *tup)
     return newtuple;
 }
 
+static int
+init_co_cached(PyCodeObject *self) {
+    if (self->_co_cached == NULL) {
+        self->_co_cached = PyMem_New(_PyCoCached, 1);
+        if (self->_co_cached == NULL) {
+            PyErr_NoMemory();
+            return -1;
+        }
+        self->_co_cached->_co_code = NULL;
+        self->_co_cached->_co_cellvars = NULL;
+        self->_co_cached->_co_freevars = NULL;
+        self->_co_cached->_co_varnames = NULL;
+    }
+    return 0;
 
+}
 /******************
  * _PyCode_New()
  ******************/
@@ -336,7 +351,7 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
     /* not set */
     co->co_weakreflist = NULL;
     co->co_extra = NULL;
-    co->_co_code = NULL;
+    co->_co_cached = NULL;
 
     co->co_warmup = QUICKENING_INITIAL_WARMUP_VALUE;
     co->_co_linearray_entry_size = 0;
@@ -1384,10 +1399,31 @@ _PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
  * other PyCodeObject accessor functions
  ******************/
 
+static PyObject *
+get_cached_locals(PyCodeObject *co, PyObject **cached_field,
+    _PyLocals_Kind kind, int num)
+{
+    assert(cached_field != NULL);
+    assert(co->_co_cached != NULL);
+    if (*cached_field != NULL) {
+        return Py_NewRef(*cached_field);
+    }
+    assert(*cached_field == NULL);
+    PyObject *varnames = get_localsplus_names(co, kind, num);
+    if (varnames == NULL) {
+        return NULL;
+    }
+    *cached_field = Py_NewRef(varnames);
+    return varnames;
+}
+
 PyObject *
 _PyCode_GetVarnames(PyCodeObject *co)
 {
-    return get_localsplus_names(co, CO_FAST_LOCAL, co->co_nlocals);
+    if (init_co_cached(co)) {
+        return NULL;
+    }
+    return get_cached_locals(co, &co->_co_cached->_co_varnames, CO_FAST_LOCAL, co->co_nlocals);
 }
 
 PyObject *
@@ -1399,7 +1435,10 @@ PyCode_GetVarnames(PyCodeObject *code)
 PyObject *
 _PyCode_GetCellvars(PyCodeObject *co)
 {
-    return get_localsplus_names(co, CO_FAST_CELL, co->co_ncellvars);
+    if (init_co_cached(co)) {
+        return NULL;
+    }
+    return get_cached_locals(co, &co->_co_cached->_co_cellvars, CO_FAST_CELL, co->co_ncellvars);
 }
 
 PyObject *
@@ -1411,7 +1450,10 @@ PyCode_GetCellvars(PyCodeObject *code)
 PyObject *
 _PyCode_GetFreevars(PyCodeObject *co)
 {
-    return get_localsplus_names(co, CO_FAST_FREE, co->co_nfreevars);
+    if (init_co_cached(co)) {
+        return NULL;
+    }
+    return get_cached_locals(co, &co->_co_cached->_co_freevars, CO_FAST_FREE, co->co_nfreevars);
 }
 
 PyObject *
@@ -1437,8 +1479,11 @@ deopt_code(_Py_CODEUNIT *instructions, Py_ssize_t len)
 PyObject *
 _PyCode_GetCode(PyCodeObject *co)
 {
-    if (co->_co_code != NULL) {
-        return Py_NewRef(co->_co_code);
+    if (init_co_cached(co)) {
+        return NULL;
+    }
+    if (co->_co_cached->_co_code != NULL) {
+        return Py_NewRef(co->_co_cached->_co_code);
     }
     PyObject *code = PyBytes_FromStringAndSize((const char *)_PyCode_CODE(co),
                                                _PyCode_NBYTES(co));
@@ -1446,8 +1491,8 @@ _PyCode_GetCode(PyCodeObject *co)
         return NULL;
     }
     deopt_code((_Py_CODEUNIT *)PyBytes_AS_STRING(code), Py_SIZE(co));
-    assert(co->_co_code == NULL);
-    co->_co_code = Py_NewRef(code);
+    assert(co->_co_cached->_co_code == NULL);
+    co->_co_cached->_co_code = Py_NewRef(code);
     return code;
 }
 
@@ -1606,7 +1651,13 @@ code_dealloc(PyCodeObject *co)
     Py_XDECREF(co->co_qualname);
     Py_XDECREF(co->co_linetable);
     Py_XDECREF(co->co_exceptiontable);
-    Py_XDECREF(co->_co_code);
+    if (co->_co_cached != NULL) {
+        Py_XDECREF(co->_co_cached->_co_code);
+        Py_XDECREF(co->_co_cached->_co_cellvars);
+        Py_XDECREF(co->_co_cached->_co_freevars);
+        Py_XDECREF(co->_co_cached->_co_varnames);
+        PyMem_Free(co->_co_cached);
+    }
     if (co->co_weakreflist != NULL) {
         PyObject_ClearWeakRefs((PyObject*)co);
     }
@@ -2181,7 +2232,13 @@ _PyStaticCode_Dealloc(PyCodeObject *co)
     deopt_code(_PyCode_CODE(co), Py_SIZE(co));
     co->co_warmup = QUICKENING_INITIAL_WARMUP_VALUE;
     PyMem_Free(co->co_extra);
-    Py_CLEAR(co->_co_code);
+    if (co->_co_cached != NULL) {
+        Py_CLEAR(co->_co_cached->_co_code);
+        Py_CLEAR(co->_co_cached->_co_cellvars);
+        Py_CLEAR(co->_co_cached->_co_freevars);
+        Py_CLEAR(co->_co_cached->_co_varnames);
+        PyMem_Free(co->_co_cached);
+    }
     co->co_extra = NULL;
     if (co->co_weakreflist != NULL) {
         PyObject_ClearWeakRefs((PyObject *)co);
index 6a51a946ef35f0d21a5f9c7367e32cfa4e2f95c3..bd1608e0d75a6e3e5e92d37d1bd5fedfcb55ff73 100644 (file)
@@ -645,9 +645,12 @@ add_load_fast_null_checks(PyCodeObject *co)
         }
         i += _PyOpcode_Caches[_PyOpcode_Deopt[opcode]];
     }
-    if (changed) {
+    if (changed && co->_co_cached != NULL) {
         // invalidate cached co_code object
-        Py_CLEAR(co->_co_code);
+        Py_CLEAR(co->_co_cached->_co_code);
+        Py_CLEAR(co->_co_cached->_co_cellvars);
+        Py_CLEAR(co->_co_cached->_co_freevars);
+        Py_CLEAR(co->_co_cached->_co_varnames);
     }
 }
 
index d9c6030fc17c0700feaa19c5ba6105c0f5fadc01..28ac2b12f92fa78945059ee9cf6fca093888b042 100644 (file)
@@ -276,7 +276,7 @@ class Printer:
             self.write(f".co_name = {co_name},")
             self.write(f".co_qualname = {co_qualname},")
             self.write(f".co_linetable = {co_linetable},")
-            self.write(f"._co_code = NULL,")
+            self.write(f"._co_cached = NULL,")
             self.write("._co_linearray = NULL,")
             self.write(f".co_code_adaptive = {co_code_adaptive},")
             for i, op in enumerate(code.co_code[::2]):