Skip to content

Commit

Permalink
quicklog(serialize): add decode to Serialize
Browse files Browse the repository at this point in the history
  • Loading branch information
thog92 committed Mar 7, 2024
1 parent 2e0bbdf commit ce0da33
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 54 deletions.
25 changes: 12 additions & 13 deletions quicklog/benches/logger_benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::fmt::Display;
use std::str::from_utf8;
use std::sync::mpsc::{channel, Receiver, Sender};
use std::time::Duration;

Expand Down Expand Up @@ -27,17 +26,6 @@ struct Nested {

impl Serialize for BigStruct {
fn encode<'buf>(&self, write_buf: &'buf mut [u8]) -> Store<'buf> {
fn decode(buf: &[u8]) -> String {
let (mut _head, mut tail) = buf.split_at(0);
let mut vec = vec![];
for _ in 0..100 {
(_head, tail) = tail.split_at(4);
vec.push(i32::from_le_bytes(_head.try_into().unwrap()));
}
let s = from_utf8(tail).unwrap();
format!("vec: {:?}, str: {}", vec, s)
}

let (mut _head, mut tail) = write_buf.split_at_mut(0);
for i in 0..100 {
(_head, tail) = tail.split_at_mut(4);
Expand All @@ -46,7 +34,18 @@ impl Serialize for BigStruct {

tail.copy_from_slice(self.some.as_bytes());

Store::new(decode, write_buf)
Store::new(Self::decode, write_buf)
}

fn decode(buf: &[u8]) -> (String, &[u8]) {
let (mut _head, mut tail) = buf.split_at(0);
let mut vec = vec![];
for _ in 0..100 {
(_head, tail) = tail.split_at(4);
vec.push(i32::from_le_bytes(_head.try_into().unwrap()));
}
let (s, rest) = <&str as Serialize>::decode(tail);
(format!("vec: {:?}, str: {}", vec, s), rest)
}

fn buffer_size_required(&self) -> usize {
Expand Down
2 changes: 0 additions & 2 deletions quicklog/benches/quicklog_benchmark.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::str::from_utf8;

use criterion::{black_box, criterion_group, criterion_main, Bencher, Criterion};
use delog::render::DefaultRenderer;
use quanta::Instant;
Expand Down
6 changes: 5 additions & 1 deletion quicklog/examples/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,12 @@ impl Serialize for S {
self.i.encode(write_buf)
}

fn decode(read_buf: &[u8]) -> (String, &[u8]) {
i32::decode(read_buf)
}

fn buffer_size_required(&self) -> usize {
std::mem::size_of_val(&self.i)
self.i.buffer_size_required()
}
}

Expand Down
3 changes: 2 additions & 1 deletion quicklog/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@
//! }
//!
//! impl Serialize for SomeStruct {
//! fn encode(&self, write_buf: &'static mut [u8]) -> Store { /* some impl */ }
//! fn encode<'buf>(&self, write_buf: &'buf mut [u8]) -> Store<'buf> { /* some impl */ }
//! fn decode(read_buf: &[u8]) -> (String, &[u8]) { /* some impl */ }
//! fn buffer_size_required(&self) -> usize { /* some impl */ }
//! }
//!
Expand Down
42 changes: 22 additions & 20 deletions quicklog/src/serialize/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ pub mod buffer;
/// not require allocation and could speed things up.
pub trait Serialize {
fn encode<'buf>(&self, write_buf: &'buf mut [u8]) -> Store<'buf>;
fn decode(read_buf: &[u8]) -> (String, &[u8]);
fn buffer_size_required(&self) -> usize;
}

/// Function pointer which decodes a byte buffer back into `String` representation
pub type DecodeFn = fn(&[u8]) -> String;
pub type DecodeFn = fn(&[u8]) -> (String, &[u8]);

/// Contains the decode function required to decode `buffer` back into a `String`
/// representation.
Expand All @@ -27,7 +28,8 @@ impl Store<'_> {
}

pub fn as_string(&self) -> String {
(self.decode_fn)(self.buffer)
let (s, _) = (self.decode_fn)(self.buffer);
s
}
}

Expand All @@ -42,15 +44,18 @@ macro_rules! gen_serialize {
impl Serialize for $primitive {
fn encode<'buf>(&self, write_buf: &'buf mut [u8]) -> Store<'buf> {
assert!(std::mem::size_of::<$primitive>() == write_buf.len());
fn decode(read_buf: &[u8]) -> String {
let x = <$primitive>::from_le_bytes(read_buf.try_into().unwrap());
format!("{}", x)
}

let size = std::mem::size_of::<$primitive>();
let (x, _) = write_buf.split_at_mut(size);
x.copy_from_slice(&self.to_le_bytes());
Store::new(decode, &*x)
Store::new(Self::decode, x)
}

fn decode(read_buf: &[u8]) -> (String, &[u8]) {
let (chunk, rest) = read_buf.split_at(std::mem::size_of::<$primitive>());
let x = <$primitive>::from_le_bytes(chunk.try_into().unwrap());

(format!("{}", x), rest)
}

fn buffer_size_required(&self) -> usize {
Expand All @@ -72,12 +77,13 @@ gen_serialize!(usize);
impl Serialize for &str {
fn encode<'buf>(&self, write_buf: &'buf mut [u8]) -> Store<'buf> {
assert!(self.len() == write_buf.len());
fn decode(read_buf: &[u8]) -> String {
let x = from_utf8(read_buf).unwrap();
x.to_string()
}
write_buf.copy_from_slice(self.as_bytes());
Store::new(decode, write_buf)
Store::new(Self::decode, write_buf)
}

fn decode(read_buf: &[u8]) -> (String, &[u8]) {
let x = from_utf8(read_buf).unwrap();
(x.to_string(), &[])
}

fn buffer_size_required(&self) -> usize {
Expand All @@ -92,9 +98,9 @@ pub fn encode_debug<T: std::fmt::Debug>(val: T, write_buf: &mut [u8]) -> Store {
// `buffer_size_required`
assert!(val_string.len() <= write_buf.len());

fn decode(read_buf: &[u8]) -> String {
fn decode(read_buf: &[u8]) -> (String, &[u8]) {
let x = from_utf8(read_buf).unwrap();
x.to_string()
(x.to_string(), &[])
}

let (chunk, _) = write_buf.split_at_mut(val_string.len());
Expand All @@ -115,7 +121,7 @@ mod tests {

let x: $primitive = $val;
let x_store = x.encode(&mut buf);
assert_eq!(format!("{}", x), (x_store.decode_fn)(&buf));
assert_eq!(format!("{}", x), format!("{}", x_store));
}};
}

Expand Down Expand Up @@ -146,13 +152,9 @@ mod tests {
let b_store = b.encode(b_chunk);
let c_store = c.encode(c_chunk);

let a_str = (a_store.decode_fn)(a_store.buffer);
let b_str = (b_store.decode_fn)(b_store.buffer);
let c_str = (c_store.decode_fn)(c_store.buffer);

assert_eq!(
format!("{} {} {}", a, b, c),
format!("{} {} {}", a_str, b_str, c_str)
format!("{} {} {}", a_store, b_store, c_store)
)
}

Expand Down
35 changes: 18 additions & 17 deletions quicklog/tests/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,13 @@ pub(crate) struct SerializeStruct {

impl Serialize for SerializeStruct {
fn encode<'buf>(&self, write_buf: &'buf mut [u8]) -> Store<'buf> {
fn decode(read_buf: &[u8]) -> String {
let x = std::str::from_utf8(read_buf).unwrap();
x.to_string()
}
write_buf.copy_from_slice(self.symbol.as_bytes());
Store::new(decode, write_buf)
Store::new(Self::decode, write_buf)
}

fn decode(read_buf: &[u8]) -> (String, &[u8]) {
let x = std::str::from_utf8(read_buf).unwrap();
(x.to_string(), &[])
}

fn buffer_size_required(&self) -> usize {
Expand All @@ -127,17 +128,6 @@ pub(crate) struct BigStruct {

impl Serialize for BigStruct {
fn encode<'buf>(&self, write_buf: &'buf mut [u8]) -> Store<'buf> {
fn decode(buf: &[u8]) -> String {
let (mut _head, mut tail) = buf.split_at(0);
let mut vec = vec![];
for _ in 0..100 {
(_head, tail) = tail.split_at(4);
vec.push(i32::from_le_bytes(_head.try_into().unwrap()));
}
let s = std::str::from_utf8(tail).unwrap();
format!("vec: {:?}, str: {}", vec, s)
}

let (mut _head, mut tail) = write_buf.split_at_mut(0);
for i in 0..100 {
(_head, tail) = tail.split_at_mut(4);
Expand All @@ -146,7 +136,18 @@ impl Serialize for BigStruct {

tail.copy_from_slice(self.some.as_bytes());

Store::new(decode, write_buf)
Store::new(Self::decode, write_buf)
}

fn decode(buf: &[u8]) -> (String, &[u8]) {
let (mut _head, mut tail) = buf.split_at(0);
let mut vec = vec![];
for _ in 0..100 {
(_head, tail) = tail.split_at(4);
vec.push(i32::from_le_bytes(_head.try_into().unwrap()));
}
let s = std::str::from_utf8(tail).unwrap();
(format!("vec: {:?}, str: {}", vec, s), &[])
}

fn buffer_size_required(&self) -> usize {
Expand Down
3 changes: 3 additions & 0 deletions rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[toolchain]
channel = "1.72"
profile = "default"

0 comments on commit ce0da33

Please sign in to comment.