Skip to content

Commit

Permalink
file_storage: Wrap File in BufWriter (#3353)
Browse files Browse the repository at this point in the history
When exiting my application, it would freeze for around a minute waiting to finish writing ca. 40MB to file storage.
Some quick profiling revealed that it was spending all that time in `File::write`,
presumably making a large number of system calls writing tiny amounts of data at a time.
We can avoid this by buffering writes using a `BufWriter`, just like we already do with `BufReader`.

With this change, my application takes around 1-2 seconds to exit, with the majority of that time spent serializing `ron`.
I'm sure there are further potential performance improvements there, but this is already an order of magnitude or two better.

I also fixed the call to `log::warn!`, which was using incorrect formatting syntax and not including the error message.
  • Loading branch information
sebbert authored Sep 18, 2023
1 parent 8073ca6 commit 433d1fc
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions crates/eframe/src/native/file_storage.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
collections::HashMap,
io::Write,
path::{Path, PathBuf},
};

Expand Down Expand Up @@ -126,10 +127,13 @@ fn save_to_disk(file_path: &PathBuf, kv: &HashMap<String, String>) {

match std::fs::File::create(file_path) {
Ok(file) => {
let mut writer = std::io::BufWriter::new(file);
let config = Default::default();

if let Err(err) = ron::ser::to_writer_pretty(file, &kv, config) {
log::warn!("Failed to serialize app state: {err}");
if let Err(err) = ron::ser::to_writer_pretty(&mut writer, &kv, config)
.and_then(|_| writer.flush().map_err(|err| err.into()))
{
log::warn!("Failed to serialize app state: {}", err);
} else {
log::trace!("Persisted to {:?}", file_path);
}
Expand Down

0 comments on commit 433d1fc

Please sign in to comment.