Skip to content

Commit

Permalink
Update wiring 0.2.0
Browse files Browse the repository at this point in the history
  • Loading branch information
louaykamel committed Apr 26, 2024
1 parent 077811d commit 8057b59
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 50 deletions.
4 changes: 1 addition & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ simd-json-derive = { version = "=0.13.0", optional = true }
speedy = { version = "=0.8.7", optional = true }
savefile = { version = "=0.16.5", optional = true }
savefile-derive = { version = "=0.16.5", optional = true }
tokio = { version = "1.37", features = ["full"], optional = true }
wiring = { version = "0.1", optional = true }
wiring = { version = "=0.2.0", optional = true }
zstd = "=0.12.4"

[features]
Expand Down Expand Up @@ -140,7 +139,6 @@ prost = ["dep:capnp", "dep:prost"]
simd-json = ["dep:simd-json", "simd-json-derive"]
savefile = ["dep:savefile", "savefile-derive"]
scale = ["parity-scale-codec", "parity-scale-codec-derive"]
wiring = ["dep:wiring", "dep:tokio", "criterion/async_tokio"]

# Enable these features to regenerate generated files rather than using the committed versions.
regenerate-capnp = ["dep:capnpc"]
Expand Down
8 changes: 4 additions & 4 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ fn bench_log(c: &mut Criterion) {
bench_nanoserde::bench(BENCH, c, &data);

#[cfg(feature = "wiring")]
bench_wiring::bench::<Logs>(BENCH, c, &data);
bench_wiring::bench(BENCH, c, &data);
}

fn bench_mesh(c: &mut Criterion) {
Expand Down Expand Up @@ -412,7 +412,7 @@ fn bench_mesh(c: &mut Criterion) {
bench_nanoserde::bench(BENCH, c, &data);

#[cfg(feature = "wiring")]
bench_wiring::bench::<Mesh>(BENCH, c, &data);
bench_wiring::bench(BENCH, c, &data);
}

fn bench_minecraft_savedata(c: &mut Criterion) {
Expand Down Expand Up @@ -580,7 +580,7 @@ fn bench_minecraft_savedata(c: &mut Criterion) {
bench_nanoserde::bench(BENCH, c, &data);

#[cfg(feature = "wiring")]
bench_wiring::bench::<Players>(BENCH, c, &data);
bench_wiring::bench(BENCH, c, &data);
}

fn bench_mk48(c: &mut Criterion) {
Expand Down Expand Up @@ -744,7 +744,7 @@ fn bench_mk48(c: &mut Criterion) {
bench_nanoserde::bench(BENCH, c, &data);

#[cfg(feature = "wiring")]
bench_wiring::bench::<Updates>(BENCH, c, &data);
bench_wiring::bench(BENCH, c, &data);
}

#[cfg(feature = "pprof")]
Expand Down
55 changes: 16 additions & 39 deletions src/bench_wiring.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,31 @@
use criterion::{black_box, Criterion};
use tokio::runtime::Runtime;
use wiring::prelude::{Unwire, Unwiring, Wire, Wiring};
use zstd::zstd_safe::WriteBuf;

pub fn bench<'a, T>(name: &'static str, c: &mut Criterion, data: &'a T)
where
&'a T: Wiring,
T: Unwiring + PartialEq,
{
async fn wire_data<W: Wire, T: Wiring>(mut wire: W, data: T) {
(&mut wire).wire(data).await.unwrap();
}

let rt = Runtime::new().unwrap();
const BUFFER_LEN: usize = 50_000_000;
use wiring::prelude::{BufUnWire, BufWire, Unwiring, Wiring};

pub fn bench<T: Wiring + Unwiring + PartialEq>(name: &'static str, c: &mut Criterion, data: &T) {
const BUFFER_LEN: usize = 10_000_000;

let mut group = c.benchmark_group(format!("{}/wiring", name));

let mut wire: Vec<u8> = Vec::with_capacity(BUFFER_LEN);
let mut wire: Vec<u8> = Vec::new();
wire.reserve(BUFFER_LEN); // Optional

group.bench_function("serialize", |b| {
b.to_async(Runtime::new().unwrap()).iter_batched(
|| wire.clone(),
|w| wire_data(black_box(w), black_box(data)),
criterion::BatchSize::SmallInput,
);
});

rt.block_on(async {
(&mut wire).wire(data).await.unwrap();
b.iter(|| {
black_box(BufWire::new(&mut wire).wire(black_box(data)).unwrap());
})
});

let mut unwire = std::io::Cursor::new(wire);

let buffer = wire.as_slice();
group.bench_function("deserialize", |b| {
b.to_async(Runtime::new().unwrap()).iter_batched(
|| unwire.clone(),
|w| async move {
black_box(w).unwire::<T>().await.unwrap();
},
criterion::BatchSize::SmallInput,
);
b.iter(|| {
black_box(BufUnWire::new(black_box(buffer)).unwire::<T>().unwrap());
})
});

crate::bench_size(name, "wiring", unwire.as_slice());
crate::bench_size(name, "wiring", wire.as_slice());

rt.block_on(async {
let unwired = unwire.unwire::<T>().await.unwrap();
assert!(&unwired == data);
});
let unwired: T = BufUnWire::new(buffer).unwire().unwrap();
assert!(&unwired == data);

group.finish();
}
8 changes: 7 additions & 1 deletion src/datasets/log/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rand::Rng;
#[cfg(feature = "rkyv")]
use rkyv::Archived;
#[cfg(feature = "wiring")]
use wiring::prelude::{Unwiring, Wiring};
use wiring::prelude::{concat_end, concat_mid, concat_start, Unwiring, Wiring};

#[cfg(feature = "capnp")]
use crate::bench_capnp;
Expand Down Expand Up @@ -64,12 +64,16 @@ use crate::Generate;
#[cfg_attr(feature = "wiring", derive(Wiring, Unwiring))]
pub struct Address {
#[cfg_attr(feature = "bilrost", bilrost(encoding(varint)))]
#[cfg_attr(feature = "wiring", concat_start)]
pub x0: u8,
#[cfg_attr(feature = "bilrost", bilrost(encoding(varint)))]
#[cfg_attr(feature = "wiring", concat_mid)]
pub x1: u8,
#[cfg_attr(feature = "bilrost", bilrost(encoding(varint)))]
#[cfg_attr(feature = "wiring", concat_mid)]
pub x2: u8,
#[cfg_attr(feature = "bilrost", bilrost(encoding(varint)))]
#[cfg_attr(feature = "wiring", concat_end)]
pub x3: u8,
}

Expand Down Expand Up @@ -182,7 +186,9 @@ pub struct Log {
pub userid: String,
pub date: String,
pub request: String,
#[cfg_attr(feature = "wiring", concat_start)]
pub code: u16,
#[cfg_attr(feature = "wiring", concat_end)]
pub size: u64,
}

Expand Down
9 changes: 8 additions & 1 deletion src/datasets/mesh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use rand::Rng;
#[cfg(feature = "rkyv")]
use rkyv::Archived;
#[cfg(feature = "wiring")]
use wiring::prelude::{Unwiring, Wiring};
use wiring::prelude::{concat_end, concat_mid, concat_start, Unwiring, Wiring};

#[cfg(feature = "capnp")]
use crate::bench_capnp;
Expand Down Expand Up @@ -61,8 +61,11 @@ use crate::Generate;
#[cfg_attr(feature = "nanoserde", derive(nanoserde::SerBin, nanoserde::DeBin))]
#[cfg_attr(feature = "wiring", derive(Wiring, Unwiring))]
pub struct Vector3 {
#[cfg_attr(feature = "wiring", concat_start)]
pub x: f32,
#[cfg_attr(feature = "wiring", concat_mid)]
pub y: f32,
#[cfg_attr(feature = "wiring", concat_end)]
pub z: f32,
}

Expand Down Expand Up @@ -166,9 +169,13 @@ impl alkahest::Pack<Vector3> for Vector3 {
#[cfg_attr(feature = "nanoserde", derive(nanoserde::SerBin, nanoserde::DeBin))]
#[cfg_attr(feature = "wiring", derive(Wiring, Unwiring))]
pub struct Triangle {
#[cfg_attr(feature = "wiring", concat_start)]
pub v0: Vector3,
#[cfg_attr(feature = "wiring", concat_mid)]
pub v1: Vector3,
#[cfg_attr(feature = "wiring", concat_mid)]
pub v2: Vector3,
#[cfg_attr(feature = "wiring", concat_end)]
pub normal: Vector3,
}

Expand Down
47 changes: 46 additions & 1 deletion src/datasets/minecraft_savedata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use rand::Rng;
#[cfg(feature = "rkyv")]
use rkyv::Archived;
#[cfg(feature = "wiring")]
use wiring::prelude::{Unwiring, Wiring};
use wiring::prelude::{concat_end, concat_mid, concat_start, Unwiring, Wiring};

#[cfg(feature = "capnp")]
use crate::bench_capnp;
Expand Down Expand Up @@ -181,8 +181,10 @@ impl alkahest::Pack<GameType> for GameType {
#[cfg_attr(feature = "wiring", derive(Wiring, Unwiring))]
pub struct Item {
#[cfg_attr(feature = "bilrost", bilrost(encoding(varint)))]
#[cfg_attr(feature = "wiring", concat_start)]
pub count: i8,
#[cfg_attr(feature = "bilrost", bilrost(encoding(varint)))]
#[cfg_attr(feature = "wiring", concat_end)]
pub slot: u8,
pub id: String,
}
Expand Down Expand Up @@ -318,12 +320,19 @@ impl alkahest::Pack<ItemSchema> for &'_ Item {
#[cfg_attr(feature = "nanoserde", derive(nanoserde::SerBin, nanoserde::DeBin))]
#[cfg_attr(feature = "wiring", derive(Wiring, Unwiring))]
pub struct Abilities {
#[cfg_attr(feature = "wiring", concat_start)]
pub walk_speed: f32,
#[cfg_attr(feature = "wiring", concat_mid)]
pub fly_speed: f32,
#[cfg_attr(feature = "wiring", concat_mid)]
pub may_fly: bool,
#[cfg_attr(feature = "wiring", concat_mid)]
pub flying: bool,
#[cfg_attr(feature = "wiring", concat_mid)]
pub invulnerable: bool,
#[cfg_attr(feature = "wiring", concat_mid)]
pub may_build: bool,
#[cfg_attr(feature = "wiring", concat_end)]
pub instabuild: bool,
}

Expand Down Expand Up @@ -455,21 +464,35 @@ impl alkahest::Pack<Abilities> for Abilities {
#[cfg_attr(feature = "wiring", derive(Wiring, Unwiring))]
pub struct Entity {
pub id: String,
#[cfg_attr(feature = "wiring", concat_start)]
pub pos: (f64, f64, f64),
#[cfg_attr(feature = "wiring", concat_mid)]
pub motion: (f64, f64, f64),
#[cfg_attr(feature = "wiring", concat_mid)]
pub rotation: (f32, f32),
#[cfg_attr(feature = "wiring", concat_mid)]
pub fall_distance: f32,
#[cfg_attr(feature = "wiring", concat_mid)]
pub fire: u16,
#[cfg_attr(feature = "wiring", concat_mid)]
pub air: u16,
#[cfg_attr(feature = "wiring", concat_mid)]
pub on_ground: bool,
#[cfg_attr(feature = "wiring", concat_mid)]
pub no_gravity: bool,
#[cfg_attr(feature = "wiring", concat_mid)]
pub invulnerable: bool,
#[cfg_attr(feature = "wiring", concat_mid)]
pub portal_cooldown: i32,
#[cfg_attr(feature = "wiring", concat_end)]
#[cfg_attr(feature = "bilrost", bilrost(encoding = "packed<fixed>"))]
pub uuid: [u32; 4],
pub custom_name: Option<String>,
#[cfg_attr(feature = "wiring", concat_start)]
pub custom_name_visible: bool,
#[cfg_attr(feature = "wiring", concat_mid)]
pub silent: bool,
#[cfg_attr(feature = "wiring", concat_end)]
pub glowing: bool,
}

Expand Down Expand Up @@ -768,13 +791,21 @@ pub struct RecipeBook {
pub recipes: Vec<String>,
#[cfg_attr(feature = "bilrost", bilrost(encoding(packed)))]
pub to_be_displayed: Vec<String>,
#[cfg_attr(feature = "wiring", concat_start)]
pub is_filtering_craftable: bool,
#[cfg_attr(feature = "wiring", concat_mid)]
pub is_gui_open: bool,
#[cfg_attr(feature = "wiring", concat_mid)]
pub is_furnace_filtering_craftable: bool,
#[cfg_attr(feature = "wiring", concat_mid)]
pub is_furnace_gui_open: bool,
#[cfg_attr(feature = "wiring", concat_mid)]
pub is_blasting_furnace_filtering_craftable: bool,
#[cfg_attr(feature = "wiring", concat_mid)]
pub is_blasting_furnace_gui_open: bool,
#[cfg_attr(feature = "wiring", concat_mid)]
pub is_smoker_filtering_craftable: bool,
#[cfg_attr(feature = "wiring", concat_end)]
pub is_smoker_gui_open: bool,
}

Expand Down Expand Up @@ -992,24 +1023,38 @@ impl alkahest::Pack<RecipeBookSchema> for &'_ RecipeBook {
#[cfg_attr(feature = "nanoserde", derive(nanoserde::SerBin, nanoserde::DeBin))]
#[cfg_attr(feature = "wiring", derive(Wiring, Unwiring))]
pub struct Player {
#[cfg_attr(feature = "wiring", concat_start)]
pub game_type: GameType,
#[cfg_attr(feature = "wiring", concat_mid)]
pub previous_game_type: GameType,
#[cfg_attr(feature = "wiring", concat_end)]
pub score: i64,
pub dimension: String,
pub selected_item_slot: u32,
pub selected_item: Item,
pub spawn_dimension: Option<String>,
#[cfg_attr(feature = "wiring", concat_start)]
pub spawn_x: i64,
#[cfg_attr(feature = "wiring", concat_mid)]
pub spawn_y: i64,
#[cfg_attr(feature = "wiring", concat_end)]
pub spawn_z: i64,
pub spawn_forced: Option<bool>,
#[cfg_attr(feature = "wiring", concat_start)]
pub sleep_timer: u16,
#[cfg_attr(feature = "wiring", concat_mid)]
pub food_exhaustion_level: f32,
#[cfg_attr(feature = "wiring", concat_mid)]
pub food_saturation_level: f32,
#[cfg_attr(feature = "wiring", concat_mid)]
pub food_tick_timer: u32,
#[cfg_attr(feature = "wiring", concat_mid)]
pub xp_level: u32,
#[cfg_attr(feature = "wiring", concat_mid)]
pub xp_p: f32,
#[cfg_attr(feature = "wiring", concat_mid)]
pub xp_total: i32,
#[cfg_attr(feature = "wiring", concat_end)]
pub xp_seed: i32,
pub inventory: Vec<Item>,
pub ender_items: Vec<Item>,
Expand Down
13 changes: 12 additions & 1 deletion src/datasets/mk48/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use rand::Rng;
#[cfg(feature = "rkyv")]
use rkyv::Archived;
#[cfg(feature = "wiring")]
use wiring::prelude::{Unwiring, Wiring};
use wiring::prelude::{concat_end, concat_mid, concat_start, Unwiring, Wiring};

#[cfg(feature = "capnp")]
use crate::bench_capnp;
Expand Down Expand Up @@ -318,9 +318,13 @@ fn generate_velocity(rng: &mut impl Rng) -> i16 {
#[cfg_attr(feature = "wiring", derive(Wiring, Unwiring))]
pub struct Transform {
#[cfg_attr(feature = "bilrost", bilrost(encoding(varint)))]
#[cfg_attr(feature = "wiring", concat_start)]
pub altitude: i8,
#[cfg_attr(feature = "wiring", concat_mid)]
pub angle: u16,
#[cfg_attr(feature = "wiring", concat_mid)]
pub position: (f32, f32),
#[cfg_attr(feature = "wiring", concat_end)]
pub velocity: i16,
}

Expand Down Expand Up @@ -448,8 +452,11 @@ impl alkahest::Pack<Transform> for Transform {
#[cfg_attr(feature = "nanoserde", derive(nanoserde::SerBin, nanoserde::DeBin))]
#[cfg_attr(feature = "wiring", derive(Wiring, Unwiring))]
pub struct Guidance {
#[cfg_attr(feature = "wiring", concat_start)]
pub angle: u16,
#[cfg_attr(feature = "wiring", concat_mid)]
pub submerge: bool,
#[cfg_attr(feature = "wiring", concat_end)]
pub velocity: i16,
}

Expand Down Expand Up @@ -553,7 +560,9 @@ impl alkahest::Pack<Guidance> for Guidance {
#[cfg_attr(feature = "wiring", derive(Wiring, Unwiring))]
pub struct Contact {
#[cfg_attr(feature = "bilrost", bilrost(encoding(varint)))]
#[cfg_attr(feature = "wiring", concat_start)]
pub damage: u8,
#[cfg_attr(feature = "wiring", concat_end)]
pub entity_id: u32,
pub entity_type: Option<EntityType>,
pub guidance: Guidance,
Expand Down Expand Up @@ -928,7 +937,9 @@ impl alkahest::Pack<TerrainUpdateSchema> for &'_ TerrainUpdate {
pub struct Update {
#[cfg_attr(feature = "bilrost", bilrost(encoding(packed)))]
pub contacts: Vec<Contact>,
#[cfg_attr(feature = "wiring", concat_start)]
pub score: u32,
#[cfg_attr(feature = "wiring", concat_end)]
pub world_radius: f32,
#[cfg_attr(feature = "bilrost", bilrost(encoding(packed)))]
pub terrain_updates: Vec<TerrainUpdate>,
Expand Down

0 comments on commit 8057b59

Please sign in to comment.