From 2ec17aee9b731aeba8ddae2e2d9dc56982ed5850 Mon Sep 17 00:00:00 2001 From: Luke Street Date: Sat, 28 Sep 2024 10:54:54 -0600 Subject: [PATCH] Improve config read/write performance We were accidentally using unbuffered readers and writers before, leading to long pauses on the main thread on slow filesystems. (e.g. FUSE or WSL) --- objdiff-core/src/config/mod.rs | 9 +++++---- objdiff-gui/src/views/graphics.rs | 5 +++-- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/objdiff-core/src/config/mod.rs b/objdiff-core/src/config/mod.rs index 8f603e1..de06707 100644 --- a/objdiff-core/src/config/mod.rs +++ b/objdiff-core/src/config/mod.rs @@ -1,6 +1,6 @@ use std::{ fs::File, - io::Read, + io::{BufReader, Read}, path::{Path, PathBuf}, }; @@ -156,7 +156,7 @@ pub struct ProjectConfigInfo { pub fn try_project_config(dir: &Path) -> Option<(Result, ProjectConfigInfo)> { for filename in CONFIG_FILENAMES.iter() { let config_path = dir.join(filename); - let Ok(mut file) = File::open(&config_path) else { + let Ok(file) = File::open(&config_path) else { continue; }; let metadata = file.metadata(); @@ -165,9 +165,10 @@ pub fn try_project_config(dir: &Path) -> Option<(Result, ProjectC continue; } let ts = FileTime::from_last_modification_time(&metadata); + let mut reader = BufReader::new(file); let mut result = match filename.contains("json") { - true => read_json_config(&mut file), - false => read_yml_config(&mut file), + true => read_json_config(&mut reader), + false => read_yml_config(&mut reader), }; if let Ok(config) = &result { // Validate min_version if present diff --git a/objdiff-gui/src/views/graphics.rs b/objdiff-gui/src/views/graphics.rs index 53b3286..6b57e94 100644 --- a/objdiff-gui/src/views/graphics.rs +++ b/objdiff-gui/src/views/graphics.rs @@ -1,5 +1,6 @@ use std::{ fs::File, + io::{BufReader, BufWriter}, path::{Path, PathBuf}, }; @@ -46,13 +47,13 @@ pub fn load_graphics_config(path: &Path) -> Result> { if !path.exists() { return Ok(None); } - let file = File::open(path)?; + let file = BufReader::new(File::open(path)?); let config: GraphicsConfig = ron::de::from_reader(file)?; Ok(Some(config)) } pub fn save_graphics_config(path: &Path, config: &GraphicsConfig) -> Result<()> { - let file = File::create(path)?; + let file = BufWriter::new(File::create(path)?); ron::ser::to_writer(file, config)?; Ok(()) }