From a3e19f1cbdf929eb45dc0e22d116b466d7b985a1 Mon Sep 17 00:00:00 2001 From: Daniel Harding Date: Wed, 19 May 2021 13:31:19 +0300 Subject: 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. --- sqlparse/sql.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/sqlparse/sql.py b/sqlparse/sql.py index 6a32c26..19c2ed4 100644 --- a/sqlparse/sql.py +++ b/sqlparse/sql.py @@ -240,7 +240,10 @@ class TokenList(Token): if func(token): return idx, token else: - for idx, token in enumerate(self.tokens[start:end], start=start): + if end is None: + end = len(self.tokens) + for idx in range(start, end): + token = self.tokens[idx] for func in funcs: if func(token): return idx, token -- cgit v1.2.1