Skip to content

Commit a3e19f1

Browse files
living180andialbrecht
authored andcommitted
Don't make slice copies in TokenList._token_matching().
Since we are working with indexes anyway, don't bother calling enumerate() with a slice from self.tokens (which requires copying memory). Instead, just generate the indexes using range() and use normal indexing to access the desired tokens. The old behavior resulted in quadratic runtime with respect to the number of tokens, which significantly impacted performance for statements with very large numbers of tokens. With the new behavior, the runtime is now linear with respect to the number of tokens.
1 parent 4073b56 commit a3e19f1

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

sqlparse/sql.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,10 @@ def _token_matching(self, funcs, start=0, end=None, reverse=False):
240240
if func(token):
241241
return idx, token
242242
else:
243-
for idx, token in enumerate(self.tokens[start:end], start=start):
243+
if end is None:
244+
end = len(self.tokens)
245+
for idx in range(start, end):
246+
token = self.tokens[idx]
244247
for func in funcs:
245248
if func(token):
246249
return idx, token

0 commit comments

Comments
 (0)