Skip to content

gh-95756: Lazily created cached co_* attrs #97791

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Oct 11, 2022
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Doc/c-api/code.rst
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ bound into a function.
.. c:function:: PyObject* PyCode_GetVarnames(PyCodeObject *co)

Equivalent to the Python code ``getattr(co, 'co_varnames')``.
Returns a new reference to a :c:type:`PyTupleObject` containing the names of
Returns a strong reference to a :c:type:`PyTupleObject` containing the names of
the local variables. On error, ``NULL`` is returned and an exception
is raised.

Expand All @@ -102,7 +102,7 @@ bound into a function.
.. c:function:: PyObject* PyCode_GetCellvars(PyCodeObject *co)

Equivalent to the Python code ``getattr(co, 'co_cellvars')``.
Returns a new reference to a :c:type:`PyTupleObject` containing the names of
Returns a strong reference to a :c:type:`PyTupleObject` containing the names of
the local variables that are referenced by nested functions. On error, ``NULL``
is returned and an exception is raised.

Expand All @@ -111,7 +111,7 @@ bound into a function.
.. c:function:: PyObject* PyCode_GetFreevars(PyCodeObject *co)

Equivalent to the Python code ``getattr(co, 'co_freevars')``.
Returns a new reference to a :c:type:`PyTupleObject` containing the names of
Returns a strong reference to a :c:type:`PyTupleObject` containing the names of
the free variables. On error, ``NULL`` is returned and an exception is raised.

.. versionadded:: 3.11
9 changes: 8 additions & 1 deletion Include/cpython/code.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) { \
Expand Down Expand Up @@ -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. \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Lazily create and cache ``co_*`` attributes for better performance for code getters.
81 changes: 71 additions & 10 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -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()
******************/
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -1371,7 +1386,20 @@ _PyCode_SetExtra(PyObject *code, Py_ssize_t index, void *extra)
PyObject *
_PyCode_GetVarnames(PyCodeObject *co)
{
return get_localsplus_names(co, CO_FAST_LOCAL, co->co_nlocals);
if (init_co_cached(co)) {
return NULL;
}
if (co->_co_cached->_co_varnames != NULL) {
return Py_NewRef(co->_co_cached->_co_varnames);
}
assert(co->_co_cached->_co_varnames == NULL);
PyObject *varnames = get_localsplus_names(co, CO_FAST_LOCAL, co->co_nlocals);
if (varnames == NULL) {
return NULL;
}
co->_co_cached->_co_varnames = Py_NewRef(varnames);
return varnames;

}

PyObject *
Expand All @@ -1383,7 +1411,16 @@ 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;
}
if (co->_co_cached->_co_cellvars != NULL) {
return Py_NewRef(co->_co_cached->_co_cellvars);
}
PyObject *cellvars = get_localsplus_names(co, CO_FAST_CELL, co->co_ncellvars);
assert(co->_co_cached->_co_cellvars == NULL);
co->_co_cached->_co_cellvars = Py_NewRef(cellvars);
return cellvars;
}

PyObject *
Expand All @@ -1395,7 +1432,16 @@ 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;
}
if (co->_co_cached->_co_freevars != NULL) {
return Py_NewRef(co->_co_cached->_co_freevars);
}
PyObject *freevars = get_localsplus_names(co, CO_FAST_FREE, co->co_nfreevars);
assert(co->_co_cached->_co_freevars == NULL);
co->_co_cached->_co_freevars = Py_NewRef(freevars);
return freevars;
}

PyObject *
Expand All @@ -1421,17 +1467,20 @@ 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));
if (code == NULL) {
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;
}

Expand Down Expand Up @@ -1590,7 +1639,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);
}
Expand Down Expand Up @@ -2165,7 +2220,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);
Expand Down
7 changes: 5 additions & 2 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -644,9 +644,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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion Tools/scripts/deepfreeze.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]):
Expand Down