From: Tapeline Date: Mon, 1 Sep 2025 17:33:15 +0000 (+0500) Subject: gh-138302: Specialize int ops only if ints are compact (GH-138347) X-Git-Tag: v3.15.0a1~546 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=ea77feecbba389916af8f90b2fc77f07910a2963;p=thirdparty%2FPython%2Fcpython.git gh-138302: Specialize int ops only if ints are compact (GH-138347) --- diff --git a/Lib/test/test_opcache.py b/Lib/test/test_opcache.py index 30baa0904861..f1271fc540eb 100644 --- a/Lib/test/test_opcache.py +++ b/Lib/test/test_opcache.py @@ -1333,6 +1333,21 @@ class TestSpecializer(TestBase): self.assert_specialized(binary_op_add_int, "BINARY_OP_ADD_INT") self.assert_no_opcode(binary_op_add_int, "BINARY_OP") + def binary_op_int_non_compact(): + for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): + a, b = 10000000000, 1 + c = a + b + self.assertEqual(c, 10000000001) + c = a - b + self.assertEqual(c, 9999999999) + c = a * b + self.assertEqual(c, 10000000000) + + binary_op_int_non_compact() + self.assert_no_opcode(binary_op_int_non_compact, "BINARY_OP_ADD_INT") + self.assert_no_opcode(binary_op_int_non_compact, "BINARY_OP_SUBTRACT_INT") + self.assert_no_opcode(binary_op_int_non_compact, "BINARY_OP_MULTIPLY_INT") + def binary_op_add_unicode(): for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD): a, b = "foo", "bar" diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-09-01-21-52-54.gh-issue-138302.-ez47B.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-01-21-52-54.gh-issue-138302.-ez47B.rst new file mode 100644 index 000000000000..156b9df48b40 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2025-09-01-21-52-54.gh-issue-138302.-ez47B.rst @@ -0,0 +1,3 @@ +``BINARY_OP`` now specializes to ``BINARY_OP_ADD_INT``, +``BINARY_OP_SUBTRACT_INT`` or ``BINARY_OP_MULTIPLY_INT`` if operands +are compact ints. diff --git a/Python/specialize.c b/Python/specialize.c index 38df5741f325..47f7b27b4908 100644 --- a/Python/specialize.c +++ b/Python/specialize.c @@ -2595,7 +2595,7 @@ _Py_Specialize_BinaryOp(_PyStackRef lhs_st, _PyStackRef rhs_st, _Py_CODEUNIT *in specialize(instr, BINARY_OP_ADD_UNICODE); return; } - if (PyLong_CheckExact(lhs)) { + if (_PyLong_CheckExactAndCompact(lhs) && _PyLong_CheckExactAndCompact(rhs)) { specialize(instr, BINARY_OP_ADD_INT); return; } @@ -2609,7 +2609,7 @@ _Py_Specialize_BinaryOp(_PyStackRef lhs_st, _PyStackRef rhs_st, _Py_CODEUNIT *in if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) { break; } - if (PyLong_CheckExact(lhs)) { + if (_PyLong_CheckExactAndCompact(lhs) && _PyLong_CheckExactAndCompact(rhs)) { specialize(instr, BINARY_OP_MULTIPLY_INT); return; } @@ -2623,7 +2623,7 @@ _Py_Specialize_BinaryOp(_PyStackRef lhs_st, _PyStackRef rhs_st, _Py_CODEUNIT *in if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) { break; } - if (PyLong_CheckExact(lhs)) { + if (_PyLong_CheckExactAndCompact(lhs) && _PyLong_CheckExactAndCompact(rhs)) { specialize(instr, BINARY_OP_SUBTRACT_INT); return; }