Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20230424-163611.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Optimize template rendering for common parse scenarios
time: 2023-04-24T16:36:11.24088-04:00
custom:
Author: peterallenwebb
Issue: "7449"
20 changes: 17 additions & 3 deletions core/dbt/clients/jinja.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,8 @@ def _requote_result(raw_value: str, rendered: str) -> str:
# is small enough that I've just chosen the more readable option.
_HAS_RENDER_CHARS_PAT = re.compile(r"({[{%#]|[#}%]})")

_render_cache: Dict[str, Any] = dict()


def get_rendered(
string: str,
Expand All @@ -579,16 +581,28 @@ def get_rendered(
# If this is desirable in the native env as well, we could handle the
# native=True case by passing the input string to ast.literal_eval, like
# the native renderer does.
if not native and isinstance(string, str) and _HAS_RENDER_CHARS_PAT.search(string) is None:
return string
has_render_chars = _HAS_RENDER_CHARS_PAT.search(string)

if not has_render_chars:
if not native:
return string
elif string in _render_cache:
return _render_cache[string]

template = get_template(
string,
ctx,
node,
capture_macros=capture_macros,
native=native,
)
return render_template(template, ctx, node)

rendered = render_template(template, ctx, node)

if not has_render_chars and native:
_render_cache[string] = rendered

return rendered


def undefined_error(msg) -> NoReturn:
Expand Down