Commit 993ee82
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>1 parent 9777e8a commit 993ee82
3 files changed
Lines changed: 31 additions & 3 deletions
File tree
- Lib/test
- Misc/NEWS.d/next/Core_and_Builtins
- Objects
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1698 | 1698 | | |
1699 | 1699 | | |
1700 | 1700 | | |
| 1701 | + | |
| 1702 | + | |
| 1703 | + | |
| 1704 | + | |
| 1705 | + | |
| 1706 | + | |
| 1707 | + | |
| 1708 | + | |
| 1709 | + | |
| 1710 | + | |
| 1711 | + | |
| 1712 | + | |
| 1713 | + | |
| 1714 | + | |
| 1715 | + | |
1701 | 1716 | | |
1702 | 1717 | | |
1703 | 1718 | | |
| |||
Lines changed: 2 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
3760 | 3760 | | |
3761 | 3761 | | |
3762 | 3762 | | |
3763 | | - | |
| 3763 | + | |
| 3764 | + | |
| 3765 | + | |
| 3766 | + | |
| 3767 | + | |
| 3768 | + | |
| 3769 | + | |
3764 | 3770 | | |
3765 | 3771 | | |
3766 | 3772 | | |
| |||
3773 | 3779 | | |
3774 | 3780 | | |
3775 | 3781 | | |
3776 | | - | |
3777 | | - | |
| 3782 | + | |
| 3783 | + | |
| 3784 | + | |
| 3785 | + | |
| 3786 | + | |
| 3787 | + | |
| 3788 | + | |
3778 | 3789 | | |
3779 | 3790 | | |
3780 | 3791 | | |
| |||
0 commit comments