From 4ee73a8793af1d791a5b0ccde7ba62cb3906a079 Mon Sep 17 00:00:00 2001 From: Alula <6276139+alula@users.noreply.github.com> Date: Mon, 12 Aug 2024 23:08:04 +0200 Subject: [PATCH] Hopefully this will improve I/O perf on Horizon --- src/game/settings.rs | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/game/settings.rs b/src/game/settings.rs index 9a6de88..09ae380 100644 --- a/src/game/settings.rs +++ b/src/game/settings.rs @@ -1,3 +1,5 @@ +use std::io::{Read, Write}; + use crate::framework::context::Context; use crate::framework::error::GameResult; use crate::framework::filesystem::{user_create, user_open}; @@ -173,8 +175,13 @@ fn default_cutscene_skip_mode() -> CutsceneSkipMode { impl Settings { pub fn load(ctx: &Context) -> GameResult { - if let Ok(file) = user_open(ctx, "/settings.json") { - match serde_json::from_reader::<_, Settings>(file) { + if let Ok(mut file) = user_open(ctx, "/settings.json") { + // we use a buffer to avoid hammering the OS with small reads/writes + // I/O on settings was particularly VERY SLOW on Horizon (well it's based on microkernel, so IPC overhead...) + let mut buf = Vec::new(); + file.read_to_end(&mut buf)?; + + match serde_json::from_slice::(&buf) { Ok(settings) => return Ok(settings.upgrade()), Err(err) => log::warn!("Failed to deserialize settings: {}", err), } @@ -183,6 +190,14 @@ impl Settings { Ok(Settings::default()) } + pub fn save(&self, ctx: &Context) -> GameResult { + // see why in load() + let buffer = serde_json::to_vec_pretty(self)?; + user_create(ctx, "/settings.json")?.write_all(&buffer)?; + + Ok(()) + } + fn upgrade(mut self) -> Self { let initial_version = self.version; @@ -362,13 +377,6 @@ impl Settings { self } - pub fn save(&self, ctx: &Context) -> GameResult { - let file = user_create(ctx, "/settings.json")?; - serde_json::to_writer_pretty(file, self)?; - - Ok(()) - } - pub fn create_player1_controller(&self) -> Box { if self.touch_controls { return Box::new(TouchPlayerController::new());