From 0b06eaec6f0673ba20f9e20720534c01fe9550a0 Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Tue, 9 Aug 2022 14:44:02 +0100 Subject: [PATCH] Use MessagePack for more compact rtsim state persistence --- Cargo.lock | 29 +++++++++++++++++++++++++++++ rtsim/Cargo.toml | 1 + rtsim/src/data/mod.rs | 12 +++++++----- server/src/rtsim2/mod.rs | 11 ++++++----- 4 files changed, 43 insertions(+), 10 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2ce962ac5c..4915092e2c 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4318,6 +4318,12 @@ dependencies = [ "regex", ] +[[package]] +name = "paste" +version = "1.0.8" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9423e2b32f7a043629287a536f21951e8c6a82482d0acb1eeebfc90bc2225b22" + [[package]] name = "peeking_take_while" version = "0.1.2" @@ -5096,6 +5102,28 @@ dependencies = [ "syn 1.0.100", ] +[[package]] +name = "rmp" +version = "0.8.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "44519172358fd6d58656c86ab8e7fbc9e1490c3e8f14d35ed78ca0dd07403c9f" +dependencies = [ + "byteorder", + "num-traits", + "paste", +] + +[[package]] +name = "rmp-serde" +version = "1.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25786b0d276110195fa3d6f3f31299900cf71dfbd6c28450f3f58a0e7f7a347e" +dependencies = [ + "byteorder", + "rmp", + "serde", +] + [[package]] name = "rodio" version = "0.15.0" @@ -6913,6 +6941,7 @@ version = "0.10.0" dependencies = [ "enum-map", "hashbrown 0.12.3", + "rmp-serde", "ron 0.8.0", "serde", "vek 0.15.8", diff --git a/rtsim/Cargo.toml b/rtsim/Cargo.toml index 504d1d50cd..96fccf251e 100644 --- a/rtsim/Cargo.toml +++ b/rtsim/Cargo.toml @@ -11,3 +11,4 @@ serde = { version = "1.0.110", features = ["derive"] } hashbrown = { version = "0.12", features = ["rayon", "serde", "nightly"] } enum-map = { version = "2.4", features = ["serde"] } vek = { version = "0.15.8", features = ["serde"] } +rmp-serde = "1.1.0" diff --git a/rtsim/src/data/mod.rs b/rtsim/src/data/mod.rs index d4dd3d97e9..4e8622f294 100644 --- a/rtsim/src/data/mod.rs +++ b/rtsim/src/data/mod.rs @@ -7,7 +7,6 @@ pub use self::{ }; use self::helper::Latest; -use ron::error::SpannedResult; use serde::{Serialize, Deserialize}; use std::io::{Read, Write}; @@ -17,12 +16,15 @@ pub struct Data { pub actors: Actors, } +pub type ReadError = rmp_serde::decode::Error; +pub type WriteError = rmp_serde::encode::Error; + impl Data { - pub fn from_reader(reader: R) -> SpannedResult { - ron::de::from_reader(reader) + pub fn from_reader(reader: R) -> Result { + rmp_serde::decode::from_read(reader) } - pub fn write_to(&self, writer: W) -> Result<(), ron::Error> { - ron::ser::to_writer(writer, self) + pub fn write_to(&self, mut writer: W) -> Result<(), WriteError> { + rmp_serde::encode::write(&mut writer, self) } } diff --git a/server/src/rtsim2/mod.rs b/server/src/rtsim2/mod.rs index 515c0dbd7b..2cf6b2b337 100644 --- a/server/src/rtsim2/mod.rs +++ b/server/src/rtsim2/mod.rs @@ -8,7 +8,7 @@ use common::{ vol::RectRasterableVol, }; use common_ecs::{dispatch, System}; -use rtsim2::{data::Data, RtState}; +use rtsim2::{data::{Data, ReadError}, RtState}; use specs::{DispatcherBuilder, WorldExt}; use std::{ fs::{self, File}, @@ -16,6 +16,7 @@ use std::{ sync::Arc, time::Instant, io::{self, Write}, + error::Error, }; use enum_map::EnumMap; use tracing::{error, warn, info}; @@ -51,7 +52,7 @@ impl RtSim { loop { let mut backup_path = file_path.clone(); backup_path.set_extension(if i == 0 { - format!("ron_backup_{}", i) + format!("backup_{}", i) } else { "ron_backup".to_string() }); @@ -88,7 +89,7 @@ impl RtSim { data_dir.push("rtsim"); data_dir }); - path.push("state.ron"); + path.push("state.dat"); path } @@ -112,7 +113,7 @@ impl RtSim { // TODO: Use slow job // slowjob_pool.spawn("RTSIM_SAVE", move || { std::thread::spawn(move || { - let tmp_file_name = "state_tmp.ron"; + let tmp_file_name = "state_tmp.dat"; if let Err(e) = file_path .parent() .map(|dir| { @@ -124,7 +125,7 @@ impl RtSim { .and_then(|tmp_file_path| { Ok((File::create(&tmp_file_path)?, tmp_file_path)) }) - .map_err(|e: io::Error| ron::Error::from(e)) + .map_err(|e: io::Error| Box::new(e) as Box::) .and_then(|(mut file, tmp_file_path)| { info!("Writing rtsim state to file..."); data.write_to(io::BufWriter::new(&mut file))?;