Skip to content

Commit e59260b

Browse files
eendebakptmdickinsonclaudeStanFromIreland
authored
gh-100687: Reduce frequency of overallocation in x_add() (#158002)
* gh-100687: Reduce frequency of overallocation in x_add() x_add() allocated a digit for the carry out of the top digit on every call. In most cases that digit is zero and long_normalize() drops it from the digit count, but the allocation keeps its size, so the result uses more memory than its value needs for as long as it lives. Allocate the carry digit only when the top digits of the operands sum to at least PyLong_MASK, the only case in which a carry out of the top digit is possible. When no carry digit was allocated the top digit of the result is at least the top digit of the larger operand, so the result is already normalized and is returned directly. This applies to same-sign additions and opposite-sign subtractions, which go through x_add(); x_sub() is unchanged. Extends PR 100688 by Mark Dickinson, which contributed the size test and the tests. Co-authored-by: Mark Dickinson <dickinsm@gmail.com> Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> * Apply batched suggestions from code review Co-authored-by: Stan Ulbrych <stan@python.org> --------- Co-authored-by: Mark Dickinson <dickinsm@gmail.com> Co-authored-by: Claude Fable 5.1 <noreply@anthropic.com> Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent 355486e commit e59260b

3 files changed

Lines changed: 30 additions & 3 deletions

File tree

‎Lib/test/test_long.py‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1734,6 +1734,20 @@ class MyInt(int):
17341734
# GH-117195 -- This shouldn't crash
17351735
object.__sizeof__(1)
17361736

1737+
def test_long_add_overallocate(self):
1738+
# see gh-100687
1739+
x = (MASK//2) * (MASK+1)
1740+
x2 = (MASK//2 + 1) * (MASK+1)
1741+
self.assertEqual(x + x2, MASK * (MASK + 1))
1742+
1743+
def test_karatsuba_single_digit_parts(self):
1744+
# gh-100687: k_mul() adds the halves of its operands with x_add(),
1745+
# and those halves can be single digits.
1746+
a = 1 + (1 << (SHIFT * 70))
1747+
b = 1 << (SHIFT * 139)
1748+
self.assertEqual(a * b, (1 << (SHIFT * 139)) + (1 << (SHIFT * 209)))
1749+
self.assertEqual(b * a, a * b)
1750+
17371751
def test_hash(self):
17381752
# gh-136599
17391753
self.assertEqual(hash(-1), -2)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Reduce the memory used by the result of adding two large :class:`int`
2+
values. Such additions are also slightly faster for most sizes.

‎Objects/longobject.c‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3755,7 +3755,13 @@ x_add(PyLongObject *a, PyLongObject *b)
37553755
size_a = size_b;
37563756
size_b = size_temp; }
37573757
}
3758-
z = long_alloc(size_a+1);
3758+
assert(size_a >= 1);
3759+
/* A carry out of the top digit is only possible if the top digits sum
3760+
to at least PyLong_MASK; only then allocate a digit for it. */
3761+
digit top_sum = a->long_value.ob_digit[size_a - 1]
3762+
+ (size_b == size_a ? b->long_value.ob_digit[size_b - 1] : 0);
3763+
int extra_digit = top_sum >= PyLong_MASK;
3764+
z = long_alloc(size_a + extra_digit);
37593765
if (z == NULL)
37603766
return NULL;
37613767
for (i = 0; i < size_b; ++i) {
@@ -3768,8 +3774,13 @@ x_add(PyLongObject *a, PyLongObject *b)
37683774
z->long_value.ob_digit[i] = carry & PyLong_MASK;
37693775
carry >>= PyLong_SHIFT;
37703776
}
3771-
z->long_value.ob_digit[i] = carry;
3772-
return long_normalize(z);
3777+
if (extra_digit) {
3778+
z->long_value.ob_digit[i] = carry;
3779+
return long_normalize(z);
3780+
}
3781+
assert(carry == 0);
3782+
assert(z->long_value.ob_digit[i - 1] != 0);
3783+
return z;
37733784
}
37743785

37753786
/* Subtract the absolute values of two integers. */

0 commit comments

Comments
 (0)