Skip to content

Commit 83782f6

Browse files
committed
Use bisect instead of iterating over every offsets.
As the offsets looks already sorted (which looks logical so I assumed they always are), using a bisection if faster than iterating over all of them. On a specific test I encontered, I got nice enhancement with this patch: $ time python3 ./pycodestyle.py ~/Downloads/PmagPy/PmagPy/coefficients-552K.py > /dev/null real 1m16.405s $ time python3 ./pycodestyle.py ~/Downloads/PmagPy/PmagPy/coefficients-552K.py > /dev/null real 0m3.318s
1 parent 54a11aa commit 83782f6

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

pycodestyle.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@
5656
import time
5757
import tokenize
5858
import warnings
59+
import bisect
5960

6061
from fnmatch import fnmatch
6162
from optparse import OptionParser
@@ -1664,10 +1665,10 @@ def check_logical(self):
16641665
"""Build a line from tokens and run all logical checks on it."""
16651666
self.report.increment_logical_line()
16661667
mapping = self.build_tokens_line()
1667-
16681668
if not mapping:
16691669
return
16701670

1671+
mapping_offsets = [offset for offset, _ in mapping]
16711672
(start_row, start_col) = mapping[0][1]
16721673
start_line = self.lines[start_row - 1]
16731674
self.indent_level = expand_indent(start_line[:start_col])
@@ -1681,9 +1682,10 @@ def check_logical(self):
16811682
self.init_checker_state(name, argument_names)
16821683
for offset, text in self.run_check(check, argument_names) or ():
16831684
if not isinstance(offset, tuple):
1684-
for token_offset, pos in mapping:
1685-
if offset <= token_offset:
1686-
break
1685+
# As mappings are ordered, bisecting is a fast way
1686+
# to find a given offset in them.
1687+
token_offset, pos = mapping[bisect.bisect_left(
1688+
mapping_offsets, offset)]
16871689
offset = (pos[0], pos[1] + offset - token_offset)
16881690
self.report_error(offset[0], offset[1], text, check)
16891691
if self.logical_line:

0 commit comments

Comments
 (0)