perf(display): cache terminal theme detection with 100ms timeout#2992
Merged
tusharmath merged 3 commits intomainfrom Apr 14, 2026
Merged
perf(display): cache terminal theme detection with 100ms timeout#2992tusharmath merged 3 commits intomainfrom
tusharmath merged 3 commits intomainfrom
Conversation
Collaborator
Author
|
Fixes #2621 |
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
Cache terminal theme detection with a 100ms timeout to avoid repeated blocking queries on every syntax-highlighted output.
Context
Terminal theme detection (
terminal_colorsaurus::theme_mode) was called on every invocation of syntax highlighting and markdown rendering. This sends an escape-sequence query to the terminal and waits for a response, which is an I/O-bound operation that adds latency to every render cycle. In non-interactive environments (CI, piped output, some terminal emulators) the query can block for the full default timeout before falling back to dark mode, visibly slowing down output.Changes
OnceLock<bool>/OnceLock<ThemeMode>process-wide cache in bothforge_displayandforge_markdown_streamso the terminal is queried at most once per process lifetime.QueryOptionsin both crates, replacing the library default (which can be several seconds).THEME_DETECT_TIMEOUTconstants and doc-comments explaining the caching and fallback behaviour.Key Implementation Details
std::sync::OnceLockprovides lock-free reads after the first initialization, making subsequent calls tois_dark_theme()/detect_theme_mode()essentially free (a single atomic load). The 100 ms timeout is a safe upper bound: real terminals respond in < 10 ms, while environments that do not support the query will now fail-fast instead of stalling.Testing