[Python-checkins] gh-95756: Lazily created cached co_* attrs (GH-97791)

Fidget-Spinner webhook-mailer at python.org
Mon Oct 10 23:26:21 EDT 2022


https://github.com/python/cpython/commit/b399115ef18945f7526492225d72a512048ad20d
commit: b399115ef18945f7526492225d72a512048ad20d
branch: main
author: Ken Jin <kenjin at python.org>
committer: Fidget-Spinner <kenjin at python.org>
date: 2022-10-11T11:26:08+08:00
summary:

gh-95756: Lazily created cached co_* attrs (GH-97791)

files:
A Misc/NEWS.d/next/C API/2022-10-03-20-33-24.gh-issue-95756.SSmXlG.rst
M Include/cpython/code.h
M Objects/codeobject.c
M Objects/frameobject.c
M Tools/scripts/deepfreeze.py

diff --git a/Include/cpython/code.h b/Include/cpython/code.h
index 7ce69022557a..893ff934d3d8 100644
--- a/Include/cpython/code.h
+++ b/Include/cpython/code.h
@@ -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
index 000000000000..b5c78c16e86d
--- /dev/null
+++ b/Misc/NEWS.d/next/C API/2022-10-03-20-33-24.gh-issue-95756.SSmXlG.rst	
@@ -0,0 +1 @@
+Lazily create and cache ``co_`` attributes for better performance for code getters.
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index 14d1d00684ae..7a0080c08c7b 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -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);
diff --git a/Objects/frameobject.c b/Objects/frameobject.c
index 6a51a946ef35..bd1608e0d75a 100644
--- a/Objects/frameobject.c
+++ b/Objects/frameobject.c
@@ -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);
     }
 }
 
diff --git a/Tools/scripts/deepfreeze.py b/Tools/scripts/deepfreeze.py
index d9c6030fc17c..28ac2b12f92f 100644
--- a/Tools/scripts/deepfreeze.py
+++ b/Tools/scripts/deepfreeze.py
@@ -276,7 +276,7 @@ def generate_code(self, name: str, code: types.CodeType) -> str:
             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]):



More information about the Python-checkins mailing list