Skip to content

Commit

Permalink
more minor improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Bastian Schubert committed Jul 10, 2024
1 parent e2696ff commit 3690dcd
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 53 deletions.
1 change: 1 addition & 0 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ uuid_bytes = ["dep:uuid"]

[dependencies]
bytes = { workspace = true }
smallvec = { workspace = true }
thiserror = { workspace = true }
tracing = { workspace = true }
uuid = { workspace = true, optional = true }
Expand Down
2 changes: 1 addition & 1 deletion core/src/wire/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ where
T: IntoWire,
{
let size = message.size_hint(tag);
tag.required_space() + size.required_space() + size
tag.required_space() as usize + size.required_space() as usize + size
}
29 changes: 17 additions & 12 deletions core/src/wire/scalars.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl IntoWire for f64 {
}

fn size_hint(&self, tag: u32) -> usize {
8 + tag.required_space()
8 + tag.required_space() as usize
}
}

Expand All @@ -52,7 +52,7 @@ impl IntoWire for f32 {
}

fn size_hint(&self, tag: u32) -> usize {
4 + tag.required_space()
4 + tag.required_space() as usize
}
}

Expand Down Expand Up @@ -81,7 +81,7 @@ impl IntoWire for u64 {
}

fn size_hint(&self, tag: u32) -> usize {
self.required_space() + tag.required_space()
self.required_space() as usize + tag.required_space() as usize
}
}

Expand Down Expand Up @@ -110,7 +110,7 @@ impl IntoWire for i64 {
}

fn size_hint(&self, tag: u32) -> usize {
self.required_space() + tag.required_space()
self.required_space() as usize + tag.required_space() as usize
}
}

Expand Down Expand Up @@ -139,7 +139,7 @@ impl IntoWire for u32 {
}

fn size_hint(&self, tag: u32) -> usize {
self.required_space() + tag.required_space()
self.required_space() as usize + tag.required_space() as usize
}
}

Expand Down Expand Up @@ -168,7 +168,7 @@ impl IntoWire for i32 {
}

fn size_hint(&self, tag: u32) -> usize {
self.required_space() + tag.required_space()
self.required_space() as usize + tag.required_space() as usize
}
}

Expand All @@ -193,7 +193,7 @@ impl IntoWire for String {

fn size_hint(&self, tag: u32) -> usize {
let len = self.len();
len.required_space() + tag.required_space() + len
len.required_space() as usize + tag.required_space() as usize + len
}
}

Expand Down Expand Up @@ -223,7 +223,7 @@ impl IntoWire for bool {
}

fn size_hint(&self, tag: u32) -> usize {
if *self { 1u32 } else { 0u32 }.required_space() + tag.required_space()
if *self { 1u32 } else { 0u32 }.required_space() as usize + tag.required_space() as usize
}
}

Expand All @@ -232,13 +232,18 @@ where
T: Message,
{
fn into_wire(self) -> WireType {
let mut buffer = bytes::BytesMut::with_capacity(self.size_hint());
self.serialize(&mut buffer);
WireType::LengthEncoded(buffer.freeze())
let size = self.size_hint();
let mut buffer = smallvec::SmallVec::<[u8; 1024]>::new();
buffer.resize(size, 0);
let mut buffer_ref = buffer.as_mut_slice();
self.serialize(&mut buffer_ref);
let written = size - buffer_ref.len();
WireType::LengthEncoded(bytes::Bytes::copy_from_slice(&buffer[0..written]))
}

fn size_hint(&self, tag: u32) -> usize {
tag.required_space() + self.size_hint()
// println!("size hint {} + {}", tag.required_space(), self.size_hint());
tag.required_space() as usize + self.size_hint()
}
}

Expand Down
8 changes: 5 additions & 3 deletions core/src/wire/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -587,16 +587,18 @@ mod test_messages {
}

fn size_hint(&self) -> usize {
let tag_size = 1.required_space() as usize;
let map_size: usize = self
.map
.iter()
.map(|(key, value)| {
let message_size = key.size_hint(1) + value.size_hint(2);
message_size + message_size.required_space() + 1.required_space()
message_size + message_size.required_space() as usize + tag_size
})
.sum();
.sum::<usize>()
+ tag_size;

let nested_size = crate::wire::nested::size_hint(2, &self.nested);
let nested_size = 2.required_space() as usize + IntoWire::size_hint(&self.nested, 2); // crate::wire::nested::size_hint(2, &self.nested);

map_size + nested_size
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/wire/uuid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ mod uuid_bytes {

fn size_hint(&self, tag: u32) -> usize {
let (high, low) = self.as_u64_pair();
let data_len = high.required_space() + low.required_space();
tag.required_space() + data_len.required_space() + data_len
let data_len = high.required_space() as usize + low.required_space() as usize;
tag.required_space() as usize + data_len.required_space() as usize + data_len
}
}

Expand Down
21 changes: 11 additions & 10 deletions core/src/wire/varint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const DROP_MSB: u8 = 0b0111_1111;

/// How many bytes an integer uses when being encoded as a VarInt.
#[inline]
fn required_encoded_space_unsigned(mut v: u64) -> usize {
fn required_encoded_space_unsigned(mut v: u64) -> u8 {
if v == 0 {
return 1;
}
Expand All @@ -26,7 +26,7 @@ fn required_encoded_space_unsigned(mut v: u64) -> usize {

/// How many bytes an integer uses when being encoded as a VarInt.
#[inline]
fn required_encoded_space_signed(v: i64) -> usize {
fn required_encoded_space_signed(v: i64) -> u8 {
required_encoded_space_unsigned(zigzag_encode(v))
}

Expand All @@ -37,7 +37,7 @@ fn required_encoded_space_signed(v: i64) -> usize {
pub trait VarInt: Sized + Copy {
/// Returns the number of bytes this number needs in its encoded form. Note: This varies
/// depending on the actual number you want to encode.
fn required_space(self) -> usize;
fn required_space(self) -> u8;
/// Decode a value from the slice. Returns the value and the number of bytes read from the
/// slice (can be used to read several consecutive values from a big slice)
/// return None if all bytes has MSB set.
Expand All @@ -49,7 +49,7 @@ pub trait VarInt: Sized + Copy {
/// Helper: Encode a value and return the encoded form as Vec. The Vec must be at least
/// `required_space()` bytes long.
fn encode_var_vec(self) -> Vec<u8> {
let mut v = vec![0; self.required_space()];
let mut v = vec![0; self.required_space() as usize];
self.encode_var(&mut v);
v
}
Expand All @@ -71,7 +71,7 @@ fn zigzag_decode(from: u64) -> i64 {
macro_rules! impl_varint {
($t:ty, unsigned) => {
impl VarInt for $t {
fn required_space(self) -> usize {
fn required_space(self) -> u8 {
required_encoded_space_unsigned(self as u64)
}

Expand All @@ -87,7 +87,7 @@ macro_rules! impl_varint {
};
($t:ty, signed) => {
impl VarInt for $t {
fn required_space(self) -> usize {
fn required_space(self) -> u8 {
required_encoded_space_signed(self as i64)
}

Expand Down Expand Up @@ -117,12 +117,14 @@ impl_varint!(i8, signed);
// first cast to these biggest types before being encoded.

impl VarInt for u64 {
fn required_space(self) -> usize {
fn required_space(self) -> u8 {
required_encoded_space_unsigned(self)
}

#[inline]
fn decode_var(src: &[u8]) -> Option<(Self, usize)> {
const SIXTY_THREE: usize = 9 * 7;

let mut result: u64 = 0;
let mut shift = 0;

Expand All @@ -132,7 +134,7 @@ impl VarInt for u64 {
result |= (msb_dropped as u64) << shift;
shift += 7;

if b & MSB == 0 || shift > (9 * 7) {
if b & MSB == 0 || shift > SIXTY_THREE {
success = b & MSB == 0;
break;
}
Expand Down Expand Up @@ -162,7 +164,7 @@ impl VarInt for u64 {
}

impl VarInt for i64 {
fn required_space(self) -> usize {
fn required_space(self) -> u8 {
required_encoded_space_signed(self)
}

Expand All @@ -177,7 +179,6 @@ impl VarInt for i64 {

#[inline]
fn encode_var(self, dst: &mut [u8]) -> u8 {
assert!(dst.len() >= self.required_space());
let mut n: u64 = zigzag_encode(self);
let mut i = 0u8;

Expand Down
26 changes: 13 additions & 13 deletions core/src/wire/wire_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,21 @@ impl<'a> WireTypeView<'a> {

pub fn size_hint(&self, tag: u32) -> usize {
match self {
WireTypeView::VarInt(data) => tag.required_space() + data.len(),
WireTypeView::FixedI64(_) => tag.required_space() + 8,
WireTypeView::SGroup => tag.required_space(),
WireTypeView::EGroup => tag.required_space(),
WireTypeView::VarInt(data) => tag.required_space() as usize + data.len(),
WireTypeView::FixedI64(_) => tag.required_space() as usize + 8,
WireTypeView::SGroup => tag.required_space() as usize,
WireTypeView::EGroup => tag.required_space() as usize,
WireTypeView::LengthEncoded(data) => {
let data_len = data.len();
tag.required_space() + data_len.required_space() + data_len
tag.required_space() as usize + data_len.required_space() as usize + data_len
}
WireTypeView::FixedI32(_) => tag.required_space() + 4,
WireTypeView::FixedI32(_) => tag.required_space() as usize + 4,
}
}
}

/// [WireType] is used for writing messages
#[derive(Debug, Clone, PartialEq)]
#[derive(Debug, Clone)]
pub enum WireType {
VarInt([u8; 10], u8),
FixedI64([u8; 8]),
Expand Down Expand Up @@ -137,15 +137,15 @@ impl WireType {

pub fn size_hint(&self, tag: u32) -> usize {
match self {
WireType::VarInt(_, size) => tag.required_space() + *size as usize,
WireType::FixedI64(_) => tag.required_space() + 8,
WireType::SGroup => tag.required_space(),
WireType::EGroup => tag.required_space(),
WireType::VarInt(_, size) => tag.required_space() as usize + *size as usize,
WireType::FixedI64(_) => tag.required_space() as usize + 8,
WireType::SGroup => tag.required_space() as usize,
WireType::EGroup => tag.required_space() as usize,
WireType::LengthEncoded(data) => {
let data_len = data.len();
tag.required_space() + data_len.required_space() + data_len
tag.required_space() as usize + data_len.required_space() as usize + data_len
}
WireType::FixedI32(_) => tag.required_space() + 4,
WireType::FixedI32(_) => tag.required_space() as usize + 4,
}
}

Expand Down
14 changes: 7 additions & 7 deletions derive/src/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ fn expand_message_message(
});

size_hint_impl.extend(quote_spanned! { span=>
let #field_size_ident = #tag.required_space() + #root::IntoWire::size_hint(&self.#field_ident, #tag);
let #field_size_ident = #tag.required_space() as usize + #root::IntoWire::size_hint(&self.#field_ident, #tag);
});
}
Kind::OneOf => {
Expand Down Expand Up @@ -158,13 +158,13 @@ fn expand_message_message(
});

size_hint_impl.extend(quote_spanned! { span=>
let tag_space = #tag.required_space();
let tag_space = #tag.required_space() as usize;
let #field_size_ident: usize = self
.#field_ident
.iter()
.map(|(key, value)| {
let message_size = key.size_hint(1) + value.size_hint(2);
message_size + message_size.required_space() + tag_space
message_size + message_size.required_space() as usize + tag_space
})
.sum();
});
Expand Down Expand Up @@ -301,12 +301,12 @@ fn expand_message_message(

size_hint_impl.extend(quote_spanned! { span=>
let #field_size_ident = if let Some(map) = self.#field_ident.as_ref() {
let tag_space = #tag.required_space();
let tag_space = #tag.required_space() as usize;
map
.iter()
.map(|(key, value)| {
let message_size = key.size_hint(1) + value.size_hint(2);
message_size + message_size.required_space() + tag_space
message_size + message_size.required_space() as usize + tag_space
})
.sum()
} else {
Expand Down Expand Up @@ -554,7 +554,7 @@ pub(crate) fn expand_enumeration(
size_hint_impl.extend(quote_spanned! {span=>
#ty::#var_ident => {
let tag: u32 = #tag;
tag.required_space()
tag.required_space() as usize
}
});

Expand All @@ -576,7 +576,7 @@ pub(crate) fn expand_enumeration(
fn size_hint(&self, tag: u32) -> usize {
use #root::export::VarInt;

tag.required_space()
tag.required_space() as usize
+ match self {
#size_hint_impl
}
Expand Down
7 changes: 2 additions & 5 deletions examples/shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,9 @@ version = "0.1.0"
edition = "2021"

[dependencies]
gin-tonic = { version = "0.4.8", features = ["uuid_bytes"] }
gin-tonic = { path = "../../gin", features = ["uuid_bytes"] }
tonic = { version = "0.12.0", features = ["transport"] }
uuid = { version = "1.10.0" }

[build-dependencies]
gin-tonic = { version = "0.4.8", features = ["uuid_bytes"] }

[patch.crates-io]
gin-tonic = { path = "../../gin" }
gin-tonic = { path = "../../gin", features = ["uuid_bytes"] }

0 comments on commit 3690dcd

Please sign in to comment.