Give syntax highlighting a two-second budget
Browse filesHighlighting ran outside every deadline, so a slow grammar on a large file kept its render slot busy indefinitely. Past the budget the file is shown as plain text. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- Author
- Um9i vulgarmessiah@gmail.com
- Date
- 22 Nov 2026, 22:02
- Commit
- a47d6e4f9ab4
- Parent
- baecb32
1 file changed +23−5
| @@ -4,6 +4,7 @@ | ||
| //! inline styles, so the page's `style-src 'self'` policy holds; the site stylesheet colours them. | ||
| use std::sync::LazyLock; | ||
| use std::time::{Duration, Instant}; | ||
| use pulldown_cmark::{CodeBlockKind, CowStr, Event, Options, Parser, Tag, TagEnd}; | ||
| use syntect::html::{ClassStyle, ClassedHTMLGenerator}; | ||
| @@ -13,6 +14,9 @@ use syntect::util::LinesWithEndings; | ||
| /// Files larger than this are shown as plain escaped text. | ||
| pub const HIGHLIGHT_LIMIT: usize = 512 << 10; | ||
| /// The most time one file or code block may spend being highlighted. | ||
| const HIGHLIGHT_BUDGET: Duration = Duration::from_secs(2); | ||
| const CLASSES: ClassStyle = ClassStyle::SpacedPrefixed { prefix: "hl-" }; | ||
| static SYNTAXES: LazyLock<SyntaxSet> = LazyLock::new(SyntaxSet::load_defaults_newlines); | ||
| @@ -42,13 +46,20 @@ fn syntax_for_fence(lang: &str) -> Option<&'static SyntaxReference> { | ||
| } | ||
| fn highlight_with(code: &str, syntax: Option<&'static SyntaxReference>) -> String { | ||
| let Some(syntax) = syntax.filter(|_| code.len() <= HIGHLIGHT_LIMIT) else { | ||
| return html_escape::encode_text(code).into_owned(); | ||
| }; | ||
| highlight_within(code, syntax, HIGHLIGHT_BUDGET) | ||
| } | ||
| /// Highlight, or give up and show plain text once `budget` is spent. Some grammars are slow on | ||
| /// some input, and nothing else bounds this work: it runs outside git's deadlines and holds a | ||
| /// render slot until it ends. | ||
| fn highlight_within(code: &str, syntax: Option<&'static SyntaxReference>, budget: Duration) -> String { | ||
| let plain = || html_escape::encode_text(code).into_owned(); | ||
| let Some(syntax) = syntax.filter(|_| code.len() <= HIGHLIGHT_LIMIT) else { return plain() }; | ||
| let deadline = Instant::now() + budget; | ||
| let mut generator = ClassedHTMLGenerator::new_with_class_style(syntax, &SYNTAXES, CLASSES); | ||
| for line in LinesWithEndings::from(code) { | ||
| if generator.parse_html_for_line_which_includes_newline(line).is_err() { | ||
| return html_escape::encode_text(code).into_owned(); | ||
| if Instant::now() >= deadline || generator.parse_html_for_line_which_includes_newline(line).is_err() { | ||
| return plain(); | ||
| } | ||
| } | ||
| generator.finalize() | ||
| @@ -298,6 +309,13 @@ mod tests { | ||
| assert_eq!(repo(b"", "a%00b"), None); | ||
| } | ||
| #[test] | ||
| fn highlighting_past_its_budget_falls_back_to_plain_text() { | ||
| let syntax = syntax_for("x.rs", ""); | ||
| assert!(highlight_within("fn main() {}\n", syntax, Duration::from_secs(60)).contains("hl-")); | ||
| assert_eq!(highlight_within("fn <b>\n", syntax, Duration::ZERO), "fn <b>\n"); | ||
| } | ||
| #[test] | ||
| fn highlights_fenced_code_and_files() { | ||
| let html = markdown("```rust\nfn main() {}\n```", b"", &link); | ||