Repository: sharkdp/bat (language: rust)

Issue:
Loading assets from the cache is significantly slower than from the binary
### Step-by-step:

1. Measure the time it takes bat to highlight `examples/simple.rs` without cached assets, i.e. when the assets integrated into the binary are used:
```
% rm -rf ~/.cache/bat
% hyperfine --export-markdown /dev/tty 'bat --color always examples/simple.rs'
```
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|:---|---:|---:|---:|---:|
| `bat --color always examples/simple.rs` | 107.4 ± 2.1 | 104.1 | 112.5 | 1.00 |

2. Create cached assets that are identical to the integrated assets:
```
% mkdir -p ~/.cache/bat
% echo "---\nbat_version: 0.18.2" > ~/.cache/bat/metadata.yaml
% cp assets/syntaxes.bin ~/.cache/bat/syntaxes.bin
% cp assets/themes.bin ~/.cache/bat/themes.bin
```

3. Measure again
```
% hyperfine --export-markdown /dev/tty 'bat --color always examples/simple.rs'
```
| Command | Mean [ms] | Min [ms] | Max [ms] | Relative |
|:---|---:|---:|---:|---:|
| `bat --color always examples/simple.rs` | 115.0 ± 2.1 | 111.9 | 120.1 | 1.00 |

### Expected result
There is no significant difference.

### Actual result
There is a significant difference.

I will soon create a PR with a proposed fix.



Discussion / hints:

Fix patch:
diff --git a/CHANGELOG.md b/CHANGELOG.md
index e14ba84f47..79481d8709 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,8 @@
 
 ## Other
 
+- Load cached assets as fast as integrated assets, see #1753 (@Enselic)
+
 
 ## Syntaxes
 
diff --git a/src/assets.rs b/src/assets.rs
index 3c12531683..d9f4420d63 100644
--- a/src/assets.rs
+++ b/src/assets.rs
@@ -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};
@@ -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)]
@@ -316,7 +314,7 @@ mod tests {
     use std::ffi::OsStr;
 
     use std::fs::File;
-    use std::io::Write;
+    use std::io::{BufReader, Write};
     use tempfile::TempDir;
 
     use crate::input::Input;


Test patch: