diff options
author | Daniel Harding <dharding@living180.net> | 2021-05-19 13:31:19 +0300 |
---|---|---|
committer | Andi Albrecht <albrecht.andi@gmail.com> | 2022-08-08 10:52:33 +0200 |
commit | a3e19f1cbdf929eb45dc0e22d116b466d7b985a1 (patch) | |
tree | 5cd51c3fedd668fd2bf610a5fdafa4ad6c7377f3 /sqlparse/sql.py | |
parent | 4073b569428036259a093dcbfe0f67ac3ef3a1e0 (diff) | |
download | sqlparse-a3e19f1cbdf929eb45dc0e22d116b466d7b985a1.tar.gz |
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.
Diffstat (limited to 'sqlparse/sql.py')
-rw-r--r-- | sqlparse/sql.py | 5 |
1 files changed, 4 insertions, 1 deletions
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 |