Skip to content

Commit 135de93

Browse files
committed
f Drop BufReader to just read to Vec directly
As we're always reading the files to their end anyways there is no benefit to maintaining a separate buffer as `BufReader`. Here, we therefore opt to read to our `Vec` directly.
1 parent 1329fd2 commit 135de93

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

lightning-persister/src/fs_store.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use lightning::util::string::PrintableString;
44

55
use std::collections::HashMap;
66
use std::fs;
7-
use std::io::{BufReader, Read, Write};
7+
use std::io::{Read, Write};
88
use std::path::{Path, PathBuf};
99
use std::sync::atomic::{AtomicUsize, Ordering};
1010
use std::sync::{Arc, Mutex, RwLock};
@@ -28,6 +28,9 @@ fn path_to_windows_str<T: AsRef<OsStr>>(path: T) -> Vec<u16> {
2828
path.as_ref().encode_wide().chain(Some(0)).collect()
2929
}
3030

31+
// We reuse std::io's DEFAULT_BUF_SIZE value of 8KB.
32+
const DEFAULT_BUF_SIZE: usize = 8 * 1024;
33+
3134
/// A [`KVStore`] implementation that writes to and reads from the file system.
3235
pub struct FilesystemStore {
3336
data_dir: PathBuf,
@@ -76,10 +79,9 @@ impl KVStore for FilesystemStore {
7679
};
7780
let _guard = inner_lock_ref.read().unwrap();
7881

79-
let mut buf = Vec::new();
80-
let f = fs::File::open(dest_file_path.clone())?;
81-
let mut reader = BufReader::new(f);
82-
let nread = reader.read_to_end(&mut buf)?;
82+
let mut buf = Vec::with_capacity(DEFAULT_BUF_SIZE);
83+
let mut f = fs::File::open(dest_file_path.clone())?;
84+
let nread = f.read_to_end(&mut buf)?;
8385
debug_assert_ne!(nread, 0);
8486

8587
Ok(buf)

0 commit comments

Comments
 (0)