Skip to content

Commit

Permalink
SerializedSyntaxSet: Load cache data early; only parse lazily
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Enselic committed Jul 25, 2021
1 parent 6797854 commit 4197558
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::collections::BTreeMap;
use std::ffi::OsStr;
use std::fs::{self, File};
use std::io::BufReader;
use std::path::{Path, PathBuf};
use std::path::Path;

use lazycell::LazyCell;

Expand Down Expand Up @@ -122,9 +122,9 @@ impl HighlightingAssets {
pub fn from_cache(cache_path: &Path) -> Result<Self> {
Ok(HighlightingAssets::new(
None,
Some(SerializedSyntaxSet::FromFile(
Some(SerializedSyntaxSet::FromFile(std::fs::read(
cache_path.join("syntaxes.bin"),
)),
)?)),
asset_from_cache(&cache_path.join("themes.bin"), "theme set")?,
))
}
Expand Down Expand Up @@ -308,7 +308,7 @@ impl HighlightingAssets {
#[derive(Debug)]
enum SerializedSyntaxSet {
/// The data comes from a user-generated cache file.
FromFile(PathBuf),
FromFile(Vec<u8>),

/// The data to use is embedded into the bat binary.
FromBinary(&'static [u8]),
Expand All @@ -320,8 +320,8 @@ impl SerializedSyntaxSet {
SerializedSyntaxSet::FromBinary(data) => {
from_binary(data)
},
SerializedSyntaxSet::FromFile(ref path) => {
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")
SerializedSyntaxSet::FromFile(ref data) => {
from_reader(&data[..]).expect("cache corrupt, consider rebuilding or clearing, see https://github.com/sharkdp/bat#adding-new-syntaxes--language-definitions on how")
},
}
}
Expand Down

0 comments on commit 4197558

Please sign in to comment.