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)
This commit is contained in:
Luke Street 2024-09-28 10:54:54 -06:00
parent ec9731e1e5
commit 2ec17aee9b
2 changed files with 8 additions and 6 deletions

View File

@ -1,6 +1,6 @@
use std::{ use std::{
fs::File, fs::File,
io::Read, io::{BufReader, Read},
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
@ -156,7 +156,7 @@ pub struct ProjectConfigInfo {
pub fn try_project_config(dir: &Path) -> Option<(Result<ProjectConfig>, ProjectConfigInfo)> { pub fn try_project_config(dir: &Path) -> Option<(Result<ProjectConfig>, ProjectConfigInfo)> {
for filename in CONFIG_FILENAMES.iter() { for filename in CONFIG_FILENAMES.iter() {
let config_path = dir.join(filename); let config_path = dir.join(filename);
let Ok(mut file) = File::open(&config_path) else { let Ok(file) = File::open(&config_path) else {
continue; continue;
}; };
let metadata = file.metadata(); let metadata = file.metadata();
@ -165,9 +165,10 @@ pub fn try_project_config(dir: &Path) -> Option<(Result<ProjectConfig>, ProjectC
continue; continue;
} }
let ts = FileTime::from_last_modification_time(&metadata); let ts = FileTime::from_last_modification_time(&metadata);
let mut reader = BufReader::new(file);
let mut result = match filename.contains("json") { let mut result = match filename.contains("json") {
true => read_json_config(&mut file), true => read_json_config(&mut reader),
false => read_yml_config(&mut file), false => read_yml_config(&mut reader),
}; };
if let Ok(config) = &result { if let Ok(config) = &result {
// Validate min_version if present // Validate min_version if present

View File

@ -1,5 +1,6 @@
use std::{ use std::{
fs::File, fs::File,
io::{BufReader, BufWriter},
path::{Path, PathBuf}, path::{Path, PathBuf},
}; };
@ -46,13 +47,13 @@ pub fn load_graphics_config(path: &Path) -> Result<Option<GraphicsConfig>> {
if !path.exists() { if !path.exists() {
return Ok(None); return Ok(None);
} }
let file = File::open(path)?; let file = BufReader::new(File::open(path)?);
let config: GraphicsConfig = ron::de::from_reader(file)?; let config: GraphicsConfig = ron::de::from_reader(file)?;
Ok(Some(config)) Ok(Some(config))
} }
pub fn save_graphics_config(path: &Path, config: &GraphicsConfig) -> Result<()> { 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)?; ron::ser::to_writer(file, config)?;
Ok(()) Ok(())
} }