From 9d27989bf861eb361aee852c9538afe4d8db8e3b Mon Sep 17 00:00:00 2001 From: Shamil Abdulaev Date: Sat, 14 Nov 2026 14:43:53 +0300 Subject: [PATCH] perf(int): trim bitwise results before allocation --- Lib/test/test_long.py | 38 ++++++++++++++ ...-11-14-14-39-19.gh-issue-161075.mK8r2Q.rst | 2 + Objects/longobject.c | 49 ++++++++++++++----- 3 files changed, 78 insertions(+), 11 deletions(-) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-11-14-14-39-19.gh-issue-161075.mK8r2Q.rst diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index c16bf1b1fe020e9..fec4d8f8d3374d7 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -316,6 +316,44 @@ def test_bitop_identities(self): self.check_bitop_identities_2(x, y) self.check_bitop_identities_3(x, y, self.getran((lenx + leny)//2)) + def test_bitwise_cancellation(self): + for ndigits in (3, 4, 8, 16): + high = BASE ** ndigits + for low in (0, 1, 256, 257, BASE - 1, BASE, BASE + 1, + BASE ** 2 - 1, BASE ** 2): + with self.subTest(ndigits=ndigits, low=low): + self.assertEqual((high + low) ^ high, low) + self.assertEqual(high ^ (high + low), low) + self.assertEqual((high + low) & (2 * high + low), low) + self.assertEqual((2 * high + low) & (high + low), low) + self.assertEqual((high + low) & (BASE * high + low), low) + self.assertEqual((BASE * high + low) & (high + low), low) + + def test_bitwise_digit_boundaries(self): + values = {0, 1, -1} + for ndigits in (1, 2, 3, 4, 8): + power = BASE ** ndigits + for value in (power - 1, power, power + 1, power + BASE - 1, + power + BASE, 2 * power - 1): + values.update((value, -value)) + + values = sorted(values) + for x in values: + for y in values: + with self.subTest(x=x, y=y): + nbytes = (max(x.bit_length(), y.bit_length()) + 8) // 8 + a = x.to_bytes(nbytes, 'little', signed=True) + b = y.to_bytes(nbytes, 'little', signed=True) + self.assertEqual(x & y, int.from_bytes( + bytes(u & v for u, v in zip(a, b)), + 'little', signed=True)) + self.assertEqual(x | y, int.from_bytes( + bytes(u | v for u, v in zip(a, b)), + 'little', signed=True)) + self.assertEqual(x ^ y, int.from_bytes( + bytes(u ^ v for u, v in zip(a, b)), + 'little', signed=True)) + def slow_format(self, x, base): digits = [] sign = 0 diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-11-14-14-39-19.gh-issue-161075.mK8r2Q.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-14-14-39-19.gh-issue-161075.mK8r2Q.rst new file mode 100644 index 000000000000000..a1f6588b942c734 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-11-14-14-39-19.gh-issue-161075.mK8r2Q.rst @@ -0,0 +1,2 @@ +Reduce memory usage for bitwise ``&`` and ``^`` operations on non-negative +:class:`int` objects when their high bits cancel out. Patch by Shamil Abdulaev. diff --git a/Objects/longobject.c b/Objects/longobject.c index 09bfe4ad4641281..b23563d3472ac3f 100644 --- a/Objects/longobject.c +++ b/Objects/longobject.c @@ -5627,6 +5627,10 @@ long_bitwise(PyLongObject *a, b = z; } + const digit *a_digits = a->long_value.ob_digit; + const digit *b_digits = b->long_value.ob_digit; + int nonnegative = !nega && !negb; + /* JRH: The original logic here was to allocate the result value (z) as the longer of the two operands. However, there are some cases where the result is guaranteed to be shorter than that: AND of two @@ -5638,10 +5642,21 @@ long_bitwise(PyLongObject *a, case '^': negz = nega ^ negb; size_z = size_a; + if (nonnegative && size_a == size_b) { + while (size_z > 0 && a_digits[size_z - 1] == b_digits[size_z - 1]) { + --size_z; + } + } break; case '&': negz = nega & negb; size_z = negb ? size_a : size_b; + if (nonnegative) { + while (size_z > 0 && + (a_digits[size_z - 1] & b_digits[size_z - 1]) == 0) { + --size_z; + } + } break; case '|': negz = nega | negb; @@ -5657,6 +5672,13 @@ long_bitwise(PyLongObject *a, return get_small_int(0); } + if (nonnegative && size_z == 1 && op != '|') { + digit a0 = a_digits[0]; + digit b0 = size_b ? b_digits[0] : 0; + digit d = op == '&' ? a0 & b0 : a0 ^ b0; + return (PyObject *)_PyLong_FromSTwoDigits(d); + } + /* We allow an extra digit if z is negative, to make sure that the final two's complement of z doesn't overflow. */ z = long_alloc(size_z + negz); @@ -5667,18 +5689,20 @@ long_bitwise(PyLongObject *a, } /* Compute digits for overlap of a and b. */ + digit *z_digits = z->long_value.ob_digit; + Py_ssize_t size_overlap = Py_MIN(size_b, size_z); switch(op) { case '&': - for (i = 0; i < size_b; ++i) - z->long_value.ob_digit[i] = a->long_value.ob_digit[i] & b->long_value.ob_digit[i]; + for (i = 0; i < size_overlap; ++i) + z_digits[i] = a_digits[i] & b_digits[i]; break; case '|': - for (i = 0; i < size_b; ++i) - z->long_value.ob_digit[i] = a->long_value.ob_digit[i] | b->long_value.ob_digit[i]; + for (i = 0; i < size_overlap; ++i) + z_digits[i] = a_digits[i] | b_digits[i]; break; case '^': - for (i = 0; i < size_b; ++i) - z->long_value.ob_digit[i] = a->long_value.ob_digit[i] ^ b->long_value.ob_digit[i]; + for (i = 0; i < size_overlap; ++i) + z_digits[i] = a_digits[i] ^ b_digits[i]; break; default: Py_UNREACHABLE(); @@ -5687,16 +5711,19 @@ long_bitwise(PyLongObject *a, /* Copy any remaining digits of a, inverting if necessary. */ if (op == '^' && negb) for (; i < size_z; ++i) - z->long_value.ob_digit[i] = a->long_value.ob_digit[i] ^ PyLong_MASK; + z_digits[i] = a_digits[i] ^ PyLong_MASK; else if (i < size_z) - memcpy(&z->long_value.ob_digit[i], &a->long_value.ob_digit[i], - (size_z-i)*sizeof(digit)); + memcpy(z_digits + i, a_digits + i, (size_z-i)*sizeof(digit)); /* Complement result if negative. */ if (negz) { _PyLong_FlipSign(z); - z->long_value.ob_digit[size_z] = PyLong_MASK; - v_complement(z->long_value.ob_digit, z->long_value.ob_digit, size_z+1); + z_digits[size_z] = PyLong_MASK; + v_complement(z_digits, z_digits, size_z+1); + } + + if (nonnegative) { + return (PyObject *)z; } Py_XDECREF(new_a);