Skip to content

Commit

Permalink
Add LZ4 compression support
Browse files Browse the repository at this point in the history
  • Loading branch information
romac committed Nov 25, 2024
1 parent 2b0324d commit a9fd826
Show file tree
Hide file tree
Showing 10 changed files with 306 additions and 56 deletions.
21 changes: 21 additions & 0 deletions code/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions code/crates/wal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@
name = "malachite-wal"
version = "0.1.0"
edition = "2021"
rust-version = "1.82.0"

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[[bench]]
name = "wal"
harness = false

[features]
compression = ["dep:lz4_flex"]
force-compression = ["compression"]

[dependencies]
cfg-if = "1"
advisory-lock = "0.3.0"
bytes = "1.5.0"
crc32fast = "1.4.0"
lz4_flex = { version = "0.11.0", optional = true }

[dev-dependencies]
criterion = "0.5.1"
Expand Down
14 changes: 11 additions & 3 deletions code/crates/wal/benches/wal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ struct BenchConfig {
sync_interval: usize,
}

impl BenchConfig {
fn total_size(&self) -> usize {
self.entry_size * self.batch_size
}
}

fn wal_benchmarks(c: &mut Criterion) {
let dir = tempdir().unwrap();

Expand All @@ -35,7 +41,7 @@ fn wal_benchmarks(c: &mut Criterion) {
sync_interval: 100,
};

read_group.throughput(Throughput::Bytes(*size as u64));
read_group.throughput(Throughput::Bytes(config.total_size() as u64));
read_group.bench_with_input(BenchmarkId::new("sequential_read", size), size, |b, _| {
let path = get_temp_wal_path(&dir);
setup_wal_for_reading(&path, &config);
Expand All @@ -56,7 +62,7 @@ fn wal_benchmarks(c: &mut Criterion) {
sync_interval: 100,
};

write_group.throughput(Throughput::Bytes(*size as u64));
write_group.throughput(Throughput::Bytes(config.total_size() as u64));
write_group.bench_with_input(BenchmarkId::new("sequential_write", size), size, |b, _| {
let path = get_temp_wal_path(&dir);
b.iter(|| bench_sequential_write(&path, &config));
Expand All @@ -73,7 +79,7 @@ fn wal_benchmarks(c: &mut Criterion) {
sync_interval: *batch_size,
};

write_group.throughput(Throughput::Elements(*batch_size as u64));
write_group.throughput(Throughput::Bytes(config.total_size() as u64));
write_group.bench_with_input(
BenchmarkId::new("batch_write", batch_size),
batch_size,
Expand All @@ -94,6 +100,7 @@ fn wal_benchmarks(c: &mut Criterion) {
sync_interval: *interval,
};

write_group.throughput(Throughput::Bytes(config.total_size() as u64));
write_group.bench_with_input(
BenchmarkId::new("sync_interval", interval),
interval,
Expand Down Expand Up @@ -182,6 +189,7 @@ fn bench_small_writes_frequent_sync(c: &mut Criterion) {
let mut group = c.benchmark_group("small_writes_frequent_sync");
let dir = tempdir().unwrap();

group.throughput(Throughput::Bytes(64 * 100));
group.bench_function("write_sync_every", |b| {
b.iter(|| {
let path = get_temp_wal_path(&dir);
Expand Down
22 changes: 17 additions & 5 deletions code/crates/wal/src/ext.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
use std::io::{self, Read, Write};

#[inline(always)]
pub fn read_u8<R: Read>(reader: &mut R) -> io::Result<u8> {
let mut buf = [0; 1];
reader.read_exact(&mut buf)?;
Ok(buf[0])
}

#[inline(always)]
pub fn write_u8<W: Write>(writer: &mut W, value: u8) -> io::Result<()> {
writer.write_all(&[value])
}

#[inline(always)]
pub fn read_u32<R: Read>(reader: &mut R) -> io::Result<u32> {
let mut buf = [0; 4];
reader.read_exact(&mut buf)?;
Ok(u32::from_be_bytes(buf))
}

#[inline(always)]
pub fn write_u32<W: Write>(writer: &mut W, value: u32) -> io::Result<()> {
writer.write_all(&value.to_be_bytes())
}

#[inline(always)]
pub fn read_u64<R: Read>(reader: &mut R) -> io::Result<u64> {
let mut buf = [0; 8];
reader.read_exact(&mut buf)?;
Ok(u64::from_be_bytes(buf))
}

#[inline(always)]
pub fn write_u32<W: Write>(writer: &mut W, value: u32) -> io::Result<()> {
writer.write_all(&value.to_be_bytes())
}

#[inline(always)]
pub fn write_u64<W: Write>(writer: &mut W, value: u64) -> io::Result<()> {
writer.write_all(&value.to_be_bytes())
Expand Down
2 changes: 2 additions & 0 deletions code/crates/wal/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#![cfg_attr(docsrs, feature(doc_cfg))]

//! Write-Ahead Log (WAL) implementation
mod ext;
Expand Down
Loading

0 comments on commit a9fd826

Please sign in to comment.