perf: lazily initialize Handlebars and SyntaxHighlighter to reduce startup time#2739
Merged
tusharmath merged 2 commits intomainfrom Mar 30, 2026
Merged
perf: lazily initialize Handlebars and SyntaxHighlighter to reduce startup time#2739tusharmath merged 2 commits intomainfrom
tusharmath merged 2 commits intomainfrom
Conversation
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.
Summary
Defer initialization of the Handlebars template engine and syntax highlighter to their first use, reducing application startup time by avoiding expensive upfront work.
Context
ForgeTemplateServiceandMarkdownFormatpreviously initialized their heaviest dependencies — the Handlebars engine and theSyntaxHighlighter— eagerly at construction time. These initializations are non-trivial in cost and are not always needed immediately (or at all) during a given session, so paying for them unconditionally at startup was wasteful.Changes
ForgeTemplateService: Replaced the eagerly-initializedArc<RwLock<Handlebars>>withArc<OnceCell<RwLock<Handlebars>>>. Added a privateget_hb()async helper that creates the Handlebars instance on the first call and returns a reference to it on all subsequent calls. Thenew()constructor now does no heavy work.MarkdownFormat: Changed thehighlighterfield from a plainSyntaxHighlighterto aOnceLock<SyntaxHighlighter>. The highlighter is created viaget_or_initthe first timeformat()is called, leaving construction ofMarkdownFormatitself lightweight.Key Implementation Details
Both changes use the standard lazy-initialization primitives idiomatic to their execution context:
tokio::sync::OnceCellfor the asyncForgeTemplateService(supports.awaitin the initializer closure).std::sync::OnceLockfor the synchronousMarkdownFormat(no async required).The public API of both types is unchanged — callers see no difference in behavior.
Testing
Fixes #2574