Skip to content

Commit

Permalink
Add support for 'Savefile'-serialization framework
Browse files Browse the repository at this point in the history
  • Loading branch information
avl committed Jul 12, 2023
1 parent 668be80 commit 0516622
Show file tree
Hide file tree
Showing 8 changed files with 85 additions and 1 deletion.
5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ serde_json = { version = "1.0.96", optional = true }
simd-json = { version = "0.9.2", optional = true }
simd-json-derive = { version = "0.9.2", optional = true }
speedy = { version = "0.8.6", optional = true }
savefile = { version = "0.13", optional = true }
savefile-derive = { version = "0.13", optional = true }
zstd = "0.12.3"

[features]
Expand Down Expand Up @@ -107,9 +109,12 @@ default = [
"serde_json",
"simd-json",
"speedy",
"savefile"
]
simd-json = ["dep:simd-json", "simd-json-derive"]

savefile = ["dep:savefile", "savefile-derive"]

scale = ["parity-scale-codec", "parity-scale-codec-derive"]

[dev-dependencies]
Expand Down
16 changes: 15 additions & 1 deletion benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,9 @@ use rust_serialization_benchmark::bench_serde_json;
use rust_serialization_benchmark::bench_simd_json;
#[cfg(feature = "speedy")]
use rust_serialization_benchmark::bench_speedy;
use rust_serialization_benchmark::generate_vec;
#[cfg(feature = "savefile")]
use rust_serialization_benchmark::bench_savefile;
use rust_serialization_benchmark::{generate_vec};

fn bench_log(c: &mut Criterion) {
use rust_serialization_benchmark::datasets::log::{Log, Logs};
Expand Down Expand Up @@ -206,6 +208,9 @@ fn bench_log(c: &mut Criterion) {

#[cfg(feature = "speedy")]
bench_speedy::bench(BENCH, c, &data);

#[cfg(feature = "savefile")]
bench_savefile::bench(BENCH, c, &data);
}

fn bench_mesh(c: &mut Criterion) {
Expand Down Expand Up @@ -347,6 +352,9 @@ fn bench_mesh(c: &mut Criterion) {

#[cfg(feature = "speedy")]
bench_speedy::bench(BENCH, c, &data);

#[cfg(feature = "savefile")]
bench_savefile::bench(BENCH, c, &data);
}

fn bench_minecraft_savedata(c: &mut Criterion) {
Expand Down Expand Up @@ -491,6 +499,9 @@ fn bench_minecraft_savedata(c: &mut Criterion) {

#[cfg(feature = "speedy")]
bench_speedy::bench(BENCH, c, &data);

#[cfg(feature = "savefile")]
bench_savefile::bench(BENCH, c, &data);
}

fn bench_mk48(c: &mut Criterion) {
Expand Down Expand Up @@ -631,6 +642,9 @@ fn bench_mk48(c: &mut Criterion) {

#[cfg(feature = "speedy")]
bench_speedy::bench(BENCH, c, &data);

#[cfg(feature = "savefile")]
bench_savefile::bench(BENCH, c, &data);
}

pub fn criterion_benchmark(c: &mut Criterion) {
Expand Down
33 changes: 33 additions & 0 deletions src/bench_savefile.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::io::Cursor;
use criterion::{black_box, Criterion};
use savefile::{Deserialize, Serialize, WithSchema};

pub fn bench<T>(name: &'static str, c: &mut Criterion, data: &T)
where
T: Serialize + Deserialize + WithSchema
{
let mut group = c.benchmark_group(format!("{}/savefile", name));

let mut serialize_buffer = Vec::new();
group.bench_function("serialize", |b| {
b.iter(|| {
serialize_buffer.clear();
savefile::save_noschema(black_box(&mut serialize_buffer), 0, black_box(data)).unwrap();
black_box(());
})
});

let mut deserialize_buffer = Vec::new();
savefile::save_noschema(&mut deserialize_buffer, 0, data).unwrap();

group.bench_function("deserialize", |b| {
b.iter(|| {
let mut reader = Cursor::new(&deserialize_buffer);
black_box(savefile::load_noschema::<T>(black_box(&mut reader), 0).unwrap());
})
});

crate::bench_size(name, "savefile", deserialize_buffer.as_slice());

group.finish();
}
6 changes: 6 additions & 0 deletions src/datasets/log/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ use crate::bench_flatbuffers;
use crate::bench_prost;
use crate::Generate;

#[cfg(feature = "savefile")]
use savefile::prelude::ReprC;

#[derive(Clone, Copy)]
#[cfg_attr(feature = "abomonation", derive(abomonation_derive::Abomonation))]
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
Expand All @@ -53,6 +56,7 @@ use crate::Generate;
)]
#[cfg_attr(feature = "speedy", derive(speedy::Readable, speedy::Writable))]
#[cfg_attr(feature = "alkahest", derive(alkahest::Schema))]
#[cfg_attr(feature = "savefile", derive(savefile_derive::ReprC, savefile_derive::Savefile), repr(C))]
pub struct Address {
pub x0: u8,
pub x1: u8,
Expand Down Expand Up @@ -145,6 +149,7 @@ impl alkahest::Pack<Address> for Address {
derive(parity_scale_codec_derive::Encode, parity_scale_codec_derive::Decode)
)]
#[cfg_attr(feature = "speedy", derive(speedy::Readable, speedy::Writable))]
#[cfg_attr(feature = "savefile", derive(savefile_derive::Savefile))]
pub struct Log {
pub address: Address,
pub identity: String,
Expand Down Expand Up @@ -349,6 +354,7 @@ impl alkahest::Pack<LogSchema> for &'_ Log {
derive(parity_scale_codec_derive::Encode, parity_scale_codec_derive::Decode)
)]
#[cfg_attr(feature = "speedy", derive(speedy::Readable, speedy::Writable))]
#[cfg_attr(feature = "savefile", derive(savefile_derive::Savefile))]
pub struct Logs {
pub logs: Vec<Log>,
}
Expand Down
6 changes: 6 additions & 0 deletions src/datasets/mesh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@ use crate::bench_flatbuffers;
use crate::bench_prost;
use crate::Generate;

#[cfg(feature = "savefile")]
use savefile::prelude::ReprC;

#[derive(Clone, Copy)]
#[cfg_attr(feature = "abomonation", derive(abomonation_derive::Abomonation))]
#[cfg_attr(feature = "bitcode", derive(bitcode::Encode, bitcode::Decode))]
Expand All @@ -52,6 +55,7 @@ use crate::Generate;
)]
#[cfg_attr(feature = "speedy", derive(speedy::Readable, speedy::Writable))]
#[cfg_attr(feature = "alkahest", derive(alkahest::Schema))]
#[cfg_attr(feature = "savefile", derive(savefile_derive::ReprC, savefile_derive::Savefile), repr(C))]
pub struct Vector3 {
pub x: f32,
pub y: f32,
Expand Down Expand Up @@ -140,6 +144,7 @@ impl alkahest::Pack<Vector3> for Vector3 {
)]
#[cfg_attr(feature = "speedy", derive(speedy::Readable, speedy::Writable))]
#[cfg_attr(feature = "alkahest", derive(alkahest::Schema))]
#[cfg_attr(feature = "savefile", derive(savefile_derive::ReprC, savefile_derive::Savefile), repr(C))]
pub struct Triangle {
pub v0: Vector3,
pub v1: Vector3,
Expand Down Expand Up @@ -238,6 +243,7 @@ impl alkahest::Pack<Triangle> for &'_ Triangle {
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
#[cfg_attr(feature = "speedy", derive(speedy::Readable, speedy::Writable))]
#[cfg_attr(feature = "savefile", derive(savefile_derive::Savefile))]
pub struct Mesh {
pub triangles: Vec<Triangle>,
}
Expand Down
9 changes: 9 additions & 0 deletions src/datasets/minecraft_savedata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ use crate::bench_flatbuffers;
#[cfg(feature = "prost")]
use crate::bench_prost;
use crate::{generate_vec, Generate};
#[cfg(feature = "savefile")]
use savefile::prelude::ReprC;

#[derive(Clone, Copy)]
#[cfg_attr(feature = "abomonation", derive(abomonation_derive::Abomonation))]
Expand All @@ -53,6 +55,7 @@ use crate::{generate_vec, Generate};
)]
#[cfg_attr(feature = "speedy", derive(speedy::Readable, speedy::Writable))]
#[cfg_attr(feature = "alkahest", derive(alkahest::Schema))]
#[cfg_attr(feature = "savefile", derive(savefile_derive::ReprC, savefile_derive::Savefile))]
#[repr(u8)]
pub enum GameType {
Survival,
Expand Down Expand Up @@ -148,6 +151,7 @@ impl alkahest::Pack<GameType> for GameType {
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
#[cfg_attr(feature = "speedy", derive(speedy::Readable, speedy::Writable))]
#[cfg_attr(feature = "savefile", derive(savefile_derive::Savefile))]
pub struct Item {
pub count: i8,
pub slot: u8,
Expand Down Expand Up @@ -265,6 +269,7 @@ impl alkahest::Pack<ItemSchema> for &'_ Item {
)]
#[cfg_attr(feature = "speedy", derive(speedy::Readable, speedy::Writable))]
#[cfg_attr(feature = "alkahest", derive(alkahest::Schema))]
#[cfg_attr(feature = "savefile", derive(savefile_derive::ReprC, savefile_derive::Savefile), repr(C))]
pub struct Abilities {
#[cfg_attr(feature = "bitcode", bitcode_hint(expected_range = "0.0..1.0"))]
pub walk_speed: f32,
Expand Down Expand Up @@ -382,6 +387,7 @@ impl alkahest::Pack<Abilities> for Abilities {
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
#[cfg_attr(feature = "speedy", derive(speedy::Readable, speedy::Writable))]
#[cfg_attr(feature = "savefile", derive(savefile_derive::Savefile))]
pub struct Entity {
pub id: String,
#[cfg_attr(feature = "bitcode", bitcode_hint(expected_range = "0.0..1.0"))]
Expand Down Expand Up @@ -642,6 +648,7 @@ impl alkahest::Pack<EntitySchema> for &'_ Entity {
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
#[cfg_attr(feature = "speedy", derive(speedy::Readable, speedy::Writable))]
#[cfg_attr(feature = "savefile", derive(savefile_derive::Savefile))]
pub struct RecipeBook {
pub recipes: Vec<String>,
pub to_be_displayed: Vec<String>,
Expand Down Expand Up @@ -842,6 +849,7 @@ impl alkahest::Pack<RecipeBookSchema> for &'_ RecipeBook {
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
#[cfg_attr(feature = "speedy", derive(speedy::Readable, speedy::Writable))]
#[cfg_attr(feature = "savefile", derive(savefile_derive::Savefile))]
pub struct Player {
pub game_type: GameType,
pub previous_game_type: GameType,
Expand Down Expand Up @@ -1287,6 +1295,7 @@ impl alkahest::Pack<PlayerSchema> for &'_ Player {
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
#[cfg_attr(feature = "speedy", derive(speedy::Readable, speedy::Writable))]
#[cfg_attr(feature = "savefile", derive(savefile_derive::Savefile))]
pub struct Players {
pub players: Vec<Player>,
}
Expand Down
9 changes: 9 additions & 0 deletions src/datasets/mk48/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ use crate::bench_flatbuffers;
#[cfg(feature = "prost")]
use crate::bench_prost;
use crate::{generate_vec, Generate};
#[cfg(feature = "savefile")]
use savefile::prelude::ReprC;

#[derive(Copy, Clone, Debug)]
#[cfg_attr(feature = "abomonation", derive(abomonation_derive::Abomonation))]
Expand All @@ -55,6 +57,7 @@ use crate::{generate_vec, Generate};
)]
#[cfg_attr(feature = "speedy", derive(speedy::Readable, speedy::Writable))]
#[cfg_attr(feature = "alkahest", derive(alkahest::Schema))]
#[cfg_attr(feature = "savefile", derive(savefile_derive::ReprC, savefile_derive::Savefile))]
#[repr(u8)]
pub enum EntityType {
#[cfg_attr(feature = "bitcode", bitcode_hint(frequency = 2.14))]
Expand Down Expand Up @@ -283,6 +286,7 @@ fn generate_velocity(rng: &mut impl Rng) -> i16 {
)]
#[cfg_attr(feature = "speedy", derive(speedy::Readable, speedy::Writable))]
#[cfg_attr(feature = "alkahest", derive(alkahest::Schema))]
#[cfg_attr(feature = "savefile", derive(savefile_derive::Savefile))]
pub struct Transform {
#[cfg_attr(feature = "bitcode", bitcode_hint(expected_range = "0..1"))]
pub altitude: i8,
Expand Down Expand Up @@ -389,6 +393,7 @@ impl alkahest::Pack<Transform> for Transform {
)]
#[cfg_attr(feature = "speedy", derive(speedy::Readable, speedy::Writable))]
#[cfg_attr(feature = "alkahest", derive(alkahest::Schema))]
#[cfg_attr(feature = "savefile", derive(savefile_derive::ReprC, savefile_derive::Savefile), repr(C))]
pub struct Guidance {
pub angle: u16,
pub submerge: bool,
Expand Down Expand Up @@ -476,6 +481,7 @@ impl alkahest::Pack<Guidance> for Guidance {
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
#[cfg_attr(feature = "speedy", derive(speedy::Readable, speedy::Writable))]
#[cfg_attr(feature = "savefile", derive(savefile_derive::Savefile))]
pub struct Contact {
#[cfg_attr(feature = "bitcode", bitcode_hint(expected_range = "0..1"))]
pub damage: u8,
Expand Down Expand Up @@ -678,6 +684,7 @@ impl alkahest::Pack<ContactSchema> for &'_ Contact {
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
#[cfg_attr(feature = "speedy", derive(speedy::Readable, speedy::Writable))]
#[cfg_attr(feature = "savefile", derive(savefile_derive::Savefile))]
pub struct TerrainUpdate {
#[cfg_attr(feature = "bitcode", bitcode_hint(gamma))]
chunk_id: (i8, i8),
Expand Down Expand Up @@ -798,6 +805,7 @@ impl alkahest::Pack<TerrainUpdateSchema> for &'_ TerrainUpdate {
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
#[cfg_attr(feature = "speedy", derive(speedy::Readable, speedy::Writable))]
#[cfg_attr(feature = "savefile", derive(savefile_derive::Savefile))]
pub struct Update {
pub contacts: Vec<Contact>,
#[cfg_attr(feature = "bitcode", bitcode_hint(expected_range = "0..5000"))]
Expand Down Expand Up @@ -951,6 +959,7 @@ impl alkahest::Pack<UpdateSchema> for &'_ Update {
derive(simd_json_derive::Serialize, simd_json_derive::Deserialize)
)]
#[cfg_attr(feature = "speedy", derive(speedy::Readable, speedy::Writable))]
#[cfg_attr(feature = "savefile", derive(savefile_derive::Savefile))]
pub struct Updates {
pub updates: Vec<Update>,
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ pub mod bench_serde_json;
pub mod bench_simd_json;
#[cfg(feature = "speedy")]
pub mod bench_speedy;
#[cfg(feature = "savefile")]
pub mod bench_savefile;
pub mod datasets;

use core::{mem, ops};
Expand Down

0 comments on commit 0516622

Please sign in to comment.