Engine: Compute relative file paths lazily - #2262
Merged
marcoroth merged 2 commits intoAug 17, 2026
Merged
Conversation
Avoid normalizing each template filename during context construction when compilation does not consume the relative path. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: ccc00bcf-145f-4f65-8675-5d004f02dd98
joelhawksley
marked this pull request as ready for review
August 17, 2026 20:20
marcoroth
reviewed
Aug 17, 2026
Cache the lazily derived path through a private mutable holder so VisitorContext can remain frozen. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: 30890d9a-a37a-4e59-b7bc-f23153d5768a
joelhawksley
commented
Aug 17, 2026
| @file_path = self.class.coerce_file_path(file_path) | ||
| @project_path = self.class.coerce_project_path(project_path) | ||
| @relative_file_path = self.class.derive_relative_file_path(@file_path, @project_path) | ||
| @relative_file_path_cache = [] #: Array[String] |
Contributor
Author
There was a problem hiding this comment.
@marcoroth this approach keeps VisitorContext frozen, FYI.
marcoroth
approved these changes
Aug 17, 2026
marcoroth
left a comment
Owner
There was a problem hiding this comment.
Thank you @joelhawksley! 馃檹馃徏
marcoroth
enabled auto-merge (squash)
August 17, 2026 21:28
This was referenced Aug 17, 2026
marcoroth
added a commit
that referenced
this pull request
Aug 20, 2026
This pull request stops `VisitorContext` from asking the operating system where it is for every template it builds a context for. `coerce_project_path` falls back to `Pathname.new(Dir.pwd)` when no project path is given, and it ran during construction. The project path is only read when a visitor, a diagnostic, an overlay or the relative file path asks for it, so a template compiled without any of those still paid for a `getcwd` and two allocations. Deriving it on demand takes about 7% off compile time over the `examples/` corpus, measured A/B interleaved against `main`. The value is unchanged. `coerce_project_path` keeps its behavior for a caller that passes a path, and the derived one is cached through the same mutable holder #2262 introduced for the relative file path, so the context can stay frozen. Follow-up to #2262, which left this the last eagerly computed field on the context.
marcoroth
added a commit
that referenced
this pull request
Aug 20, 2026
This pull request stops `VisitorContext` from asking the operating system where it is for every template it builds a context for. `coerce_project_path` falls back to `Pathname.new(Dir.pwd)` when no project path is given, and it ran during construction. The project path is only read when a visitor, a diagnostic, an overlay or the relative file path asks for it, so a template compiled without any of those still paid for a `getcwd` and two allocations. Deriving it on demand takes about 7% off compile time over the `examples/` corpus. The value is unchanged. `coerce_project_path` keeps its behavior for a caller that passes a path, and the derived one is cached through the same mutable holder #2262 introduced for the relative file path, so the context can stay frozen. Follow-up to #2262.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Compute
VisitorContext#relative_file_pathon demand instead of eagerly duringcontext construction.
Herb::Enginecreates a context for every template, but the relative path isprimarily consumed by diagnostics, overlays, debug output, and visitors that
explicitly request it. Valid templates compiled without those features
previously still paid for
Pathname#absolute?, path joining,Pathname#relative_path_from, and#to_s.The public behavior is unchanged:
relative_file_path, context hash access,merging, inspection, and serialization still return the same value. A
regression test verifies that derivation does not happen during initialization.
Split out of #1872.
Benchmark
Measured
Herb::Enginecompilation overmarcoroth/herb-corpusat5560d823, using Ruby 4.0.2.The benchmark compiled the 36,046 corpus templates accepted by both variants
with a filename and project path,
escape: true, no visitors, non-strictparsing, and Ruby validation disabled. Results are medians of three full-corpus
runs:
main(cc3eb8bc)Generated Ruby was byte-identical in every run
(
aee36910bcd428792272440a7d39cabcd14c641544c6e35e2b610a14fbd9892b).