Skip to content

Commit

Permalink
perf: Cache more things
Browse files Browse the repository at this point in the history
  • Loading branch information
JCWasmx86 committed Dec 31, 2024
1 parent 7b1a7b9 commit 9139b22
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 7 deletions.
8 changes: 4 additions & 4 deletions djlint/formatter/indent.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def fix_handlebars_template_tags(
# handlebars templates
rawcode = regex_utils.sub(r"({{#(?:each|if).+?[^ ])(}})", func, rawcode)

rawcode_flat_list = regex_utils.split("\n", rawcode)
rawcode_flat_list = rawcode.split("\n")

indent = config.indent

Expand Down Expand Up @@ -123,7 +123,7 @@ def fix_handlebars_template_tags(
) or (
not is_block_raw
and (
regex_utils.search(
regex_utils.search_cached(
rf"""^(?:[^<\s].*?)? # start of a line, optionally with some text
(?:
<({slt_html})(?:(?:>|\b[^>]+?>)(?:.*?)(?:</(?:\1)>)|\b[^>]*?/>) # <span stuff-or-not>stuff</span> or <img stuff /> >>> match 1
Expand Down Expand Up @@ -237,12 +237,12 @@ def fix_handlebars_template_tags(
flags=RE_FLAGS_IMX,
)
) or (
regex_utils.search(
not is_block_raw
and regex_utils.search(
r"^(?:" + str(config.tag_indent) + r")",
item,
flags=RE_FLAGS_IMX,
)
and not is_block_raw
):
tmp = (indent * indent_level) + item + "\n"
indent_level += 1
Expand Down
6 changes: 4 additions & 2 deletions djlint/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def is_ignored_block_opening(config: Config, item: str) -> bool:
else 0
)
return bool(
regex_utils.search(
regex_utils.search_cached(
config.ignored_block_opening, item[last_index:], flags=RE_FLAGS_IX
)
)
Expand Down Expand Up @@ -95,6 +95,8 @@ def inside_protected_trans_block(
True = non indentable > inside ignored trans block
False = indentable > either inside a trans trimmed block, or somewhere else, but not a trans non trimmed :)
"""
if "endblocktrans" not in match.group():
return False
close_block = regex_utils.search(
config.ignored_trans_blocks_closing, match.group(), flags=RE_FLAGS_IX
)
Expand Down Expand Up @@ -173,7 +175,7 @@ def is_ignored_block_closing(config: Config, item: str) -> bool:
else 0
)
return bool(
regex_utils.search(
regex_utils.search_cached(
config.ignored_block_closing, item[last_index:], flags=RE_FLAGS_IX
)
)
Expand Down
7 changes: 7 additions & 0 deletions djlint/regex_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ def search(
return _compile_cached(pattern, flags=flags).search(string)


@cache
def search_cached(
pattern: str, string: str, /, *, flags: int = 0
) -> re.Match[str] | None:
return _compile_cached(pattern, flags=flags).search(string)


def sub(
pattern: str,
repl: str | Callable[[re.Match[str]], str],
Expand Down
2 changes: 1 addition & 1 deletion djlint/rules/H025.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def run(
orphan_tags: list[re.Match[str]] = []
regex_str = r"<(/?(\w+))\s*(" + config.attribute_pattern + r"|\s*)*\s*?>"
for match in regex_utils.finditer(regex_str, html, flags=re.X):
if match.group(1) and not regex_utils.search(
if match.group(1) and not regex_utils.search_cached(
rf"^/?{config.always_self_closing_html_tags}\b",
match.group(1),
flags=RE_FLAGS_IX,
Expand Down

0 comments on commit 9139b22

Please sign in to comment.