Skip to content

Commit dd278b6

Browse files
committed
gh-162777: Avoid a temporary Decimal for arithmetic with small integers
1 parent d88a6b9 commit dd278b6

3 files changed

Lines changed: 93 additions & 0 deletions

File tree

‎Lib/test/test_decimal.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1335,6 +1335,53 @@ class PyFormatTest(FormatTest, unittest.TestCase):
13351335
class ArithmeticOperatorsTest:
13361336
'''Unit tests for all arithmetic operators, binary and unary.'''
13371337

1338+
def test_int64_operands(self):
1339+
Decimal = self.decimal.Decimal
1340+
1341+
class SubDecimal(Decimal):
1342+
pass
1343+
1344+
class SubInt(int):
1345+
pass
1346+
1347+
operands = (
1348+
-(2**63) - 1, -(2**63), -1, 0, 2**63 - 1, 2**63,
1349+
True, SubInt(7),
1350+
)
1351+
for cls in (Decimal, SubDecimal):
1352+
d = cls("-1.25")
1353+
for n in operands:
1354+
for op in (operator.add, operator.sub, operator.mul,
1355+
operator.truediv, operator.floordiv, operator.mod):
1356+
for left, right in ((d, n), (n, d)):
1357+
with self.subTest(cls=cls, n=n, op=op, left=left):
1358+
with self.decimal.localcontext(prec=6) as ctx:
1359+
ctx.clear_traps()
1360+
ctx.clear_flags()
1361+
expected = op(Decimal(left), Decimal(right))
1362+
flags = ctx.flags.copy()
1363+
ctx.clear_flags()
1364+
actual = op(left, right)
1365+
self.assertIs(type(actual), Decimal)
1366+
self.assertEqual(
1367+
actual.as_tuple(), expected.as_tuple()
1368+
)
1369+
self.assertEqual(ctx.flags.copy(), flags)
1370+
1371+
def test_int_operand_traps(self):
1372+
dec = self.decimal
1373+
with dec.localcontext(prec=2) as ctx:
1374+
ctx.traps[dec.Inexact] = True
1375+
ctx.clear_flags()
1376+
with self.assertRaises(dec.Inexact):
1377+
dec.Decimal(2**63 - 1) - dec.Decimal("0.1")
1378+
expected_flags = ctx.flags.copy()
1379+
ctx.clear_flags()
1380+
with self.assertRaises(dec.Inexact):
1381+
(2**63 - 1) - dec.Decimal("0.1")
1382+
self.assertEqual(ctx.flags.copy(), expected_flags)
1383+
self.assertTrue(ctx.flags[dec.Inexact])
1384+
13381385
def test_addition(self):
13391386
Decimal = self.decimal.Decimal
13401387

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Speed up arithmetic between a :class:`~decimal.Decimal` and an :class:`int`
2+
that fits in a signed 64-bit integer.

‎Modules/_decimal/_decimal.c‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4491,6 +4491,38 @@ nm_##MPDFUNC(PyObject *self) \
44914491
return result; \
44924492
}
44934493

4494+
typedef void (*mpd_binaryfunc)(mpd_t *, const mpd_t *, const mpd_t *,
4495+
const mpd_context_t *, uint32_t *);
4496+
4497+
static PyObject *
4498+
dec_binop_int64(decimal_state *state, PyObject *context, mpd_binaryfunc op,
4499+
PyObject *decimal, int64_t value, int reflected)
4500+
{
4501+
mpd_uint_t data[_Py_DEC_MINALLOC];
4502+
mpd_t integer = {
4503+
.flags = MPD_STATIC | MPD_STATIC_DATA,
4504+
.alloc = _Py_DEC_MINALLOC,
4505+
.data = data,
4506+
};
4507+
uint32_t status = 0;
4508+
mpd_qset_i64_exact(&integer, value, &status);
4509+
if (status && dec_addstatus(context, status)) {
4510+
mpd_del(&integer);
4511+
return NULL;
4512+
}
4513+
PyObject *result = dec_alloc(state);
4514+
if (result != NULL) {
4515+
const mpd_t *a = reflected ? &integer : MPD(decimal);
4516+
const mpd_t *b = reflected ? MPD(decimal) : &integer;
4517+
op(MPD(result), a, b, CTX(context), &status);
4518+
if (dec_addstatus(context, status)) {
4519+
Py_CLEAR(result);
4520+
}
4521+
}
4522+
mpd_del(&integer);
4523+
return result;
4524+
}
4525+
44944526
/* Binary number method that uses default module context. */
44954527
#define Dec_BinaryNumberMethod(MPDFUNC) \
44964528
static PyObject * \
@@ -4503,6 +4535,18 @@ nm_##MPDFUNC(PyObject *self, PyObject *other) \
45034535
\
45044536
decimal_state *state = find_state_left_or_right(self, other); \
45054537
CURRENT_CONTEXT(state, context) ; \
4538+
int reflected = PyLong_CheckExact(self); \
4539+
if (reflected || PyLong_CheckExact(other)) { \
4540+
PyObject *integer = reflected ? self : other; \
4541+
int overflow; \
4542+
long long value = \
4543+
PyLong_AsLongLongAndOverflow(integer, &overflow); \
4544+
if (!overflow) { \
4545+
return dec_binop_int64(state, context, MPDFUNC, \
4546+
reflected ? other : self, \
4547+
value, reflected); \
4548+
} \
4549+
} \
45064550
CONVERT_BINOP(&a, &b, self, other, context); \
45074551
\
45084552
if ((result = dec_alloc(state)) == NULL) { \

0 commit comments

Comments
 (0)