@@ -4,7 +4,6 @@ use std::path::{Path, PathBuf};
44
55use lazycell::LazyCell;
66
7−use syntect::dumps::{from_binary, from_reader};
87use syntect::highlighting::{Theme, ThemeSet};
98use syntect::parsing::{SyntaxReference, SyntaxSet};
109
@@ -29,6 +28,12 @@ pub struct SyntaxReferenceInSet<'a> {
2928 pub syntax_set: &'a SyntaxSet,
3029}
3130
31+// Compress for size of ~700 kB instead of ~4600 kB at the cost of ~30% longer deserialization time
32+pub(crate) const COMPRESS_SYNTAXES: bool = true;
33+
34+// Compress for size of ~20 kB instead of ~200 kB at the cost of ~30% longer deserialization time
35+pub(crate) const COMPRESS_THEMES: bool = true;
36+
3237const IGNORED_SUFFIXES: [&str; 13] = [
3338 // Editor etc backups
3439 "~",
@@ -66,7 +71,7 @@ impl HighlightingAssets {
6671 pub fn from_cache(cache_path: &Path) -> Result<Self> {
6772 Ok(HighlightingAssets::new(
6873 SerializedSyntaxSet::FromFile(cache_path.join("syntaxes.bin")),
69− asset_from_cache(&cache_path.join("themes.bin"), "theme set")?,
74+ asset_from_cache(&cache_path.join("themes.bin"), "theme set", COMPRESS_THEMES)?,
7075 ))
7176 }
7277
@@ -299,8 +304,10 @@ enum SerializedSyntaxSet {
299304impl SerializedSyntaxSet {
300305 fn deserialize(&self) -> Result<SyntaxSet> {
301306 match self {
302− SerializedSyntaxSet::FromBinary(data) => Ok(from_binary(data)),
303− SerializedSyntaxSet::FromFile(ref path) => asset_from_cache(path, "syntax set"),
307+ SerializedSyntaxSet::FromBinary(data) => Ok(from_binary(data, COMPRESS_SYNTAXES)),
308+ SerializedSyntaxSet::FromFile(ref path) => {
309+ asset_from_cache(path, "syntax set", COMPRESS_SYNTAXES)
310+ }
304311 }
305312 }
306313}
@@ -310,10 +317,32 @@ pub(crate) fn get_serialized_integrated_syntaxset() -> &'static [u8] {
310317}
311318
312319pub(crate) fn get_integrated_themeset() -> ThemeSet {
313− from_binary(include_bytes!("../assets/themes.bin"))
320+ from_binary(include_bytes!("../assets/themes.bin"), COMPRESS_THEMES)
321+}
322+
323+pub(crate) fn from_binary<T: serde::de::DeserializeOwned>(v: &[u8], compressed: bool) -> T {
324+ asset_from_contents(v, "n/a", compressed)
325+ .expect("data integrated in binary is never faulty, but make sure `compressed` is in sync!")
326+}
327+
328+fn asset_from_contents<T: serde::de::DeserializeOwned>(
329+ contents: &[u8],
330+ description: &str,
331+ compressed: bool,
332+) -> Result<T> {
333+ if compressed {
334+ bincode::deserialize_from(flate2::read::ZlibDecoder::new(contents))
335+ } else {
336+ bincode::deserialize_from(contents)
337+ }
338+ .map_err(|_| format!("Could not parse {}", description).into())
314339}
315340
316−fn asset_from_cache<T: serde::de::DeserializeOwned>(path: &Path, description: &str) -> Result<T> {
341+fn asset_from_cache<T: serde::de::DeserializeOwned>(
342+ path: &Path,
343+ description: &str,
344+ compressed: bool,
345+) -> Result<T> {
317346 let contents = fs::read(path).map_err(|_| {
318347 format!(
319348 "Could not load cached {} '{}'",
@@ -321,7 +350,8 @@ fn asset_from_cache<T: serde::de::DeserializeOwned>(path: &Path, description: &s
321350 path.to_string_lossy()
322351 )
323352 })?;
324− from_reader(&contents[..]).map_err(|_| format!("Could not parse cached {}", description).into())
353+ asset_from_contents(&contents[..], description, compressed)
354+ .map_err(|_| format!("Could not parse cached {}", description).into())
325355}
326356
327357#[cfg(test)]