Skip to content

fix: share template function maps via base template to avoid per-execution copying#24

Merged
bflad merged 1 commit into
mainfrom
bflad/reduce-function-map-copying
Mar 5, 2026
Merged

fix: share template function maps via base template to avoid per-execution copying#24
bflad merged 1 commit into
mainfrom
bflad/reduce-function-map-copying

Conversation

@bflad
Copy link
Copy Markdown
Member

@bflad bflad commented Mar 5, 2026

Summary

Each call to execTemplate previously called template.New(name).Funcs(TmplFuncs), which copies every function entry and calls reflect.ValueOf on each one. With 100+ functions (sprig + custom), this dominated allocations in high-volume workloads (~784MB in a moderate sized source profile).

A shared base template is now created once and reused via baseTemplate.New(name), which shares the internal function maps by pointer through Go's text/template common struct. The base template is rebuilt only when template functions are registered or unregistered at runtime.

Test plan

  • All existing unit and integration tests pass

…ution copying

Each call to execTemplate previously called template.New(name).Funcs(TmplFuncs),
which copies every function entry and calls reflect.ValueOf on each one. With 100+
functions (sprig + custom), this dominated allocations in high-volume workloads
(~784MB in a moderate sized source profile).

A shared base template is now created once and reused via baseTemplate.New(name),
which shares the internal function maps by pointer through Go's text/template
common struct. The base template is rebuilt only when template functions are
registered or unregistered at runtime.
@bflad bflad merged commit 134012c into main Mar 5, 2026
1 of 2 checks passed
@bflad bflad deleted the bflad/reduce-function-map-copying branch March 5, 2026 13:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants