Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions Lib/test/test_opcache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
6 changes: 3 additions & 3 deletions Python/specialize.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down
Loading