From 8b216bc3c4173c4defbca2a28d71202f834d94db Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Thu, 13 Aug 2026 02:18:10 +0200 Subject: [PATCH] only load syntaxes and themes once --- src/models/pretty.rs | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-) diff --git a/src/models/pretty.rs b/src/models/pretty.rs index 4735cad..4f8b17b 100644 --- a/src/models/pretty.rs +++ b/src/models/pretty.rs @@ -1,7 +1,9 @@ use std::fs; +use std::io::Cursor; use std::path::Path; +use std::sync::LazyLock; -use syntect::highlighting::ThemeSet; +use syntect::highlighting::{Theme, ThemeSet}; use syntect::html::highlighted_html_for_string; use syntect::parsing::SyntaxSet; @@ -10,17 +12,22 @@ static SYNTAXES: &[u8] = static THEMES: &[u8] = include_bytes!("../../resources/themes/ayu_dark.tmTheme"); +static SS: LazyLock = LazyLock::new(|| { + syntect::dumps::from_uncompressed_data(SYNTAXES) + .expect("invalid syntax dump") +}); + +static THEME: LazyLock = LazyLock::new(|| { + ThemeSet::load_from_reader(&mut Cursor::new(THEMES)) + .expect("invalid theme") +}); + pub fn get_pretty_body(path: &Path, ext: &str) -> std::io::Result { - let ss: SyntaxSet = syntect::dumps::from_binary(SYNTAXES); - - let mut theme_cursor = std::io::Cursor::new(THEMES); - let theme = ThemeSet::load_from_reader(&mut theme_cursor).unwrap(); - let content = fs::read_to_string(path)?; - let syntax = ss + let syntax = SS .find_syntax_by_token(ext) - .unwrap_or_else(|| ss.find_syntax_plain_text()); + .unwrap_or_else(|| SS.find_syntax_plain_text()); - highlighted_html_for_string(&content, &ss, syntax, &theme) + highlighted_html_for_string(&content, &SS, syntax, &THEME) .map_err(std::io::Error::other) }