https://github.com/python/cpython/commit/ea77feecbba389916af8f90b2fc77f07910a2963
commit: ea77feecbba389916af8f90b2fc77f07910a2963
branch: main
author: Tapeline <[email protected]>
committer: Fidget-Spinner <[email protected]>
date: 2025-09-02T01:33:15+08:00
summary:

gh-138302: Specialize int ops only if ints are compact (GH-138347)

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2025-09-01-21-52-54.gh-issue-138302.-ez47B.rst
M Lib/test/test_opcache.py
M Python/specialize.c

diff --git a/Lib/test/test_opcache.py b/Lib/test/test_opcache.py
index 30baa09048616c..f1271fc540ebd8 100644
--- a/Lib/test/test_opcache.py
+++ b/Lib/test/test_opcache.py
@@ -1333,6 +1333,21 @@ def binary_op_add_int():
         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 00000000000000..156b9df48b40ee
--- /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 38df5741f32520..47f7b27b4908fd 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;
             }

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to