on master help sign in
Make loading of cached assets closer in performance to integrated assets

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 #1753

commitccf456357366f488d902232faf495d09b246a130 authorMartin Nordholts <enselic@gmail.com> dateMon 26 Jul 2021, 12:59 (5 years ago) parentfb1ab09
2 files changed+6−6
CHANGELOG.md+2 src/assets.rs+4−6
CHANGELOG.md+2
@@ -8,6 +8,8 @@
88
99## Other
1010
11+- Load cached assets as fast as integrated assets, see #1753 (@Enselic)
12+
1113
1214## Syntaxes
1315
src/assets.rs+4−6
@@ -1,7 +1,6 @@
11use std::collections::BTreeMap;
22use std::ffi::OsStr;
3−use std::fs::{self, File};
4−use std::io::BufReader;
3+use std::fs;
54use std::path::Path;
65
76use syntect::dumps::{dump_to_file, from_binary, from_reader};
@@ -298,15 +297,14 @@ fn asset_to_cache<T: serde::Serialize>(asset: &T, path: &Path, description: &str
298297}
299298
300299fn asset_from_cache<T: serde::de::DeserializeOwned>(path: &Path, description: &str) -> Result<T> {
301− let asset_file = File::open(&path).chain_err(|| {
300+ let contents = fs::read(path).chain_err(|| {
302301 format!(
303302 "Could not load cached {} '{}'",
304303 description,
305304 path.to_string_lossy()
306305 )
307306 })?;
308− from_reader(BufReader::new(asset_file))
309− .chain_err(|| format!("Could not parse cached {}", description))
307+ from_reader(&contents[..]).chain_err(|| format!("Could not parse cached {}", description))
310308}
311309
312310#[cfg(test)]
@@ -316,7 +314,7 @@ mod tests {
316314 use std::ffi::OsStr;
317315
318316 use std::fs::File;
319− use std::io::Write;
317+ use std::io::{BufReader, Write};
320318 use tempfile::TempDir;
321319
322320 use crate::input::Input;