aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/Python/optimizer_bytecodes.c
diff options
context:
space:
mode:
authorSavannah Ostrowski <savannahostrowski@gmail.com>2025-03-21 11:23:12 -0700
committerGitHub <noreply@github.com>2025-03-21 11:23:12 -0700
commitb92ee14b80cc8898f799aa8120ec99dd0c882339 (patch)
treea32440d8906db0dc9393983205d43b3cfb4eef41 /Python/optimizer_bytecodes.c
parent0de5e0c5442abddbe17481ef450e4abc992058f5 (diff)
downloadcpython-b92ee14b80cc8898f799aa8120ec99dd0c882339.tar.gz
cpython-b92ee14b80cc8898f799aa8120ec99dd0c882339.zip
GH-130415: Optimize constant comparison in JIT builds (GH-131489)
Diffstat (limited to 'Python/optimizer_bytecodes.c')
-rw-r--r--Python/optimizer_bytecodes.c20
1 files changed, 19 insertions, 1 deletions
diff --git a/Python/optimizer_bytecodes.c b/Python/optimizer_bytecodes.c
index cfa0a733cda..cba878748c2 100644
--- a/Python/optimizer_bytecodes.c
+++ b/Python/optimizer_bytecodes.c
@@ -446,7 +446,25 @@ dummy_func(void) {
}
op(_COMPARE_OP_INT, (left, right -- res)) {
- res = sym_new_type(ctx, &PyBool_Type);
+ if (sym_is_const(ctx, left) && sym_is_const(ctx, right))
+ {
+ assert(PyLong_CheckExact(sym_get_const(ctx, left)));
+ assert(PyLong_CheckExact(sym_get_const(ctx, right)));
+ PyObject *tmp = PyObject_RichCompare(sym_get_const(ctx, left),
+ sym_get_const(ctx, right),
+ oparg >> 5);
+ if (tmp == NULL) {
+ goto error;
+ }
+ assert(PyBool_Check(tmp));
+ assert(_Py_IsImmortal(tmp));
+ REPLACE_OP(this_instr, _POP_TWO_LOAD_CONST_INLINE_BORROW, 0, (uintptr_t)tmp);
+ res = sym_new_const(ctx, tmp);
+ Py_DECREF(tmp);
+ }
+ else {
+ res = sym_new_type(ctx, &PyBool_Type);
+ }
}
op(_COMPARE_OP_FLOAT, (left, right -- res)) {