From 83782f6cb617ef10186af270c05bfc87a4b4d0bb Mon Sep 17 00:00:00 2001 From: Julien Palard Date: Sat, 6 May 2017 16:39:34 +0200 Subject: 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 --- pycodestyle.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/pycodestyle.py b/pycodestyle.py index 2e03301..763747e 100755 --- a/pycodestyle.py +++ b/pycodestyle.py @@ -56,6 +56,7 @@ import sys import time import tokenize import warnings +import bisect from fnmatch import fnmatch from optparse import OptionParser @@ -1664,10 +1665,10 @@ class Checker(object): """Build a line from tokens and run all logical checks on it.""" self.report.increment_logical_line() mapping = self.build_tokens_line() - if not mapping: return + mapping_offsets = [offset for offset, _ in mapping] (start_row, start_col) = mapping[0][1] start_line = self.lines[start_row - 1] self.indent_level = expand_indent(start_line[:start_col]) @@ -1681,9 +1682,10 @@ class Checker(object): self.init_checker_state(name, argument_names) for offset, text in self.run_check(check, argument_names) or (): if not isinstance(offset, tuple): - for token_offset, pos in mapping: - if offset <= token_offset: - break + # As mappings are ordered, bisecting is a fast way + # to find a given offset in them. + token_offset, pos = mapping[bisect.bisect_left( + mapping_offsets, offset)] offset = (pos[0], pos[1] + offset - token_offset) self.report_error(offset[0], offset[1], text, check) if self.logical_line: -- cgit v1.2.1