Skip to content

Commit b2f6f04

Browse files
committed
Avoid pow10 calculation on the fast path for small numbers by using a lookup table.
1 parent 3914360 commit b2f6f04

File tree

1 file changed

+12
-1
lines changed

1 file changed

+12
-1
lines changed

src/quicktions.pyx

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,16 +56,27 @@ DEF CACHED_POW10 = 64 # sys.getsizeof(tuple[58]) == 512 bytes in Py3.7
5656

5757
cdef tuple _cache_pow10():
5858
cdef int i
59+
in_ull = True
5960
l = []
6061
x = 1
6162
for i in range(CACHED_POW10):
6263
l.append(x)
64+
if in_ull:
65+
try:
66+
_C_POW_10[i] = x
67+
except OverflowError:
68+
in_ull = False
6369
x *= 10
6470
return tuple(l)
6571

72+
cdef unsigned long long[CACHED_POW10] _C_POW_10
6673
cdef tuple POW_10 = _cache_pow10()
6774

6875

76+
cdef unsigned long long _c_pow10(Py_ssize_t i):
77+
return _C_POW_10[i]
78+
79+
6980
cdef pow10(long long i):
7081
if 0 <= i < CACHED_POW10:
7182
return POW_10[i]
@@ -1668,7 +1679,7 @@ cdef tuple _parse_fraction(AnyString s, Py_ssize_t s_len):
16681679
if state in (SMALL_NUM, SMALL_DECIMAL, SMALL_DECIMAL_DOT, SMALL_END_SPACE):
16691680
# Special case for 'small' numbers: normalise directly in C space.
16701681
if inum and decimal_len:
1671-
idenom = 10 ** <ullong> decimal_len
1682+
idenom = _c_pow10(decimal_len)
16721683
igcd = _c_gcd(inum, idenom)
16731684
if igcd > 1:
16741685
inum //= igcd

0 commit comments

Comments
 (0)