From 6c1b2eea9f0004538c4dd0ea24a25db82884b3c3 Mon Sep 17 00:00:00 2001 From: Takuto Ikuta Date: Wed, 29 Jun 2022 18:56:18 +0900 Subject: [PATCH 1/2] optimize some code paths --- mako/lexer.py | 7 +++---- mako/pygen.py | 24 ++++++++++++++++-------- 2 files changed, 19 insertions(+), 12 deletions(-) diff --git a/mako/lexer.py b/mako/lexer.py index 527c4b5..52798ee 100644 --- a/mako/lexer.py +++ b/mako/lexer.py @@ -74,12 +74,11 @@ def match_reg(self, reg): (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): diff --git a/mako/pygen.py b/mako/pygen.py index 46b0b52..ad14907 100644 --- a/mako/pygen.py +++ b/mako/pygen.py @@ -43,6 +43,13 @@ def __init__(self, stream): # source lines self.source_map = {} + self._re_space_comment = re.compile(r"^\s*#") + self._re_space = re.compile(r"^\s*$") + self._re_indent = re.compile(r":[ \t]*(?:#.*)?$") + self._re_compound = re.compile(r"^\s*(if|try|elif|while|for|with)") + self._re_indent_keyword = re.compile(r"^\s*(def|class|else|elif|except|finally)") + self._re_unindentor = re.compile(r"^\s*(else|elif|except|finally).*\:") + def _update_lineno(self, num): self.lineno += num @@ -86,8 +93,8 @@ def writeline(self, line): if ( line is None - or re.match(r"^\s*#", line) - or re.match(r"^\s*$", line) + or self._re_space_comment.match(line) + or self._re_space.match(line) ): hastext = False else: @@ -121,12 +128,12 @@ def writeline(self, line): # note that a line can both decrase (before printing) and # then increase (after printing) the indentation level. - if re.search(r":[ \t]*(?:#.*)?$", line): + if self._re_indent.search(line): # increment indentation count, and also # keep track of what the keyword was that indented us, # if it is a python compound statement keyword # where we might have to look for an "unindent" keyword - match = re.match(r"^\s*(if|try|elif|while|for|with)", line) + match = self._re_compound.match(line) if match: # its a "compound" keyword, so we will check for "unindentors" indentor = match.group(1) @@ -137,9 +144,7 @@ def writeline(self, line): # its not a "compound" keyword. but lets also # test for valid Python keywords that might be indenting us, # else assume its a non-indenting line - m2 = re.match( - r"^\s*(def|class|else|elif|except|finally)", line - ) + m2 = self._re_indent_keyword.match(line) if m2: self.indent += 1 self.indent_detail.append(indentor) @@ -167,7 +172,7 @@ def _is_unindentor(self, line): # if the current line doesnt have one of the "unindentor" keywords, # return False - match = re.match(r"^\s*(else|elif|except|finally).*\:", line) + match = self._re_unindentor.match(line) # if True, whitespace matches up, we have a compound indentor, # and this line has an unindentor, this # is probably good enough @@ -193,6 +198,9 @@ def _indent_line(self, line, stripspace=""): stripspace is a string of space that will be truncated from the start of the line before indenting.""" + if stripspace == "": + # Fast path optimization. + return self.indentstring * self.indent + line return re.sub( r"^%s" % stripspace, self.indentstring * self.indent, line From bcdee5ccf57100490aa0e48baeda6f15b584ab32 Mon Sep 17 00:00:00 2001 From: Takuto Ikuta Date: Thu, 30 Jun 2022 09:00:10 +0900 Subject: [PATCH 2/2] apply black --- mako/lexer.py | 2 +- mako/pygen.py | 4 +++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/mako/lexer.py b/mako/lexer.py index 52798ee..c45e20e 100644 --- a/mako/lexer.py +++ b/mako/lexer.py @@ -76,7 +76,7 @@ def match_reg(self, reg): self.matched_lineno = self.lineno cp = mp - 1 if cp >= 0 and cp < self.textlength: - cp = self.text[ : cp + 1].rfind("\n") + cp = self.text[: cp + 1].rfind("\n") self.matched_charpos = mp - cp self.lineno += self.text[mp : self.match_position].count("\n") return match diff --git a/mako/pygen.py b/mako/pygen.py index ad14907..7c46535 100644 --- a/mako/pygen.py +++ b/mako/pygen.py @@ -47,7 +47,9 @@ def __init__(self, stream): self._re_space = re.compile(r"^\s*$") self._re_indent = re.compile(r":[ \t]*(?:#.*)?$") self._re_compound = re.compile(r"^\s*(if|try|elif|while|for|with)") - self._re_indent_keyword = re.compile(r"^\s*(def|class|else|elif|except|finally)") + self._re_indent_keyword = re.compile( + r"^\s*(def|class|else|elif|except|finally)" + ) self._re_unindentor = re.compile(r"^\s*(else|elif|except|finally).*\:") def _update_lineno(self, num):