From a388c2d9eb32bc7efe1334c125b34e4ee6028f79 Mon Sep 17 00:00:00 2001 From: Yan Yanchii Date: Sat, 28 Dec 2024 13:27:31 +0100 Subject: [PATCH] Move type checks from switch cases to begining of `_Py_Specialize_BinaryOp` --- Python/specialize.c | 15 ++++++--------- 1 file changed, 6 insertions(+), 9 deletions(-) diff --git a/Python/specialize.c b/Python/specialize.c index 349ed472298945..e14e4e52189bda 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -2384,12 +2384,13 @@ _Py_Specialize_BinaryOp(_PyStackRef lhs_st, _PyStackRef rhs_st, _Py_CODEUNIT *in PyObject *rhs = PyStackRef_AsPyObjectBorrow(rhs_st); assert(ENABLE_SPECIALIZATION_FT); assert(_PyOpcode_Caches[BINARY_OP] == INLINE_CACHE_ENTRIES_BINARY_OP); + + if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) + goto fail; + switch (oparg) { case NB_ADD: case NB_INPLACE_ADD: - if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) { - break; - } if (PyUnicode_CheckExact(lhs)) { _Py_CODEUNIT next = instr[INLINE_CACHE_ENTRIES_BINARY_OP + 1]; bool to_store = (next.op.code == STORE_FAST); @@ -2411,9 +2412,6 @@ _Py_Specialize_BinaryOp(_PyStackRef lhs_st, _PyStackRef rhs_st, _Py_CODEUNIT *in break; case NB_MULTIPLY: case NB_INPLACE_MULTIPLY: - if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) { - break; - } if (PyLong_CheckExact(lhs)) { specialize(instr, BINARY_OP_MULTIPLY_INT); return; @@ -2425,9 +2423,6 @@ _Py_Specialize_BinaryOp(_PyStackRef lhs_st, _PyStackRef rhs_st, _Py_CODEUNIT *in break; case NB_SUBTRACT: case NB_INPLACE_SUBTRACT: - if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) { - break; - } if (PyLong_CheckExact(lhs)) { specialize(instr, BINARY_OP_SUBTRACT_INT); return; @@ -2438,6 +2433,8 @@ _Py_Specialize_BinaryOp(_PyStackRef lhs_st, _PyStackRef rhs_st, _Py_CODEUNIT *in } break; } + +fail: SPECIALIZATION_FAIL(BINARY_OP, binary_op_fail_kind(oparg, lhs, rhs)); unspecialize(instr); }