From faa3272fb8d63d481a136cc0467a0cba6ed7b264 Mon Sep 17 00:00:00 2001 From: Mark Shannon Date: Tue, 29 Oct 2024 11:15:42 +0000 Subject: GH-125837: Split `LOAD_CONST` into three. (GH-125972) * Add LOAD_CONST_IMMORTAL opcode * Add LOAD_SMALL_INT opcode * Remove RETURN_CONST opcode --- Python/specialize.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'Python/specialize.c') diff --git a/Python/specialize.c b/Python/specialize.c index 4b33a468733..ae47809305a 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -442,11 +442,13 @@ _PyCode_Quicken(PyCodeObject *code) { #if ENABLE_SPECIALIZATION int opcode = 0; + int oparg = 0; _Py_CODEUNIT *instructions = _PyCode_CODE(code); /* The last code unit cannot have a cache, so we don't need to check it */ for (int i = 0; i < Py_SIZE(code)-1; i++) { opcode = instructions[i].op.code; int caches = _PyOpcode_Caches[opcode]; + oparg = (oparg << 8) | instructions[i].op.arg; if (caches) { // The initial value depends on the opcode switch (opcode) { @@ -465,6 +467,18 @@ _PyCode_Quicken(PyCodeObject *code) } i += caches; } + else if (opcode == LOAD_CONST) { + /* We can't do this in the bytecode compiler as + * marshalling can intern strings and make them immortal. */ + + PyObject *obj = PyTuple_GET_ITEM(code->co_consts, oparg); + if (_Py_IsImmortal(obj)) { + instructions[i].op.code = LOAD_CONST_IMMORTAL; + } + } + if (opcode != EXTENDED_ARG) { + oparg = 0; + } } #endif /* ENABLE_SPECIALIZATION */ } -- cgit v1.2.3