load syntaxes in parallel

This commit is contained in:
liquidex 2024-11-26 22:09:39 +01:00
parent a8773aac63
commit 5de308c0c4
2 changed files with 24 additions and 9 deletions

View file

@ -1,6 +1,7 @@
use std::{collections::HashMap, ops::ControlFlow};
use anyhow::{anyhow, Context};
use rayon::iter::{IntoParallelRefIterator, ParallelIterator};
use serde::{Deserialize, Serialize};
use tracing::{error, info_span, instrument};
@ -10,7 +11,7 @@ use crate::{
Syntax,
},
import_map::ImportRoot,
vfs::{self, Dir, ImageSize, VPath, VPathBuf},
vfs::{self, Dir, DynDir, ImageSize, VPath, VPathBuf},
};
#[derive(Debug, Clone, Deserialize, Serialize)]
@ -159,9 +160,18 @@ impl Config {
/// Loads all syntax definition files.
#[instrument(name = "Config::load_syntaxes", skip(self))]
pub fn load_syntaxes(&mut self, dir: &dyn Dir) -> anyhow::Result<()> {
vfs::walk_dir_rec(dir, VPath::ROOT, &mut |path| {
pub fn load_syntaxes(&mut self, dir: DynDir) -> anyhow::Result<()> {
let mut paths = vec![];
vfs::walk_dir_rec(&dir, VPath::ROOT, &mut |path| {
if path.extension() == Some("json") {
paths.push(path.to_owned());
}
ControlFlow::Continue(())
});
let syntaxes: Vec<_> = paths
.par_iter()
.flat_map(|path| {
let name = path
.file_stem()
.expect("syntax file name should have a stem due to the .json extension");
@ -180,14 +190,19 @@ impl Config {
Ok(syntax) => {
let _span = info_span!("Config::load_syntaxes::compile", ?name).entered();
let compiled = compile_syntax(&syntax);
self.syntaxes.insert(name.to_owned(), compiled);
Some((name.to_owned(), compiled))
}
Err(err) => {
error!("error while loading syntax file `{path}`: {err}");
None
}
Err(err) => error!("error while loading syntax file `{path}`: {err}"),
}
}
})
.collect();
ControlFlow::Continue(())
});
for (name, compiled) in syntaxes {
self.syntaxes.insert(name, compiled);
}
Ok(())
}

View file

@ -320,7 +320,7 @@ impl Sources {
config.site = std::env::var("TREEHOUSE_SITE").unwrap_or(config.site);
config.autopopulate_emoji(&*dirs.emoji)?;
config.autopopulate_pics(&*dirs.pic)?;
config.load_syntaxes(&*dirs.syntax)?;
config.load_syntaxes(dirs.syntax.clone())?;
config
};