Skip to content

Commit

Permalink
Merge pull request #5 from nomad/smaller-serialized-insertions
Browse files Browse the repository at this point in the history
Make serialized `Insertion`s smaller
  • Loading branch information
noib3 authored Jan 25, 2024
2 parents 3c046fd + 07802a9 commit 0590284
Show file tree
Hide file tree
Showing 10 changed files with 954 additions and 37 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## [Unreleased]

### Changed

- the `Serialize` impl of `Insertion` now produces 3-4x smaller payloads,
depending on the data format used ([#5](https://github.com/nomad/cola/pull/5));

## [0.3.0] - Jan 9 2024

### Added
Expand Down
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ serde = { version = "1.0", features = ["derive"], optional = true }
sha2 = { version = "0.10", optional = true }

[dev-dependencies]
bincode = "1.3"
criterion = "0.5"
rand = "0.8"
rand_chacha = "0.3"
serde_json = "1"
traces = { path = "./traces" }
zstd = "0.13"

[[bench]]
name = "traces"
Expand Down
68 changes: 52 additions & 16 deletions src/anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,25 @@ impl Anchor {
}
}

/// A bias to use when creating an [`Anchor`].
///
/// This is used in the
/// [`Replica::create_anchor()`][crate::Replica::create_anchor] method to
/// create a new [`Anchor`]. See the documentation of that method for more
/// information.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(
any(feature = "encode", feature = "serde"),
derive(serde::Serialize, serde::Deserialize)
)]
pub enum AnchorBias {
/// The anchor should attach to the left.
Left,

/// The anchor should attach to the right.
Right,
}

/// TODO: docs
#[derive(Copy, Clone, PartialEq, Eq)]
#[cfg_attr(
Expand All @@ -83,6 +102,12 @@ impl core::fmt::Debug for InnerAnchor {
fn fmt(&self, f: &mut core::fmt::Formatter) -> core::fmt::Result {
if self == &Self::zero() {
write!(f, "zero")
} else if f.alternate() {
write!(
f,
"{:x}.{} in {}",
self.replica_id, self.offset, self.contained_in
)
} else {
write!(f, "{:x}.{}", self.replica_id, self.offset)
}
Expand Down Expand Up @@ -126,21 +151,32 @@ impl InnerAnchor {
}
}

/// A bias to use when creating an [`Anchor`].
///
/// This is used in the
/// [`Replica::create_anchor()`][crate::Replica::create_anchor] method to
/// create a new [`Anchor`]. See the documentation of that method for more
/// information.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[cfg_attr(
any(feature = "encode", feature = "serde"),
derive(serde::Serialize, serde::Deserialize)
)]
pub enum AnchorBias {
/// The anchor should attach to the left.
Left,
#[cfg(feature = "encode")]
mod encode {
use super::*;
use crate::encode::{Decode, Encode, Int, IntDecodeError};

impl Encode for InnerAnchor {
#[inline]
fn encode(&self, buf: &mut Vec<u8>) {
Int::new(self.replica_id()).encode(buf);
Int::new(self.run_ts()).encode(buf);
Int::new(self.offset()).encode(buf);
}
}

/// The anchor should attach to the right.
Right,
impl Decode for InnerAnchor {
type Value = Self;

type Error = IntDecodeError;

#[inline]
fn decode(buf: &[u8]) -> Result<(Self, &[u8]), Self::Error> {
let (replica_id, buf) = Int::<ReplicaId>::decode(buf)?;
let (run_ts, buf) = Int::<RunTs>::decode(buf)?;
let (offset, buf) = Int::<Length>::decode(buf)?;
let anchor = Self::new(replica_id, offset, run_ts);
Ok((anchor, buf))
}
}
}
Loading

0 comments on commit 0590284

Please sign in to comment.