Skip to content

Commit

Permalink
Update wiring 0.2.0 (#79)
Browse files Browse the repository at this point in the history
* Update wiring 0.2.0

* Apply changes
  • Loading branch information
louaykamel authored Apr 27, 2024
1 parent 077811d commit ccc7749
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 43 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.1", 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
47 changes: 11 additions & 36 deletions src/bench_wiring.rs
Original file line number Diff line number Diff line change
@@ -1,54 +1,29 @@
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, Wire, 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);

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,
);
b.iter(|| black_box(BufWire::new(&mut wire).wire(black_box(data)).unwrap()))
});

rt.block_on(async {
(&mut wire).wire(data).await.unwrap();
});
wire.sync_wire(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();
}
2 changes: 2 additions & 0 deletions src/datasets/log/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ use crate::Generate;
#[cfg_attr(feature = "wiring", derive(Wiring, Unwiring))]
pub struct Address {
#[cfg_attr(feature = "bilrost", bilrost(encoding(varint)))]
#[cfg_attr(feature = "wiring", fixed)]
pub x0: u8,
#[cfg_attr(feature = "bilrost", bilrost(encoding(varint)))]
pub x1: u8,
Expand Down Expand Up @@ -182,6 +183,7 @@ pub struct Log {
pub userid: String,
pub date: String,
pub request: String,
#[cfg_attr(feature = "wiring", fixed)]
pub code: u16,
pub size: u64,
}
Expand Down
2 changes: 2 additions & 0 deletions src/datasets/mesh/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ 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", fixed)]
pub x: f32,
pub y: f32,
pub z: f32,
Expand Down Expand Up @@ -166,6 +167,7 @@ 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", fixed)]
pub v0: Vector3,
pub v1: Vector3,
pub v2: Vector3,
Expand Down
8 changes: 8 additions & 0 deletions src/datasets/minecraft_savedata/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ 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", fixed(2))]
pub count: i8,
#[cfg_attr(feature = "bilrost", bilrost(encoding(varint)))]
pub slot: u8,
Expand Down Expand Up @@ -318,6 +319,7 @@ 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", fixed)]
pub walk_speed: f32,
pub fly_speed: f32,
pub may_fly: bool,
Expand Down Expand Up @@ -455,6 +457,7 @@ impl alkahest::Pack<Abilities> for Abilities {
#[cfg_attr(feature = "wiring", derive(Wiring, Unwiring))]
pub struct Entity {
pub id: String,
#[cfg_attr(feature = "wiring", fixed(11))]
pub pos: (f64, f64, f64),
pub motion: (f64, f64, f64),
pub rotation: (f32, f32),
Expand All @@ -468,6 +471,7 @@ pub struct Entity {
#[cfg_attr(feature = "bilrost", bilrost(encoding = "packed<fixed>"))]
pub uuid: [u32; 4],
pub custom_name: Option<String>,
#[cfg_attr(feature = "wiring", fixed)]
pub custom_name_visible: bool,
pub silent: bool,
pub glowing: bool,
Expand Down Expand Up @@ -768,6 +772,7 @@ pub struct RecipeBook {
pub recipes: Vec<String>,
#[cfg_attr(feature = "bilrost", bilrost(encoding(packed)))]
pub to_be_displayed: Vec<String>,
#[cfg_attr(feature = "wiring", fixed)]
pub is_filtering_craftable: bool,
pub is_gui_open: bool,
pub is_furnace_filtering_craftable: bool,
Expand Down Expand Up @@ -992,17 +997,20 @@ 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", fixed(3))]
pub game_type: GameType,
pub previous_game_type: GameType,
pub score: i64,
pub dimension: String,
pub selected_item_slot: u32,
pub selected_item: Item,
pub spawn_dimension: Option<String>,
#[cfg_attr(feature = "wiring", fixed(3))]
pub spawn_x: i64,
pub spawn_y: i64,
pub spawn_z: i64,
pub spawn_forced: Option<bool>,
#[cfg_attr(feature = "wiring", fixed(8))]
pub sleep_timer: u16,
pub food_exhaustion_level: f32,
pub food_saturation_level: f32,
Expand Down
4 changes: 4 additions & 0 deletions src/datasets/mk48/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,7 @@ 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", fixed)]
pub altitude: i8,
pub angle: u16,
pub position: (f32, f32),
Expand Down Expand Up @@ -448,6 +449,7 @@ 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", fixed)]
pub angle: u16,
pub submerge: bool,
pub velocity: i16,
Expand Down Expand Up @@ -553,6 +555,7 @@ 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", fixed(2))]
pub damage: u8,
pub entity_id: u32,
pub entity_type: Option<EntityType>,
Expand Down Expand Up @@ -928,6 +931,7 @@ impl alkahest::Pack<TerrainUpdateSchema> for &'_ TerrainUpdate {
pub struct Update {
#[cfg_attr(feature = "bilrost", bilrost(encoding(packed)))]
pub contacts: Vec<Contact>,
#[cfg_attr(feature = "wiring", fixed(2))]
pub score: u32,
pub world_radius: f32,
#[cfg_attr(feature = "bilrost", bilrost(encoding(packed)))]
Expand Down

0 comments on commit ccc7749

Please sign in to comment.