summaryrefslogtreecommitdiffstats
path: root/Include/internal
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2024-03-21 19:37:41 (GMT)
committerGitHub <noreply@github.com>2024-03-21 19:37:41 (GMT)
commit570a82d46abfebb9976961113fb0f8bb400ad182 (patch)
tree7b0b356a3ee48e8452aa823cc14e6bbed870b599 /Include/internal
parentc85d84166a84a5cb2d724012726bad34229ad24e (diff)
downloadcpython-570a82d46abfebb9976961113fb0f8bb400ad182.zip
cpython-570a82d46abfebb9976961113fb0f8bb400ad182.tar.gz
cpython-570a82d46abfebb9976961113fb0f8bb400ad182.tar.bz2
gh-117045: Add code object to function version cache (#117028)
Changes to the function version cache: - In addition to the function object, also store the code object, and allow the latter to be retrieved even if the function has been evicted. - Stop assigning new function versions after a critical attribute (e.g. `__code__`) has been modified; the version is permanently reset to zero in this case. - Changes to `__annotations__` are no longer considered critical. (This fixes gh-109998.) Changes to the Tier 2 optimization machinery: - If we cannot map a function version to a function, but it is still mapped to a code object, we continue projecting the trace. The operand of the `_PUSH_FRAME` and `_POP_FRAME` opcodes can be either NULL, a function object, or a code object with the lowest bit set. This allows us to trace through code that calls an ephemeral function, i.e., a function that may not be alive when we are constructing the executor, e.g. a generator expression or certain nested functions. We will lose globals removal inside such functions, but we can still do other peephole operations (and even possibly [call inlining](https://github.com/python/cpython/pull/116290), if we decide to do it), which only need the code object. As before, if we cannot retrieve the code object from the cache, we stop projecting.
Diffstat (limited to 'Include/internal')
-rw-r--r--Include/internal/pycore_frame.h2
-rw-r--r--Include/internal/pycore_function.h15
2 files changed, 12 insertions, 5 deletions
diff --git a/Include/internal/pycore_frame.h b/Include/internal/pycore_frame.h
index 0f9e733..74d9e4c 100644
--- a/Include/internal/pycore_frame.h
+++ b/Include/internal/pycore_frame.h
@@ -55,7 +55,7 @@ enum _frameowner {
};
typedef struct _PyInterpreterFrame {
- PyObject *f_executable; /* Strong reference */
+ PyObject *f_executable; /* Strong reference (code object or None) */
struct _PyInterpreterFrame *previous;
PyObject *f_funcobj; /* Strong reference. Only valid if not on C stack */
PyObject *f_globals; /* Borrowed reference. Only valid if not on C stack */
diff --git a/Include/internal/pycore_function.h b/Include/internal/pycore_function.h
index dad6a89..24fbb3d 100644
--- a/Include/internal/pycore_function.h
+++ b/Include/internal/pycore_function.h
@@ -17,20 +17,27 @@ extern PyObject* _PyFunction_Vectorcall(
#define FUNC_MAX_WATCHERS 8
#define FUNC_VERSION_CACHE_SIZE (1<<12) /* Must be a power of 2 */
+
+struct _func_version_cache_item {
+ PyFunctionObject *func;
+ PyObject *code;
+};
+
struct _py_func_state {
uint32_t next_version;
- // Borrowed references to function objects whose
+ // Borrowed references to function and code objects whose
// func_version % FUNC_VERSION_CACHE_SIZE
// once was equal to the index in the table.
- // They are cleared when the function is deallocated.
- PyFunctionObject *func_version_cache[FUNC_VERSION_CACHE_SIZE];
+ // They are cleared when the function or code object is deallocated.
+ struct _func_version_cache_item func_version_cache[FUNC_VERSION_CACHE_SIZE];
};
extern PyFunctionObject* _PyFunction_FromConstructor(PyFrameConstructor *constr);
extern uint32_t _PyFunction_GetVersionForCurrentState(PyFunctionObject *func);
PyAPI_FUNC(void) _PyFunction_SetVersion(PyFunctionObject *func, uint32_t version);
-PyFunctionObject *_PyFunction_LookupByVersion(uint32_t version);
+void _PyFunction_ClearCodeByVersion(uint32_t version);
+PyFunctionObject *_PyFunction_LookupByVersion(uint32_t version, PyObject **p_code);
extern PyObject *_Py_set_function_type_params(
PyThreadState* unused, PyObject *func, PyObject *type_params);