From 558891c14640356289a6dd55a687c2a1a8947bc2 Mon Sep 17 00:00:00 2001 From: Emil Ernerfeldt Date: Tue, 19 Apr 2022 21:26:35 +0200 Subject: [PATCH] eframe native: persist app state in background thread Gives smoother frame rate --- CHANGELOG.md | 1 + eframe/CHANGELOG.md | 2 +- epi/src/file_storage.rs | 14 ++++++++++---- 3 files changed, 12 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d828a004..723be8b8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ NOTE: [`epaint`](epaint/CHANGELOG.md), [`eframe`](eframe/CHANGELOG.md), [`egui_w * Added `Link` and `ui.link` ([#1506](https://github.com/emilk/egui/pull/1506)). * Added triple-click support; triple-clicking a TextEdit field will select the whole paragraph ([#1512](https://github.com/emilk/egui/pull/1512)). * Added `Plot::x_grid_spacer` and `Plot::y_grid_spacer` for custom grid spacing ([#1180](https://github.com/emilk/egui/pull/1180)). +* Added `Ui::spinner()` shortcut method ([#1494](https://github.com/emilk/egui/pull/1494)). * Added `CursorIcon`s for resizing columns, rows, and the eight cardinal directions. ### Changed 🔧 diff --git a/eframe/CHANGELOG.md b/eframe/CHANGELOG.md index bb3849df..558878a4 100644 --- a/eframe/CHANGELOG.md +++ b/eframe/CHANGELOG.md @@ -23,7 +23,7 @@ NOTE: [`egui_web`](../egui_web/CHANGELOG.md), [`egui-winit`](../egui-winit/CHANG * Fixed potential scale bug when DPI scaling changes (e.g. when dragging a window between different displays) ([#1441](https://github.com/emilk/egui/pull/1441)). * MSRV (Minimum Supported Rust Version) is now `1.60.0` ([#1467](https://github.com/emilk/egui/pull/1467)). * Added new feature `puffin` to add [`puffin profiler`](https://github.com/EmbarkStudios/puffin) scopes ([#1483](https://github.com/emilk/egui/pull/1483)). -* Added `Ui::spinner()` shortcut method ([#1494](https://github.com/emilk/egui/pull/1494)). +* Moved app persistence to a background thread, allowing for smoother frame rates (on native). ## 0.17.0 - 2022-02-22 diff --git a/epi/src/file_storage.rs b/epi/src/file_storage.rs index 9beddac6..2fd72aa1 100644 --- a/epi/src/file_storage.rs +++ b/epi/src/file_storage.rs @@ -59,11 +59,17 @@ impl crate::Storage for FileStorage { fn flush(&mut self) { if self.dirty { - // eprintln!("Persisted to {}", self.path.display()); - let file = std::fs::File::create(&self.ron_filepath).unwrap(); - let config = Default::default(); - ron::ser::to_writer_pretty(file, &self.kv, config).unwrap(); self.dirty = false; + + let file_path = self.ron_filepath.clone(); + let kv = self.kv.clone(); + + std::thread::spawn(move || { + let file = std::fs::File::create(&file_path).unwrap(); + let config = Default::default(); + ron::ser::to_writer_pretty(file, &kv, config).unwrap(); + tracing::trace!("Persisted to {:?}", file_path); + }); } } }