Skip to content

Compile non-strict templates Ractor-locally - #62

Open
etiennebarrie wants to merge 3 commits into
actionview-eager-load-templatesfrom
actionview-frozen-non-strict-templates
Open

Compile non-strict templates Ractor-locally#62
etiennebarrie wants to merge 3 commits into
actionview-eager-load-templatesfrom
actionview-frozen-non-strict-templates

Conversation

@etiennebarrie

Copy link
Copy Markdown
Member

Follow-up to #57, allows using non-strict templates from Ractors (mostly, we need AS::Notifications to be shareable).

While this is not advisable, this helps on the path to view rendering ractor safety without requiring strict-locals templates.

@etiennebarrie
etiennebarrie force-pushed the actionview-eager-load-templates branch from 4d95b86 to b262f80 Compare August 4, 2026 15:42
@etiennebarrie
etiennebarrie force-pushed the actionview-frozen-non-strict-templates branch from 7f94f80 to 0940ce5 Compare August 4, 2026 15:43

# Do not escape templates of these mime types.
class_attribute :escape_ignore_list, default: ["text/plain"]
class_attribute :escape_ignore_list, default: ["text/plain"].freeze

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was solved upstream with an after_initialize freeze I believe.

# freeze from leaking into other tests.
ActiveSupport::Ractors.make_shareable(Mime.instance_variable_get(:@lookup_by_extension))

r = ActionView::FileSystemResolver.new(dir)

@gmcgibbon gmcgibbon Aug 5, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
r = ActionView::FileSystemResolver.new(dir)
resolver = ActionView::FileSystemResolver.new(dir)

Minor comments. I think we can open these upstream.

etiennebarrie and others added 3 commits August 5, 2026 15:59
On Rubies before Ractor.store_if_absent (3.4) the same contract is
provided on top of Ractor-local storage.

Also dropping defined?(Ractor) from the feature check since the fallback
branch calls Ractor.current, so it assumes CRuby, where Ractor is always
defined.

Co-authored-by: Gannon McGibbon <gannon.mcgibbon@gmail.com>
Compiling an ERB template reads two pieces of class state that
non-main Ractors could not access: the ENCODING_TAG Regexp (built with
Regexp.new, so not frozen) and annotate_rendered_view_with_filenames
(a class variable via cattr_accessor, unreadable from non-main Ractors
regardless of value; now a class_attribute, which stores a class ivar
and keeps the same class-level accessors). The escape_ignore_list it
also reads is frozen at boot by the railtie by an after_initialize hook in the railtie.
UnboundTemplate#freeze required strict locals because a single template
can serve every locals set only when the template ignores render-time
locals. Without strict locals, one template exists per locals set,
which cannot be known at boot: freeze now keeps the unbound shell
frozen and shareable, and bind_locals builds unfrozen templates on
demand in a Ractor-local two-level cache (store_if_absent, keyed by the
unbound template then by normalized locals). Each Ractor compiles a
locals set at most once, mirroring the per-process behavior of the
unfrozen cache.

Requires concurrent-ruby 1.3.8, which froze the Concurrent::NULL
sentinel that every Concurrent::Map operation reads.

The Ractor test compiles through the private compile method:
Notifications does not support instrumenting from non-main Ractors
yet, which is the next blocker for worker-side compile! and render.

With non-strict templates freezable, ractorize! now also freezes every
file system resolver after eager loading and compiling it, making the
template caches shareable for stock applications.
@etiennebarrie
etiennebarrie force-pushed the actionview-eager-load-templates branch from b262f80 to 93519a3 Compare August 6, 2026 10:33
@etiennebarrie
etiennebarrie force-pushed the actionview-frozen-non-strict-templates branch from 0940ce5 to 4f4d19a Compare August 6, 2026 10:33
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.

2 participants