Skip to content

Commit

Permalink
Minor refactors
Browse files Browse the repository at this point in the history
The slight changes to read_atom_header led to a very minor speed up on
serialization.
  • Loading branch information
ecton committed Jul 31, 2023
1 parent f7a6fdc commit e4ff0f1
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 22 deletions.
3 changes: 1 addition & 2 deletions benchmarks/benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,8 +211,7 @@ fn bench_logs(c: &mut Criterion) {
}

fn pot_serialize_into(logs: &LogArchive, buffer: &mut Vec<u8>) {
logs.serialize(&mut pot::ser::Serializer::new(buffer).unwrap())
.unwrap();
pot::to_writer(logs, buffer).unwrap();
}

fn cbor_serialize_into(logs: &LogArchive, buffer: &mut Vec<u8>) {
Expand Down
11 changes: 6 additions & 5 deletions pot/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,16 @@ pub fn read_atom_header<R: ReadBytesExt>(reader: &mut R) -> Result<(Kind, u64),
let first_byte = reader.read_u8()?;
let kind = Kind::from_u8(first_byte >> 5)?;
let mut arg = u64::from(first_byte & 0b1111);
if first_byte & 0b10000 > 0 {
let mut bytes_read = 1;
if first_byte & 0b10000 != 0 {
let mut bytes_remaining = 9;
let mut offset = 4;
loop {
let byte = reader.read_u8()?;
bytes_read += 1;
arg |= u64::from(byte & 0x7f) << offset;
let data = byte & 0x7f;
arg |= u64::from(data) << offset;
offset += 7;
if byte < 128 || bytes_read == 10 {
bytes_remaining -= 1;
if data == byte || bytes_remaining == 0 {
break;
}
}
Expand Down
7 changes: 3 additions & 4 deletions pot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,10 @@ impl Config {
}

/// Serializes a value to a `Vec` using the configured options.
#[allow(clippy::unused_self)]
#[inline]
pub fn serialize<T: Serialize>(&self, value: &T) -> Result<Vec<u8>> {
let mut output = Vec::default();
let mut serializer = ser::Serializer::new(&mut output)?;
value.serialize(&mut serializer)?;
let mut output = Vec::new();
self.serialize_into(value, &mut output)?;
Ok(output)
}

Expand Down
12 changes: 1 addition & 11 deletions pot/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -495,21 +495,11 @@ impl<'de, 'a: 'de, W: WriteBytesExt + 'a> ser::SerializeStructVariant
}

/// A list of previously serialized symbols.
#[derive(Debug)]
#[derive(Debug, Default)]
pub struct SymbolMap {
symbols: Vec<(usize, u32)>,
}

impl Default for SymbolMap {
#[inline]
fn default() -> Self {
let mut symbols = Vec::default();
// TODO make this configurable
symbols.reserve(1024);
Self { symbols }
}
}

struct RegisteredSymbol {
id: u32,
new: bool,
Expand Down

0 comments on commit e4ff0f1

Please sign in to comment.