summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulien Palard <julien@palard.fr>2017-05-06 16:39:34 +0200
committerJulien Palard <julien@palard.fr>2017-05-14 10:04:33 +0200
commit83782f6cb617ef10186af270c05bfc87a4b4d0bb (patch)
treed0485f78a9564c3830fdb3940fc5a13a7073b0a8
parent54a11aa5b8aec78d29048093cf1e8d3af6819ad2 (diff)
downloadpep8-83782f6cb617ef10186af270c05bfc87a4b4d0bb.tar.gz
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
-rwxr-xr-xpycodestyle.py10
1 files 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: