Defer initialization of the Handlebars template engine and syntax highlighter to their first use, reducing application startup time by avoiding expensive upfront work.
Context
ForgeTemplateService and MarkdownFormat previously initialized their heaviest dependencies — the Handlebars engine and the SyntaxHighlighter — 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-initialized Arc<RwLock<Handlebars>> with Arc<OnceCell<RwLock<Handlebars>>>. Added a private get_hb() async helper that creates the Handlebars instance on the first call and returns a reference to it on all subsequent calls. The new() constructor now does no heavy work.
MarkdownFormat: Changed the highlighter field from a plain SyntaxHighlighter to a OnceLock<SyntaxHighlighter>. The highlighter is created via get_or_init the first time format() is called, leaving construction of MarkdownFormat itself lightweight.
Key Implementation Details
Both changes use the standard lazy-initialization primitives idiomatic to their execution context:
tokio::sync::OnceCell for the async ForgeTemplateService (supports .await in the initializer closure).
std::sync::OnceLock for the synchronous MarkdownFormat (no async required).
The public API of both types is unchanged — callers see no difference in behavior.
Testing
# Run template service tests
cargo insta test --accept -p forge_services
# Run display/markdown tests
cargo insta test --accept -p forge_display
# Quick type-check across the workspace
cargo check
tusharmath
changed the title
perf(template): lazily initialize Handlebars in ForgeTemplateService
perf: lazily initialize Handlebars and SyntaxHighlighter to reduce startup time
Mar 30, 2026
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
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
ForgeTemplateService and MarkdownFormat previously initialized their heaviest dependencies — the Handlebars engine and the SyntaxHighlighter — 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
Key Implementation Details
Both changes use the standard lazy-initialization primitives idiomatic to their execution context:
The public API of both types is unchanged — callers see no difference in behavior.
Testing
Fixes #2574