Skip to content

Eager load templates#57

Open
etiennebarrie wants to merge 2 commits into
mainfrom
actionview-eager-load-templates
Open

Eager load templates#57
etiennebarrie wants to merge 2 commits into
mainfrom
actionview-eager-load-templates

Conversation

@etiennebarrie

Copy link
Copy Markdown
Member

This gives a way to load all templates at boot time, and make them shareable.
For now only strict locals templates are supported, since they allow folding the UnboundTemplate.@templates cache into a single Template object.

Add FileSystemResolver#eager_load_templates, which globs the view path
once and populates the unbound-template cache up front instead of lazily
on first lookup. The cache stays mutable, so binding new locals at render
time keeps working exactly as before; this only warms the cache.

Also extract the lookup out of #_find_all into #unbound_templates_for and
drop the throwaway Concurrent::Map that was allocated on every uncached
lookup, computing directly instead.
Give Template, UnboundTemplate, the file source, and the resolver's
PathParser a #freeze that turns them into deeply-frozen, Ractor-shareable
objects, and add FileSystemResolver#freeze to freeze the whole eager-built
cache. Combined with #eager_load_templates, a resolver can be built and
then frozen so its templates can be shared across Ractors.

Template#freeze memoizes the method name, strict-locals declaration and
compiled source, then drops the compile mutex. UnboundTemplate collapses to
a single @strict_locals_template once it discovers the template is strict,
rather than a defaulting Hash, which makes freezing straightforward.

Freezing requires strict locals: a non-strict template compiles a distinct
method per set of locals, so it can't be reduced to one shareable template.
UnboundTemplate#freeze therefore raises for any template that hasn't
declared its locals. Support for freezing non-strict templates and partials
is left to a follow-up.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant