Skip to content

experiment with the SymBuf representation #392

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions zlib-rs/src/deflate.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![warn(unsafe_op_in_unsafe_fn)]
use core::{ffi::CStr, marker::PhantomData, mem::MaybeUninit, ops::ControlFlow};

use sym_buf::Symbol;

use crate::{
adler32::adler32,
allocate::Allocator,
Expand Down Expand Up @@ -1168,10 +1170,10 @@ impl<'a> BitWriter<'a> {
}

fn compress_block_help(&mut self, sym_buf: &SymBuf, ltree: &[Value], dtree: &[Value]) {
for (dist, lc) in sym_buf.iter() {
for Symbol { dist, len, .. } in sym_buf.iter() {
match dist {
0 => self.emit_lit(ltree, lc) as usize,
_ => self.emit_dist(ltree, dtree, lc, dist),
0 => self.emit_lit(ltree, len) as usize,
_ => self.emit_dist(ltree, dtree, len, dist),
};
}

Expand Down Expand Up @@ -1540,10 +1542,10 @@ impl<'a> State<'a> {

fn compress_block_static_trees(&mut self) {
let ltree = self::trees_tbl::STATIC_LTREE.as_slice();
for (dist, lc) in self.sym_buf.iter() {
for Symbol { dist, len, .. } in self.sym_buf.iter() {
match dist {
0 => self.bit_writer.emit_lit(ltree, lc) as usize,
_ => self.bit_writer.emit_dist_static(lc, dist),
0 => self.bit_writer.emit_lit(ltree, len) as usize,
_ => self.bit_writer.emit_dist_static(len, dist),
};
}

Expand Down
2 changes: 1 addition & 1 deletion zlib-rs/src/deflate/pending.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl<'a> Pending<'a> {
}
}

#[inline(always)]
#[inline]
#[track_caller]
pub fn extend(&mut self, buf: &[u8]) {
assert!(
Expand Down
55 changes: 30 additions & 25 deletions zlib-rs/src/deflate/sym_buf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,28 @@
use crate::allocate::Allocator;
use crate::weak_slice::WeakSliceMut;

#[derive(Clone, Copy, Default)]
#[repr(C)]
pub(crate) struct Symbol {
pub len: u8,
_padding: u8,
pub dist: u16,
}

pub(crate) struct SymBuf<'a> {
buf: WeakSliceMut<'a, u8>,
buf: WeakSliceMut<'a, Symbol>,
filled: usize,
}

impl<'a> SymBuf<'a> {
#[inline]
pub fn iter(&self) -> impl Iterator<Item = (u16, u8)> + '_ {
self.buf.as_slice()[..self.filled]
.chunks_exact(3)
.map(|chunk| match *chunk {
[dist_low, dist_high, lc] => (u16::from_le_bytes([dist_low, dist_high]), lc),
_ => unreachable!("chunks are exactly 3 elements wide"),
})
pub fn iter(&self) -> impl Iterator<Item = Symbol> + '_ {
self.buf.as_slice()[..self.filled].iter().copied()
}

#[inline]
pub fn should_flush_block(&self) -> bool {
self.filled == self.buf.len() - 3
self.filled == self.buf.len() - 1
}

/// Returns true if there are no bytes in this ReadBuf
Expand All @@ -36,41 +39,43 @@ impl<'a> SymBuf<'a> {
/// The number of initialized bytes is not changed, and the contents of the buffer are not modified.
#[inline]
pub fn clear(&mut self) {
self.buf.as_mut_slice().fill(0);
self.buf.as_mut_slice().fill(Symbol::default());
self.filled = 0;
}

#[inline(always)]
pub fn push_lit(&mut self, byte: u8) {
// NOTE: we rely on the buffer being zeroed here!
self.buf.as_mut_slice()[self.filled + 2] = byte;

self.filled += 3;
pub fn push_lit(&mut self, len: u8) {
// NOTE: we rely on the value being zeroed here!
self.buf.as_mut_slice()[self.filled] = Symbol {
len,
..Symbol::default()
};

self.filled += 1;
}

#[inline(always)]
pub fn push_dist(&mut self, dist: u16, len: u8) {
let buf = &mut self.buf.as_mut_slice()[self.filled..][..3];
let [dist1, dist2] = dist.to_le_bytes();

buf[0] = dist1;
buf[1] = dist2;
buf[2] = len;
self.buf.as_mut_slice()[self.filled] = Symbol {
len,
dist,
..Symbol::default()
};

self.filled += 3;
self.filled += 1;
}

pub(crate) fn new_in(alloc: &Allocator<'a>, len: usize) -> Option<Self> {
let ptr = alloc.allocate_zeroed_buffer(len * 3)?;
let ptr = alloc.allocate_zeroed_buffer(len * 4)?;

// safety: all elements are now initialized
let buf = unsafe { WeakSliceMut::from_raw_parts_mut(ptr.as_ptr(), len * 3) };
let buf = unsafe { WeakSliceMut::from_raw_parts_mut(ptr.as_ptr().cast(), len) };

Some(Self { buf, filled: 0 })
}

pub(crate) fn clone_in(&self, alloc: &Allocator<'a>) -> Option<Self> {
let mut clone = Self::new_in(alloc, self.buf.len() / 3)?;
let mut clone = Self::new_in(alloc, self.buf.len())?;

clone
.buf
Expand Down