Skip to content

Commit 4197558

Browse files
committed
SerializedSyntaxSet: Load cache data early; only parse lazily
The slow part is parsing, so we can load the data from the file during startup without a big performance hit. The benefit of loading the data during startup is that we can better detect when the cache is incomplete, and fallback to data from the binary. In particular, this change makes the following scenario fallback to syntaxes.bin from the binary during startup (same behavior as current bat), rather than panicing when we try to lazily load syntaxes.bin that does not exist: bat cache --build --source assets rm ~/.cache/bat/syntaxes.bin bat any-file > /tmp/out.txt
1 parent 6797854 commit 4197558

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

src/assets.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::collections::BTreeMap;
22
use std::ffi::OsStr;
33
use std::fs::{self, File};
44
use std::io::BufReader;
5-
use std::path::{Path, PathBuf};
5+
use std::path::Path;
66

77
use lazycell::LazyCell;
88

@@ -122,9 +122,9 @@ impl HighlightingAssets {
122122
pub fn from_cache(cache_path: &Path) -> Result<Self> {
123123
Ok(HighlightingAssets::new(
124124
None,
125-
Some(SerializedSyntaxSet::FromFile(
125+
Some(SerializedSyntaxSet::FromFile(std::fs::read(
126126
cache_path.join("syntaxes.bin"),
127-
)),
127+
)?)),
128128
asset_from_cache(&cache_path.join("themes.bin"), "theme set")?,
129129
))
130130
}
@@ -308,7 +308,7 @@ impl HighlightingAssets {
308308
#[derive(Debug)]
309309
enum SerializedSyntaxSet {
310310
/// The data comes from a user-generated cache file.
311-
FromFile(PathBuf),
311+
FromFile(Vec<u8>),
312312

313313
/// The data to use is embedded into the bat binary.
314314
FromBinary(&'static [u8]),
@@ -320,8 +320,8 @@ impl SerializedSyntaxSet {
320320
SerializedSyntaxSet::FromBinary(data) => {
321321
from_binary(data)
322322
},
323-
SerializedSyntaxSet::FromFile(ref path) => {
324-
asset_from_cache(&path, "syntax set").expect("cache corrupt, consider rebuilding or clearing, see https://github.com/sharkdp/bat#adding-new-syntaxes--language-definitions on how")
323+
SerializedSyntaxSet::FromFile(ref data) => {
324+
from_reader(&data[..]).expect("cache corrupt, consider rebuilding or clearing, see https://github.com/sharkdp/bat#adding-new-syntaxes--language-definitions on how")
325325
},
326326
}
327327
}

0 commit comments

Comments
 (0)