5656import time
5757import tokenize
5858import warnings
59+ import bisect
5960
6061from fnmatch import fnmatch
6162from 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