We need an API for optimizers to be plugged in to CPython. The proposed model is that of client server, where the VM is the client and the optimizer is the server. The optimizer registers with the VM, then VM calls the optimizer when hotspots are detected. The API: ```C type struct { OBJECT_HEADER; _PyInterpreterFrame *(*execute)(PyExecutorObject *self, _PyInterpreterFrame *frame, PyObject **stack_pointer); /* Data needed by the executor goes here, but is opaque to the VM */ } PyExecutorObject; /* This would be nicer as an enum, but C doesn't define the size of enums */ #define PY_OPTIMIZE_FUNCTION_ENTRY 1 #define PY_OPTIMIZE_RESUME_AFTER_YIELD 2 #define PY_OPTIMIZE_BACK_EDGE 4 typedef uint32_t PyOptimizerCapabilities; type struct { OBJECT_HEADER; PyExecutorObject *(*compile)(PyOptimizerObject* self, PyCodeObject *code, int offset); PyOptimizerCapabilities capabilities; float optimization_cost; float run_cost; /* Data needed by the compiler goes here, but is opaque to the VM */ } PyOptimizerObject; void _Py_Executor_Replace(PyCodeObject *code, int offset, PyExecutorObject *executor); int _Py_Optimizer_Register(PyOptimizerObject* optimizer); ``` The semantics of a `PyExecutorObject` is that upon return from its `execute` function, the VM state will have advanced `N` instructions. Where `N` is a non-negative integer. Full discussion here: https://github.com/faster-cpython/ideas/discussions/380 This is not a replacement for PEP 523. That will need a PEP. We should get this working first, before we consider replacing PEP 523. <!-- gh-linked-prs --> ### Linked PRs * gh-105100 * gh-105244 * gh-105683 * gh-105924 * gh-106126 * gh-106131 * gh-106141 * gh-106146 * gh-106163 * gh-106171 * gh-106277 * gh-106393 * gh-106484 * gh-106489 * gh-106492 * gh-106497 * gh-106500 * gh-106526 * gh-106641 * gh-106908 * gh-107513 * gh-108953 * gh-108961 * gh-109347 * gh-110593 <!-- /gh-linked-prs -->