Use MessagePack for more compact rtsim state persistence

This commit is contained in:
Joshua Barretto 2022-08-09 14:44:02 +01:00
parent a421c1239d
commit 0b06eaec6f
4 changed files with 43 additions and 10 deletions

29
Cargo.lock generated
View File

@ -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",

View File

@ -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"

View File

@ -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<R: Read>(reader: R) -> SpannedResult<Self> {
ron::de::from_reader(reader)
pub fn from_reader<R: Read>(reader: R) -> Result<Self, ReadError> {
rmp_serde::decode::from_read(reader)
}
pub fn write_to<W: Write>(&self, writer: W) -> Result<(), ron::Error> {
ron::ser::to_writer(writer, self)
pub fn write_to<W: Write>(&self, mut writer: W) -> Result<(), WriteError> {
rmp_serde::encode::write(&mut writer, self)
}
}

View File

@ -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::<dyn Error>)
.and_then(|(mut file, tmp_file_path)| {
info!("Writing rtsim state to file...");
data.write_to(io::BufWriter::new(&mut file))?;