Repository navigation
Conversation
skirpichev
approved these changes
Oct 25, 2026
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.