Skip to content

Commit 630d4c2

Browse files
reduce len() calls (#412)
1 parent e03f486 commit 630d4c2

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

src/rapidfuzz/fuzz_py.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -316,17 +316,18 @@ def partial_ratio_alignment(
316316

317317
if not s1 and not s2:
318318
return ScoreAlignment(100.0, 0, 0, 0, 0)
319-
320319
s1, s2 = conv_sequences(s1, s2)
321-
if len(s1) <= len(s2):
320+
len1 = len(s1)
321+
len2 = len(s2)
322+
if len1 <= len2:
322323
shorter = s1
323324
longer = s2
324325
else:
325326
shorter = s2
326327
longer = s1
327328

328329
res = _partial_ratio_impl(shorter, longer, score_cutoff / 100)
329-
if res.score != 100 and len(s1) == len(s2):
330+
if res.score != 100 and len1 == len2:
330331
score_cutoff = max(score_cutoff, res.score)
331332
res2 = _partial_ratio_impl(longer, shorter, score_cutoff / 100)
332333
if res2.score > res.score:
@@ -335,7 +336,7 @@ def partial_ratio_alignment(
335336
if res.score < score_cutoff:
336337
return None
337338

338-
if len(s1) <= len(s2):
339+
if len1 <= len2:
339340
return res
340341

341342
return ScoreAlignment(res.score, res.dest_start, res.dest_end, res.src_start, res.src_end)

src/rapidfuzz/process_cpp_impl.pyx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,7 @@ def extract(query, choices, *, scorer=WRatio, processor=None, limit=5, score_cut
12081208
cdef RF_Scorer* scorer_context = NULL
12091209
cdef RF_ScorerFlags scorer_flags
12101210
cdef int64_t c_limit
1211+
cdef int64_t choices_len
12111212
scorer_kwargs = scorer_kwargs.copy() if scorer_kwargs else {}
12121213

12131214
setupPandas()
@@ -1216,16 +1217,17 @@ def extract(query, choices, *, scorer=WRatio, processor=None, limit=5, score_cut
12161217
return []
12171218

12181219
try:
1219-
if limit is None or limit > len(choices):
1220-
limit = len(choices)
1220+
choices_len = <int64_t>len(choices)
12211221
except TypeError:
12221222
# handle generators. In Theory we could retrieve the length later on while
12231223
# preprocessing the choices, but this is good enough for now
12241224
choices = list(choices)
1225-
if limit is None or limit > len(choices):
1226-
limit = len(choices)
1225+
choices_len = <int64_t>len(choices)
1226+
1227+
c_limit = choices_len
1228+
if limit is not None:
1229+
c_limit = min(c_limit, <int64_t>limit)
12271230

1228-
c_limit = limit
12291231
if c_limit == 1:
12301232
res = extractOne(
12311233
query,

src/rapidfuzz/process_py.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -643,14 +643,17 @@ def cpdist(
643643
"""
644644
import numpy as np
645645

646-
if len(queries) != len(choices):
646+
len_queries = len(queries)
647+
len_choices = len(choices)
648+
649+
if len_queries != len_choices:
647650
error_message = "Length of queries and choices must be the same!"
648651
raise ValueError(error_message)
649652

650653
_ = workers, score_hint
651654
scorer_kwargs = scorer_kwargs or {}
652655
dtype = _dtype_to_type_num(dtype, scorer, scorer_kwargs)
653-
results = np.zeros((len(queries),), dtype=dtype)
656+
results = np.zeros((len_queries,), dtype=dtype)
654657

655658
setupPandas()
656659

0 commit comments

Comments
 (0)