From 537687dd926ec681eab705cbb55dd242e70d932e Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Tue, 12 Sep 2023 19:08:25 +0200 Subject: [PATCH 1/2] fix build warnings and a few assorted tweaks --- benchmark/Cargo.toml | 7 +- benchmark/benches/benchmark.rs | 119 ++++++++++++++++++++++++++++++--- src/primitive.rs | 4 +- src/serialize.rs | 1 + src/tests/net.rs | 68 +++++++++++++------ 5 files changed, 164 insertions(+), 35 deletions(-) diff --git a/benchmark/Cargo.toml b/benchmark/Cargo.toml index 2748b23..2b055fb 100644 --- a/benchmark/Cargo.toml +++ b/benchmark/Cargo.toml @@ -4,11 +4,14 @@ version = "0.0.0" publish = false [features] -default = ["alkahest/default"] + +nightly = ["criterion/real_blackbox"] + +default = ["alkahest/default", "speedy", "rkyv", "serde", "bincode"] [dependencies] alkahest = { path = "..", features = ["derive"], default-features = false } -criterion = { version = "0.4", features = ["real_blackbox"] } +criterion = { version = "0.4" } serde = { version = "1.0", features = ["derive"], optional = true } serde_json = { version = "1.0", optional = true } bincode = { version = "1.3", optional = true } diff --git a/benchmark/benches/benchmark.rs b/benchmark/benches/benchmark.rs index de38151..fbfe73d 100644 --- a/benchmark/benches/benchmark.rs +++ b/benchmark/benches/benchmark.rs @@ -11,7 +11,7 @@ extern crate rkyv; #[cfg(feature = "speedy")] extern crate speedy; -use alkahest::{alkahest, Deserialize, Formula, Lazy, Ref, SerIter, Serialize}; +use alkahest::{alkahest, Deserialize, Formula, Lazy, SerIter, Serialize}; use criterion::{black_box, criterion_group, criterion_main, Criterion}; #[cfg(feature = "rkyv")] @@ -19,12 +19,12 @@ use bytecheck::CheckBytes; use rand::{ distributions::{Alphanumeric, DistString}, rngs::SmallRng, - thread_rng, Rng, SeedableRng, + Rng, SeedableRng, }; #[derive(Debug, Clone, Formula, Serialize, Deserialize)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize))] +#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] #[cfg_attr(feature = "rkyv", archive_attr(derive(CheckBytes)))] #[cfg_attr(feature = "speedy", derive(speedy::Writable, speedy::Readable))] pub enum GameMessage { @@ -42,7 +42,7 @@ pub enum GameMessageRead<'de> { #[derive(Debug, Clone, Formula, Serialize, Deserialize)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize))] +#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] #[cfg_attr(feature = "rkyv", archive_attr(derive(CheckBytes)))] #[cfg_attr(feature = "speedy", derive(speedy::Writable, speedy::Readable))] pub enum ClientMessage { @@ -60,7 +60,7 @@ pub enum ClientMessageRead<'de> { #[derive(Debug, Clone, Formula, Serialize, Deserialize)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize))] +#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] #[cfg_attr(feature = "rkyv", archive_attr(derive(CheckBytes)))] #[cfg_attr(feature = "speedy", derive(speedy::Writable, speedy::Readable))] pub enum ServerMessage { @@ -78,7 +78,7 @@ pub enum ServerMessageRead<'de> { #[derive(Debug, Formula, Serialize, Deserialize)] #[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] -#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize))] +#[cfg_attr(feature = "rkyv", derive(rkyv::Archive, rkyv::Serialize, rkyv::Deserialize))] #[cfg_attr(feature = "rkyv", archive_attr(derive(CheckBytes)))] #[cfg_attr(feature = "speedy", derive(speedy::Writable, speedy::Readable))] pub struct NetPacket { @@ -123,9 +123,9 @@ fn messages<'a>(mut rng: impl Rng + 'a, len: usize) -> impl Iterator, + NetPacket, + >(&buffer[..size]) + .unwrap(); + + for message in packet.game_messages.iter() { + match message { + GameMessage::Client(ClientMessage::ClientData { + nickname, + clan, + }) => { + black_box(nickname); + black_box(clan); + } + GameMessage::Client(ClientMessage::Chat(message)) => { + black_box(message); + } + GameMessage::Server(ServerMessage::ServerData(data)) => { + black_box(data); + } + GameMessage::Server(ServerMessage::ClientChat { + client_id, + message, + }) => { + black_box(client_id); + black_box(message); + } + } + } + }) + }); } #[cfg(feature = "bincode")] @@ -194,7 +229,7 @@ pub fn criterion_benchmark(c: &mut Criterion) { }) }); - group.bench_function("read", |b| { + group.bench_function("deserialize", |b| { b.iter(|| { let packet = bincode::deserialize::>(&buffer).unwrap(); @@ -268,6 +303,39 @@ pub fn criterion_benchmark(c: &mut Criterion) { } }) }); + + group.bench_function("deserialize", |b| { + b.iter(|| { + use rkyv::Deserialize; + let archive = rkyv::check_archived_root::>(&vec[..]).unwrap(); + let packet: NetPacket = archive.deserialize(&mut rkyv::Infallible).unwrap(); + + for message in packet.game_messages.iter() { + match message { + GameMessage::Client(ClientMessage::ClientData { + nickname, + clan, + }) => { + black_box(nickname); + black_box(clan); + } + GameMessage::Client(ClientMessage::Chat(message)) => { + black_box(message); + } + GameMessage::Server(ServerMessage::ServerData(data)) => { + black_box(data); + } + GameMessage::Server(ServerMessage::ClientChat { + client_id, + message, + }) => { + black_box(client_id); + black_box(message); + } + } + } + }) + }); } #[cfg(feature = "speedy")] @@ -321,6 +389,39 @@ pub fn criterion_benchmark(c: &mut Criterion) { } }) }); + + group.bench_function("deserialize", |b| { + b.iter(|| { + let packet = + as speedy::Readable<_>>::read_from_buffer(&buffer) + .unwrap(); + + for message in packet.game_messages.iter() { + match message { + GameMessage::Client(ClientMessage::ClientData { + nickname, + clan, + }) => { + black_box(nickname); + black_box(clan); + } + GameMessage::Client(ClientMessage::Chat(message)) => { + black_box(message); + } + GameMessage::Server(ServerMessage::ServerData(data)) => { + black_box(data); + } + GameMessage::Server(ServerMessage::ClientChat { + client_id, + message, + }) => { + black_box(client_id); + black_box(message); + } + } + } + }) + }); } } diff --git a/src/primitive.rs b/src/primitive.rs index 3bbf0d5..a97f86b 100644 --- a/src/primitive.rs +++ b/src/primitive.rs @@ -1,4 +1,4 @@ -use core::{borrow::Borrow, mem::size_of}; +use core::mem::size_of; use crate::{ buffer::Buffer, @@ -157,7 +157,7 @@ impl Serialize for &bool { where B: Buffer, { - >::serialize(u8::from(*self.borrow()), sizes, buffer) + >::serialize(u8::from(*self), sizes, buffer) } #[inline(always)] diff --git a/src/serialize.rs b/src/serialize.rs index 5e79a47..1b7b1bc 100644 --- a/src/serialize.rs +++ b/src/serialize.rs @@ -496,6 +496,7 @@ where let size = FixedUsize::truncate_unchecked(size); if F::EXACT_SIZE { + debug_assert_eq!(size, FixedUsize::truncate_unchecked(F::MAX_STACK_SIZE.unwrap())); buffer.write_stack(heap, stack, &address.to_le_bytes())?; } else { buffer.write_stack(heap, stack, &size.to_le_bytes())?; diff --git a/src/tests/net.rs b/src/tests/net.rs index fd0cf3d..f8e5abd 100644 --- a/src/tests/net.rs +++ b/src/tests/net.rs @@ -6,68 +6,92 @@ use rand::{ }; use crate::{ - alkahest, read_packet, write_packet_to_vec, Formula, Lazy, SerIter, Serialize, SerializeRef, + alkahest, read_packet, write_packet_to_vec, Deserialize, Formula, Lazy, SerIter, Serialize, SerializeRef, Ref, }; +#[alkahest(Formula)] +pub enum GameMessageFormula { + Client(Ref), + Server(Ref), +} + #[derive(Debug, Clone, PartialEq, Eq)] -#[alkahest(Formula, Serialize, Deserialize)] +#[alkahest(Serialize, Deserialize<'_, GameMessageFormula>)] pub enum GameMessage { Client(ClientMessage), Server(ServerMessage), } #[derive(Debug)] -#[alkahest(Deserialize<'de, GameMessage>)] +#[alkahest(Deserialize<'de, GameMessageFormula>)] pub enum GameMessageRead<'de> { Client(ClientMessageRead<'de>), Server(ServerMessageRead<'de>), } +#[alkahest(Formula)] +pub enum ClientMessageFormula { + ClientData { nickname: Ref, clan: Ref }, + Chat(Ref), +} + #[derive(Debug, PartialEq, Eq, Clone)] -#[alkahest(Formula, Serialize, Deserialize)] +#[alkahest(Serialize, Deserialize<'_, ClientMessageFormula>)] pub enum ClientMessage { ClientData { nickname: String, clan: String }, Chat(String), } #[derive(Debug)] -#[alkahest(Deserialize<'de, ClientMessage>)] +#[alkahest(Deserialize<'de, ClientMessageFormula>)] pub enum ClientMessageRead<'de> { ClientData { nickname: &'de str, clan: &'de str }, Chat(&'de str), } +#[alkahest(Formula)] +pub enum ServerMessageFormula { + ServerData(u64), + ClientChat { client_id: u64, message: Ref }, +} + #[derive(Debug, PartialEq, Eq, Clone)] -#[alkahest(Formula, Serialize, Deserialize)] +#[alkahest(Serialize, Deserialize<'_, ServerMessageFormula>)] pub enum ServerMessage { ServerData(u64), ClientChat { client_id: u64, message: String }, } #[derive(Debug)] -#[alkahest(Deserialize<'de, ServerMessage>)] +#[alkahest(Deserialize<'de, ServerMessageFormula>)] pub enum ServerMessageRead<'de> { ServerData(u64), ClientChat { client_id: u64, message: &'de str }, } +#[alkahest(Formula)] +pub struct NetPacketFormula { + pub game_messages: Vec, +} + #[derive(Debug)] -#[alkahest(Formula, Serialize, Deserialize)] +#[alkahest(for Serialize> where G: Serialize)] +#[alkahest(for<'de, F: Formula> Deserialize<'de, NetPacketFormula> where G: Deserialize<'de, F>)] pub struct NetPacket { pub game_messages: Vec, } #[derive(Debug)] -#[alkahest(for Serialize> where G: Serialize<[X]>)] -#[alkahest(for SerializeRef> where G: SerializeRef<[X]>)] +#[alkahest(for Serialize> where G: Serialize<[F]>)] +#[alkahest(for SerializeRef> where G: SerializeRef<[F]>)] pub struct NetPacketWrite { pub game_messages: G, } #[derive(Debug)] -#[alkahest(Deserialize<'de, NetPacket::> where G: Formula)] -pub struct NetPacketRead<'de, G> { - pub game_messages: Lazy<'de, [G]>, +#[alkahest(Deserialize<'de, NetPacketFormula> where F: Formula)] +pub struct NetPacketRead<'de, F> { + pub game_messages: Lazy<'de, [F]>, } fn get_string(rng: &mut impl Rng) -> String { @@ -103,7 +127,7 @@ fn test_net_packet() { const LEN: usize = 1000; let mut buffer = Vec::new(); - let size = write_packet_to_vec::, _>( + let size = write_packet_to_vec::, _>( NetPacketWrite { game_messages: SerIter(messages(rng.clone(), LEN)), }, @@ -111,7 +135,7 @@ fn test_net_packet() { ); let mut buffer2 = Vec::new(); - let size2 = write_packet_to_vec::, _>( + let size2 = write_packet_to_vec::, _>( NetPacket { game_messages: messages(rng, LEN).collect::>(), }, @@ -122,23 +146,23 @@ fn test_net_packet() { assert_eq!(buffer[..size], buffer2[..size]); let (packet, _) = - read_packet::, NetPacketRead>(&buffer[..]).unwrap(); + read_packet::, NetPacketRead>(&buffer[..]).unwrap(); for message in packet.game_messages.iter::() { match message.unwrap() { GameMessageRead::Client(ClientMessageRead::ClientData { nickname, clan }) => { - drop(nickname); - drop(clan); + let _ = nickname; + let _ = clan; } GameMessageRead::Client(ClientMessageRead::Chat(message)) => { - drop(message); + let _ = message; } GameMessageRead::Server(ServerMessageRead::ServerData(data)) => { - drop(data); + let _ = data; } GameMessageRead::Server(ServerMessageRead::ClientChat { client_id, message }) => { - drop(client_id); - drop(message); + let _ = client_id; + let _ = message; } } } From 37130bb606dc1a47801d75382c43e5c60c442264 Mon Sep 17 00:00:00 2001 From: Gray Olson Date: Wed, 13 Sep 2023 09:58:35 +0200 Subject: [PATCH 2/2] remove unnecessary ref wrappers --- src/tests/net.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/tests/net.rs b/src/tests/net.rs index f8e5abd..6a85d62 100644 --- a/src/tests/net.rs +++ b/src/tests/net.rs @@ -11,8 +11,8 @@ use crate::{ #[alkahest(Formula)] pub enum GameMessageFormula { - Client(Ref), - Server(Ref), + Client(ClientMessageFormula), + Server(ServerMessageFormula), } #[derive(Debug, Clone, PartialEq, Eq)]