Skip to content

Commit

Permalink
Make loading of cached assets closer in performance to integrated assets
Browse files Browse the repository at this point in the history
Using BufReader makes sense for large files, but assets are never large enough
to require buffering. It is significantly faster to load the file contents in
one go, so let's do that instead.

Closes sharkdp#1753
  • Loading branch information
Enselic committed Jul 26, 2021
1 parent f464b1b commit b504738
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

## Other

- Load cached assets as fast as integrated assets, see #1753 (@Enselic)


## Syntaxes

Expand Down
8 changes: 3 additions & 5 deletions src/assets.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::collections::BTreeMap;
use std::ffi::OsStr;
use std::fs::{self, File};
use std::io::BufReader;
use std::fs;
use std::path::Path;

use syntect::dumps::{dump_to_file, from_binary, from_reader};
Expand Down Expand Up @@ -298,15 +297,14 @@ fn asset_to_cache<T: serde::Serialize>(asset: &T, path: &Path, description: &str
}

fn asset_from_cache<T: serde::de::DeserializeOwned>(path: &Path, description: &str) -> Result<T> {
let asset_file = File::open(&path).chain_err(|| {
let contents = fs::read(path).chain_err(|| {
format!(
"Could not load cached {} '{}'",
description,
path.to_string_lossy()
)
})?;
from_reader(BufReader::new(asset_file))
.chain_err(|| format!("Could not parse cached {}", description))
from_reader(&contents[..]).chain_err(|| format!("Could not parse cached {}", description))
}

#[cfg(test)]
Expand Down

0 comments on commit b504738

Please sign in to comment.