Repository navigation
Conversation
This comment was marked as resolved.
This comment was marked as resolved.
skirpichev
approved these changes
Oct 25, 2026
Member
|
CC @picnixz |
Comment on lines
+782
to
+786
| n = (1 << 100_000) | ||
| self.assertEqual((n * 3 + 1) % n, 1) | ||
| self.assertEqual((-n * 3 - 1) % n, n - 1) | ||
| self.assertEqual((n * 3 + 1) % -n, 1 - n) | ||
| self.assertEqual((-n * 3 - 1) % -n, -1) |
Member
There was a problem hiding this comment.
Suggested change
| n = (1 << 100_000) | |
| self.assertEqual((n * 3 + 1) % n, 1) | |
| self.assertEqual((-n * 3 - 1) % n, n - 1) | |
| self.assertEqual((n * 3 + 1) % -n, 1 - n) | |
| self.assertEqual((-n * 3 - 1) % -n, -1) | |
| n = (1 << 100_000) | |
| m = (1 << 50_000) | |
| self.assertEqual((n * 3 + 1) % m, 1) | |
| self.assertEqual((-n * 3 - 1) % m, m - 1) | |
| self.assertEqual((n * 3 + 1) % -m, 1 - m) | |
| self.assertEqual((-n * 3 - 1) % -m, -1) |
We don't reach the path currently, at n = 1 << 100_000, both n*3 + 1 and n are 3334 digits, so size_v - size_w is 0 and the threshold of 150 is never met?
Author
There was a problem hiding this comment.
Good catch! Applied the suggestion to ensure size_v - size_w > 150 and size_w > 300 thresholds are properly exercised. Thanks!
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #160050.
In
Objects/longobject.c,l_divmodswitches topylong_int_divmod()when operands exceed the threshold (size_w > 300 && (size_v - size_w) > 150), providing asymptotically faster division. However,l_mod()bypassed this fast path and fell through directly tolong_rem(), resulting in integer modulo (%) taking significantly longer thandivmod(a, b)[1]for large integers.This patch adds the same
WITH_PYLONG_MODULEthreshold check inl_mod(), invokingpylong_int_divmod(v, w, NULL, pmod)when sizes exceed the threshold.Benchmark
Using the benchmark from the issue with operands where
bits = 128_000://= 0.0105s,%= 0.0224s,divmod= 0.0083s (modulo ~2.1x slower than integer division)//= 0.0088s,%= 0.0093s,divmod= 0.0098s (modulo has matching complexity)Testing
test_pylong_int_modinLib/test/test_int.py.test_pylong_int_divmod_crashto cover%../PCbuild/amd64/python.exe -m test test_int test_long.