Skip to content

gh-161075: Avoid over-allocation in non-negative int bitwise operations - #161997

Open
ashm-dev wants to merge 1 commit into
python:mainfrom
ashm-dev:gh-161075
Open

ashm-dev wants to merge 1 commit into
python:mainfrom
ashm-dev:gh-161075

Conversation

@ashm-dev

@ashm-dev ashm-dev commented Nov 14, 2026 •

Copy link
Copy Markdown
Contributor

Trim leading zero digits before allocating results for non-negative int & and ^ operations. Return zero and one-digit results directly, and skip redundant normalization for non-negative results.

Add tests for cancellation, unequal operand lengths and digit boundaries.

For a = 1 << (30 * 8192), peak memory traced during a ^ a drops from 32,796 bytes to 0 on a debug build with 30-bit digits.

@ashm-dev

Copy link
Copy Markdown
Contributor Author

@eendebakpt @picnixz Could you please review this PR when you have a chance?

@eendebakpt eendebakpt left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do benchmark for the code changes here (issue benchmarks are for the other changes as well; also merge with main, there have been some relevant changes there)?

Comment thread Objects/longobject.c

if (nonnegative && size_z == 1 && op != '|') {
digit a0 = a_digits[0];
digit b0 = size_b ? b_digits[0] : 0;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
digit b0 = size_b ? b_digits[0] : 0;
digit b0 = b_digits[0];

Comment thread Objects/longobject.c
case '^':
negz = nega ^ negb;
size_z = size_a;
if (nonnegative && size_a == size_b) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
if (nonnegative && size_a == size_b) {
if (!negz && size_z <= size_b) {

I think we can widen the condition a bit (also for the other branch). Needs to be checked though.

Comment thread Objects/longobject.c
v_complement(z_digits, z_digits, size_z+1);
}

if (nonnegative) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would perhaps drop this (and the 1-digit shortcut above). If keeping here, add an assert that maybe_small_long and long_normalize are not needed here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting review type-feature A feature request or enhancement

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants