Skip to content

Commit 2183302

Browse files
committed
Avoid raising costly exceptions for large numbers in the GCD Python fallback code.
Integer comparisons still tend to be faster than creating a new exception, raising and catching it.
1 parent 18c6b5e commit 2183302

1 file changed

Lines changed: 7 additions & 14 deletions

File tree

src/quicktions.pyx

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,6 @@ cdef _gcd_fallback(a: int, b: int):
499499
"""
500500
# Try doing the computation in C space. If the numbers are too
501501
# large at the beginning, do object calculations until they are small enough.
502-
cdef ullong au, bu
503502
cdef long long ai, bi
504503

505504
if HAS_ISLONGLONG:
@@ -508,26 +507,18 @@ cdef _gcd_fallback(a: int, b: int):
508507
au = _abs(ai)
509508
bu = _abs(bi)
510509
return _py_gcd(au, bu)
511-
else:
512-
# Optimistically try to switch to C space.
513-
try:
514-
ai, bi = a, b
515-
except OverflowError:
516-
pass
517-
else:
518-
au = _abs(ai)
519-
bu = _abs(bi)
520-
return _py_gcd(au, bu)
521510

522511
# Do object calculation until we reach the C space limit.
523512
a = abs(a)
524513
b = abs(b)
525514
while b > PY_MAX_ULONGLONG:
526515
a, b = b, a%b
527-
while b and a > PY_MAX_ULONGLONG:
528-
a, b = b, a%b
529-
if not b:
516+
if b == 0:
530517
return a
518+
while a > PY_MAX_ULONGLONG:
519+
a, b = b, a%b
520+
if b == 0:
521+
return a
531522
return _py_gcd(a, b)
532523

533524

@@ -1872,6 +1863,8 @@ cdef inline int _parse_digit(char** c_digits, Py_UCS4 c, int allow_unicode) noex
18721863
if unum > 9:
18731864
if not allow_unicode:
18741865
return -1
1866+
if c < 1632:
1867+
return -1
18751868
num = _to_decimal(c)
18761869
if num == -1:
18771870
return -1

0 commit comments

Comments
 (0)