From dd278b673687dd3d609a753fcf11a1c14bfff5e1 Mon Sep 17 00:00:00 2001 From: Josh Hurst Date: Fri, 27 Nov 2026 18:41:12 -0500 Subject: [PATCH 1/2] gh-162777: Avoid a temporary Decimal for arithmetic with small integers --- Lib/test/test_decimal.py | 47 +++++++++++++++++++ ...-11-27-18-00-00.gh-issue-162777.Kc8bJf.rst | 2 + Modules/_decimal/_decimal.c | 44 +++++++++++++++++ 3 files changed, 93 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-27-18-00-00.gh-issue-162777.Kc8bJf.rst diff --git a/Lib/test/test_decimal.py b/Lib/test/test_decimal.py index ef7aa5358b5cc64..8344d55c334ea24 100644 --- a/Lib/test/test_decimal.py +++ b/Lib/test/test_decimal.py @@ -1335,6 +1335,53 @@ class PyFormatTest(FormatTest, unittest.TestCase): class ArithmeticOperatorsTest: '''Unit tests for all arithmetic operators, binary and unary.''' + def test_int64_operands(self): + Decimal = self.decimal.Decimal + + class SubDecimal(Decimal): + pass + + class SubInt(int): + pass + + operands = ( + -(2**63) - 1, -(2**63), -1, 0, 2**63 - 1, 2**63, + True, SubInt(7), + ) + for cls in (Decimal, SubDecimal): + d = cls("-1.25") + for n in operands: + for op in (operator.add, operator.sub, operator.mul, + operator.truediv, operator.floordiv, operator.mod): + for left, right in ((d, n), (n, d)): + with self.subTest(cls=cls, n=n, op=op, left=left): + with self.decimal.localcontext(prec=6) as ctx: + ctx.clear_traps() + ctx.clear_flags() + expected = op(Decimal(left), Decimal(right)) + flags = ctx.flags.copy() + ctx.clear_flags() + actual = op(left, right) + self.assertIs(type(actual), Decimal) + self.assertEqual( + actual.as_tuple(), expected.as_tuple() + ) + self.assertEqual(ctx.flags.copy(), flags) + + def test_int_operand_traps(self): + dec = self.decimal + with dec.localcontext(prec=2) as ctx: + ctx.traps[dec.Inexact] = True + ctx.clear_flags() + with self.assertRaises(dec.Inexact): + dec.Decimal(2**63 - 1) - dec.Decimal("0.1") + expected_flags = ctx.flags.copy() + ctx.clear_flags() + with self.assertRaises(dec.Inexact): + (2**63 - 1) - dec.Decimal("0.1") + self.assertEqual(ctx.flags.copy(), expected_flags) + self.assertTrue(ctx.flags[dec.Inexact]) + def test_addition(self): Decimal = self.decimal.Decimal diff --git a/Misc/NEWS.d/next/Library/2026-11-27-18-00-00.gh-issue-162777.Kc8bJf.rst b/Misc/NEWS.d/next/Library/2026-11-27-18-00-00.gh-issue-162777.Kc8bJf.rst new file mode 100644 index 000000000000000..ca952157c478b4c --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-27-18-00-00.gh-issue-162777.Kc8bJf.rst @@ -0,0 +1,2 @@ +Speed up arithmetic between a :class:`~decimal.Decimal` and an :class:`int` +that fits in a signed 64-bit integer. diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c index 3a128a13222f9b5..83f73c65f98dfeb 100644 --- a/Modules/_decimal/_decimal.c +++ b/Modules/_decimal/_decimal.c @@ -4491,6 +4491,38 @@ nm_##MPDFUNC(PyObject *self) \ return result; \ } +typedef void (*mpd_binaryfunc)(mpd_t *, const mpd_t *, const mpd_t *, + const mpd_context_t *, uint32_t *); + +static PyObject * +dec_binop_int64(decimal_state *state, PyObject *context, mpd_binaryfunc op, + PyObject *decimal, int64_t value, int reflected) +{ + mpd_uint_t data[_Py_DEC_MINALLOC]; + mpd_t integer = { + .flags = MPD_STATIC | MPD_STATIC_DATA, + .alloc = _Py_DEC_MINALLOC, + .data = data, + }; + uint32_t status = 0; + mpd_qset_i64_exact(&integer, value, &status); + if (status && dec_addstatus(context, status)) { + mpd_del(&integer); + return NULL; + } + PyObject *result = dec_alloc(state); + if (result != NULL) { + const mpd_t *a = reflected ? &integer : MPD(decimal); + const mpd_t *b = reflected ? MPD(decimal) : &integer; + op(MPD(result), a, b, CTX(context), &status); + if (dec_addstatus(context, status)) { + Py_CLEAR(result); + } + } + mpd_del(&integer); + return result; +} + /* Binary number method that uses default module context. */ #define Dec_BinaryNumberMethod(MPDFUNC) \ static PyObject * \ @@ -4503,6 +4535,18 @@ nm_##MPDFUNC(PyObject *self, PyObject *other) \ \ decimal_state *state = find_state_left_or_right(self, other); \ CURRENT_CONTEXT(state, context) ; \ + int reflected = PyLong_CheckExact(self); \ + if (reflected || PyLong_CheckExact(other)) { \ + PyObject *integer = reflected ? self : other; \ + int overflow; \ + long long value = \ + PyLong_AsLongLongAndOverflow(integer, &overflow); \ + if (!overflow) { \ + return dec_binop_int64(state, context, MPDFUNC, \ + reflected ? other : self, \ + value, reflected); \ + } \ + } \ CONVERT_BINOP(&a, &b, self, other, context); \ \ if ((result = dec_alloc(state)) == NULL) { \ From 5ce2e2656fe4bed2748ba4a79fc275dbe7c0079a Mon Sep 17 00:00:00 2001 From: Josh Hurst Date: Sat, 28 Nov 2026 16:11:49 -0500 Subject: [PATCH 2/2] gh-162777: Check exact Decimal operands before int64 fast path --- Modules/_decimal/_decimal.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/Modules/_decimal/_decimal.c b/Modules/_decimal/_decimal.c index 83f73c65f98dfeb..de2a297b76c5b5f 100644 --- a/Modules/_decimal/_decimal.c +++ b/Modules/_decimal/_decimal.c @@ -4535,6 +4535,19 @@ nm_##MPDFUNC(PyObject *self, PyObject *other) \ \ decimal_state *state = find_state_left_or_right(self, other); \ CURRENT_CONTEXT(state, context) ; \ + if (PyDec_CheckExact(state, self) && \ + PyDec_CheckExact(state, other)) { \ + if ((result = dec_alloc(state)) == NULL) { \ + return NULL; \ + } \ + MPDFUNC(MPD(result), MPD(self), MPD(other), \ + CTX(context), &status); \ + if (dec_addstatus(context, status)) { \ + Py_DECREF(result); \ + return NULL; \ + } \ + return result; \ + } \ int reflected = PyLong_CheckExact(self); \ if (reflected || PyLong_CheckExact(other)) { \ PyObject *integer = reflected ? self : other; \ @@ -4542,6 +4555,7 @@ nm_##MPDFUNC(PyObject *self, PyObject *other) \ long long value = \ PyLong_AsLongLongAndOverflow(integer, &overflow); \ if (!overflow) { \ + Py_BUILD_ASSERT(sizeof(value) == sizeof(int64_t)); \ return dec_binop_int64(state, context, MPDFUNC, \ reflected ? other : self, \ value, reflected); \