Skip to content

Commit

Permalink
Reduce cutoff for calling the new implementation from 3.5M to 2M.
Browse files Browse the repository at this point in the history
It could probably be reduced more as-is, and more still with more
fine-tuning. This was remarkably easy: it just required inner()
to let Python convert much larger strings to ints, so large that
the existing Karatsuba int(str) usually gets invoked.
  • Loading branch information
tim-one committed May 18, 2024
1 parent a288adc commit 9ee61f6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
9 changes: 7 additions & 2 deletions Lib/_pylong.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,12 @@ def inner(a, b):
del defaultdict

def _dec_str_to_int_inner(s, *, GUARD=8):
BYTELIM = 512
# Yes, BYTELIM is "large". Large enough that CPython will usually
# use the Karatsuba _str_to_int_inner to convert the string. This
# allowed reducing the cutoff for calling _this_ function from 3.5M
# to 2M digits. We could almost certainly do even better by
# fine-tuning this and/or using a larger output base than 256.
BYTELIM = 100_000
D = decimal.Decimal
result = bytearray()
# See notes at end of file for discussion of GUARD.
Expand Down Expand Up @@ -389,7 +394,7 @@ def int_from_string(s):
# contain underscores and have trailing whitespace.
s = s.rstrip().replace('_', '')
func = _str_to_int_inner
if len(s) >= 3_500_000 and _decimal is not None:
if len(s) >= 2_000_000 and _decimal is not None:
func = _dec_str_to_int_inner
return func(s)

Expand Down
4 changes: 2 additions & 2 deletions Lib/test/test_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -938,8 +938,8 @@ def test_whitebox_dec_str_to_int_inner_failsafe(self):
# wrong about that. We have no input that reaches that block.
# Here we test a contrived input that _does_ reach that block,
# provided the number of guard digits is reduced to 1.
sn = "6" * (4000000 - 1)
n = (10**len(sn) - 1) // 9 * 6
sn = "9" * 2000156
n = 10**len(sn) - 1
orig_spread = _pylong._spread.copy()
_pylong._spread.clear()
try:
Expand Down

0 comments on commit 9ee61f6

Please sign in to comment.