Skip to content

Commit b6595bc

Browse files
Tapelinelkollar
authored andcommitted
pythongh-138302: Specialize int ops only if ints are compact (pythonGH-138347)
1 parent 5be4c97 commit b6595bc

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

Lib/test/test_opcache.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,21 @@ def binary_op_add_int():
13331333
self.assert_specialized(binary_op_add_int, "BINARY_OP_ADD_INT")
13341334
self.assert_no_opcode(binary_op_add_int, "BINARY_OP")
13351335

1336+
def binary_op_int_non_compact():
1337+
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
1338+
a, b = 10000000000, 1
1339+
c = a + b
1340+
self.assertEqual(c, 10000000001)
1341+
c = a - b
1342+
self.assertEqual(c, 9999999999)
1343+
c = a * b
1344+
self.assertEqual(c, 10000000000)
1345+
1346+
binary_op_int_non_compact()
1347+
self.assert_no_opcode(binary_op_int_non_compact, "BINARY_OP_ADD_INT")
1348+
self.assert_no_opcode(binary_op_int_non_compact, "BINARY_OP_SUBTRACT_INT")
1349+
self.assert_no_opcode(binary_op_int_non_compact, "BINARY_OP_MULTIPLY_INT")
1350+
13361351
def binary_op_add_unicode():
13371352
for _ in range(_testinternalcapi.SPECIALIZATION_THRESHOLD):
13381353
a, b = "foo", "bar"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
``BINARY_OP`` now specializes to ``BINARY_OP_ADD_INT``,
2+
``BINARY_OP_SUBTRACT_INT`` or ``BINARY_OP_MULTIPLY_INT`` if operands
3+
are compact ints.

Python/specialize.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2595,7 +2595,7 @@ _Py_Specialize_BinaryOp(_PyStackRef lhs_st, _PyStackRef rhs_st, _Py_CODEUNIT *in
25952595
specialize(instr, BINARY_OP_ADD_UNICODE);
25962596
return;
25972597
}
2598-
if (PyLong_CheckExact(lhs)) {
2598+
if (_PyLong_CheckExactAndCompact(lhs) && _PyLong_CheckExactAndCompact(rhs)) {
25992599
specialize(instr, BINARY_OP_ADD_INT);
26002600
return;
26012601
}
@@ -2609,7 +2609,7 @@ _Py_Specialize_BinaryOp(_PyStackRef lhs_st, _PyStackRef rhs_st, _Py_CODEUNIT *in
26092609
if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
26102610
break;
26112611
}
2612-
if (PyLong_CheckExact(lhs)) {
2612+
if (_PyLong_CheckExactAndCompact(lhs) && _PyLong_CheckExactAndCompact(rhs)) {
26132613
specialize(instr, BINARY_OP_MULTIPLY_INT);
26142614
return;
26152615
}
@@ -2623,7 +2623,7 @@ _Py_Specialize_BinaryOp(_PyStackRef lhs_st, _PyStackRef rhs_st, _Py_CODEUNIT *in
26232623
if (!Py_IS_TYPE(lhs, Py_TYPE(rhs))) {
26242624
break;
26252625
}
2626-
if (PyLong_CheckExact(lhs)) {
2626+
if (_PyLong_CheckExactAndCompact(lhs) && _PyLong_CheckExactAndCompact(rhs)) {
26272627
specialize(instr, BINARY_OP_SUBTRACT_INT);
26282628
return;
26292629
}

0 commit comments

Comments
 (0)