Skip to content

Commit

Permalink
gh-93382: Cache result of PyCode_GetCode in codeobject (GH-93383)
Browse files Browse the repository at this point in the history
Co-authored-by: Kumar Aditya <59607654+kumaraditya303@users.noreply.github.com>
Co-authored-by: Dennis Sweeney <36520290+sweeneyde@users.noreply.github.com>
  • Loading branch information
3 people authored Jun 3, 2022
1 parent d8f40ea commit d52ffc1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 4 deletions.
1 change: 1 addition & 0 deletions Include/cpython/code.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,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 */ \
void *_co_code; /* cached co_code object/attribute */ \
/* Scratch space for extra data relating to the code object. \
Type is a void* to keep the format private in codeobject.c to force \
people to go through the proper APIs. */ \
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Speed up the :c:func:`PyCode_GetCode` function which also improves accessing the :attr:`~types.CodeType.co_code` attribute in Python.
7 changes: 7 additions & 0 deletions Objects/codeobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ init_code(PyCodeObject *co, struct _PyCodeConstructor *con)
/* not set */
co->co_weakreflist = NULL;
co->co_extra = NULL;
co->_co_code = NULL;

co->co_warmup = QUICKENING_INITIAL_WARMUP_VALUE;
memcpy(_PyCode_CODE(co), PyBytes_AS_STRING(con->code),
Expand Down Expand Up @@ -1367,12 +1368,17 @@ 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);
}
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 = (void *)Py_NewRef(code);
return code;
}

Expand Down Expand Up @@ -1531,6 +1537,7 @@ 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_weakreflist != NULL) {
PyObject_ClearWeakRefs((PyObject*)co);
}
Expand Down
8 changes: 8 additions & 0 deletions Objects/frameobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -458,22 +458,30 @@ _PyFrame_GetState(PyFrameObject *frame)
static void
add_load_fast_null_checks(PyCodeObject *co)
{
int changed = 0;
_Py_CODEUNIT *instructions = _PyCode_CODE(co);
for (Py_ssize_t i = 0; i < Py_SIZE(co); i++) {
switch (_Py_OPCODE(instructions[i])) {
case LOAD_FAST:
case LOAD_FAST__LOAD_FAST:
case LOAD_FAST__LOAD_CONST:
changed = 1;
_Py_SET_OPCODE(instructions[i], LOAD_FAST_CHECK);
break;
case LOAD_CONST__LOAD_FAST:
changed = 1;
_Py_SET_OPCODE(instructions[i], LOAD_CONST);
break;
case STORE_FAST__LOAD_FAST:
changed = 1;
_Py_SET_OPCODE(instructions[i], STORE_FAST);
break;
}
}
if (changed) {
// invalidate cached co_code object
Py_CLEAR(co->_co_code);
}
}

/* Setter for f_lineno - you can set f_lineno from within a trace function in
Expand Down
8 changes: 4 additions & 4 deletions Programs/test_frozenmain.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit d52ffc1

Please sign in to comment.