1
0
mirror of https://github.com/django/django.git synced 2025-03-10 17:32:41 +00:00

Improved performance of django.template.base.Parser.

pop(0), which is used to fetch each token, is O(n) in the length of the
list. By reversing the list and operating off the end, we can perform
next_token(), prepend_token(), and delete_first_token() in constant
time.
This commit is contained in:
Alex Gaynor 2019-10-08 22:25:20 -04:00 committed by Mariusz Felisiak
parent dafdfd6a60
commit 04ac9b45a3

View File

@ -405,7 +405,9 @@ class DebugLexer(Lexer):
class Parser: class Parser:
def __init__(self, tokens, libraries=None, builtins=None, origin=None): def __init__(self, tokens, libraries=None, builtins=None, origin=None):
self.tokens = tokens # Reverse the tokens so delete_first_token(), prepend_token(), and
# next_token() can operate at the end of the list in constant time.
self.tokens = list(reversed(tokens))
self.tags = {} self.tags = {}
self.filters = {} self.filters = {}
self.command_stack = [] self.command_stack = []
@ -541,13 +543,13 @@ class Parser:
raise self.error(token, msg) raise self.error(token, msg)
def next_token(self): def next_token(self):
return self.tokens.pop(0) return self.tokens.pop()
def prepend_token(self, token): def prepend_token(self, token):
self.tokens.insert(0, token) self.tokens.append(token)
def delete_first_token(self): def delete_first_token(self):
del self.tokens[0] del self.tokens[-1]
def add_library(self, lib): def add_library(self, lib):
self.tags.update(lib.tags) self.tags.update(lib.tags)