summaryrefslogtreecommitdiff
path: root/mako/lexer.py
diff options
context:
space:
mode:
authorTakuto Ikuta <tikuta@google.com>2022-06-30 10:40:05 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2022-06-30 10:59:44 -0400
commit4279e6538dc3ae2fa454f70a4fc6438cf306dff9 (patch)
tree87c94b1c924f6a4cf4c26e277ad3763892f09acd /mako/lexer.py
parent0d68060725e2ff206aa4c251931b1c023a54483d (diff)
downloadmako-4279e6538dc3ae2fa454f70a4fc6438cf306dff9.tar.gz
optimize some code paths
Optimized some codepaths within the lexer/Python code generation process, improving performance for generation of templates prior to their being cached. Pull request courtesy Takuto Ikuta. This shows around 10% performance improvement in our use case (https://crbug.com/1214033#c32). Closes: #361 Pull-request: https://github.com/sqlalchemy/mako/pull/361 Pull-request-sha: bcdee5ccf57100490aa0e48baeda6f15b584ab32 Change-Id: If647f77a52d5745019dcc46f82fd7a928f990757
Diffstat (limited to 'mako/lexer.py')
-rw-r--r--mako/lexer.py7
1 files changed, 3 insertions, 4 deletions
diff --git a/mako/lexer.py b/mako/lexer.py
index 527c4b5..c45e20e 100644
--- a/mako/lexer.py
+++ b/mako/lexer.py
@@ -74,12 +74,11 @@ class Lexer:
(start, end) = match.span()
self.match_position = end + 1 if end == start else end
self.matched_lineno = self.lineno
- lines = re.findall(r"\n", self.text[mp : self.match_position])
cp = mp - 1
- while cp >= 0 and cp < self.textlength and self.text[cp] != "\n":
- cp -= 1
+ if cp >= 0 and cp < self.textlength:
+ cp = self.text[: cp + 1].rfind("\n")
self.matched_charpos = mp - cp
- self.lineno += len(lines)
+ self.lineno += self.text[mp : self.match_position].count("\n")
return match
def parse_until_text(self, watch_nesting, *text):