use std::sync::Arc;
use crate::arena::Pod;
pub const INLINE_LEN: usize = 12;
#[repr(C)]
#[derive(Clone, Copy)]
pub struct StrView {
len: u32,
prefix: [u8; 4],
tail: [u8; 8],
}
unsafe impl Pod for StrView {}
const _: () = assert!(size_of::<StrView>() == 16);
#[inline(always)]
fn load_short(s: &[u8]) -> u64 {
let n = s.len();
debug_assert!(n <= 8, "one word holds eight bytes");
if n >= 4 {
let head = u32::from_le_bytes(s[..4].try_into().expect("four bytes")) as u64;
let tail = u32::from_le_bytes(s[n - 4..].try_into().expect("four bytes")) as u64;
let above = if n > 4 { tail >> ((8 - n) * 8) } else { 0 };
head | above << 32
} else if n > 0 {
u64::from(s[0]) | u64::from(s[n / 2]) << (n / 2 * 8) | u64::from(s[n - 1]) << ((n - 1) * 8)
} else {
0
}
}
impl StrView {
#[inline]
pub fn inline(bytes: &[u8]) -> Self {
let n = bytes.len();
debug_assert!(n <= INLINE_LEN);
let (lo, hi) = if n >= 8 {
let lo = u64::from_le_bytes(bytes[..8].try_into().expect("eight bytes"));
let tail = u32::from_le_bytes(bytes[n - 4..].try_into().expect("four bytes"));
(lo, if n > 8 { tail >> ((12 - n) * 8) } else { 0 })
} else {
(load_short(bytes), 0)
};
let w = lo.to_le_bytes();
let h = hi.to_le_bytes();
Self {
len: n as u32,
prefix: [w[0], w[1], w[2], w[3]],
tail: [w[4], w[5], w[6], w[7], h[0], h[1], h[2], h[3]],
}
}
pub fn long(bytes: &[u8], buffer: u16, offset: u32) -> Self {
debug_assert!(bytes.len() > INLINE_LEN);
let mut prefix = [0u8; 4];
prefix.copy_from_slice(&bytes[..4]);
let mut tail = [0u8; 8];
tail[..2].copy_from_slice(&buffer.to_le_bytes());
tail[2..6].copy_from_slice(&offset.to_le_bytes());
Self {
len: bytes.len() as u32,
prefix,
tail,
}
}
pub fn len(&self) -> usize {
self.len as usize
}
pub fn is_empty(&self) -> bool {
self.len == 0
}
pub fn is_inline(&self) -> bool {
self.len as usize <= INLINE_LEN
}
fn buffer_id(&self) -> u16 {
u16::from_le_bytes([self.tail[0], self.tail[1]])
}
fn offset(&self) -> u32 {
u32::from_le_bytes([self.tail[2], self.tail[3], self.tail[4], self.tail[5]])
}
pub fn inline_bytes(&self) -> &[u8] {
debug_assert!(self.is_inline());
let base = std::ptr::from_ref(self).cast::<u8>();
unsafe { std::slice::from_raw_parts(base.add(4), self.len as usize) }
}
pub fn bytes<'a>(&'a self, bufs: &'a StrBuffers) -> &'a [u8] {
if self.is_inline() {
self.inline_bytes()
} else {
bufs.slice(self.buffer_id(), self.offset(), self.len as usize)
}
}
pub fn sub(&self, bytes: &[u8], start: usize, len: usize) -> Self {
debug_assert!(start + len <= self.len());
let part = &bytes[start..start + len];
if len <= INLINE_LEN {
Self::inline(part)
} else {
Self::long(part, self.buffer_id(), self.offset() + start as u32)
}
}
pub fn eq_with(&self, a_bufs: &StrBuffers, other: &StrView, b_bufs: &StrBuffers) -> bool {
if self.len != other.len || self.prefix != other.prefix {
return false;
}
if self.is_inline() {
return self.tail == other.tail;
}
self.bytes(a_bufs) == other.bytes(b_bufs)
}
}
struct Buf {
ptr: *const u8,
len: usize,
_owner: Owner,
}
enum Owner {
Held(#[allow(dead_code)] Arc<[u8]>),
Lent(#[allow(dead_code)] Arc<dyn std::any::Any + Send + Sync>),
}
unsafe impl Send for Buf {}
unsafe impl Sync for Buf {}
#[derive(Default)]
pub struct StrBuffers {
bufs: Vec<Buf>,
}
pub static NO_BUFFERS: StrBuffers = StrBuffers::empty();
impl StrBuffers {
pub fn new() -> Self {
Self::default()
}
pub const fn empty() -> Self {
Self { bufs: Vec::new() }
}
pub fn push(&mut self, buf: Arc<[u8]>) -> u16 {
let (ptr, len) = (buf.as_ptr(), buf.len());
self.add(Buf {
ptr,
len,
_owner: Owner::Held(buf),
})
}
pub unsafe fn push_lent(
&mut self,
ptr: *const u8,
len: usize,
owner: Arc<dyn std::any::Any + Send + Sync>,
) -> u16 {
self.add(Buf {
ptr,
len,
_owner: Owner::Lent(owner),
})
}
fn add(&mut self, buf: Buf) -> u16 {
let id = u16::try_from(self.bufs.len()).expect("string buffer count fits u16");
self.bufs.push(buf);
id
}
pub fn len(&self) -> usize {
self.bufs.len()
}
pub fn is_empty(&self) -> bool {
self.bufs.is_empty()
}
pub fn slice(&self, id: u16, offset: u32, len: usize) -> &[u8] {
let buf = &self.bufs[id as usize];
let start = offset as usize;
assert!(
start + len <= buf.len,
"string view {start}..{} runs past its {}-byte buffer",
start + len,
buf.len
);
unsafe { std::slice::from_raw_parts(buf.ptr.add(start), len) }
}
}
#[derive(Default)]
pub struct StrBuilder {
long: Vec<u8>,
}
impl StrBuilder {
pub fn new() -> Self {
Self::default()
}
pub fn with_capacity(bytes: usize) -> Self {
Self {
long: Vec::with_capacity(bytes),
}
}
pub fn push_with(&mut self, len: usize, fill: impl FnOnce(&mut [u8])) -> StrView {
if len <= INLINE_LEN {
let mut inline = [0u8; INLINE_LEN];
fill(&mut inline[..len]);
StrView::inline(&inline[..len])
} else {
let start = self.long.len();
self.long.resize(start + len, 0);
fill(&mut self.long[start..]);
StrView::long(&self.long[start..], 0, start as u32)
}
}
pub fn push(&mut self, bytes: &[u8]) -> StrView {
self.push_with(bytes.len(), |dst| dst.copy_from_slice(bytes))
}
pub fn finish(self) -> StrBuffers {
let mut bufs = StrBuffers::new();
if !self.long.is_empty() {
bufs.push(Arc::from(self.long.into_boxed_slice()));
}
bufs
}
}
pub struct Dictionary {
bytes: Vec<u8>,
ends: Vec<u32>,
}
impl Dictionary {
pub fn from_sorted<I, B>(entries: I) -> Self
where
I: IntoIterator<Item = B>,
B: AsRef<[u8]>,
{
let mut bytes = Vec::new();
let mut ends = Vec::new();
for e in entries {
let e = e.as_ref();
debug_assert!(
ends.is_empty() || {
let start = if ends.len() == 1 {
0
} else {
ends[ends.len() - 2] as usize
};
let prev = &bytes[start..*ends.last().unwrap() as usize];
prev < e
},
"dictionary entries must be sorted and unique"
);
bytes.extend_from_slice(e);
ends.push(bytes.len() as u32);
}
Self { bytes, ends }
}
pub fn len(&self) -> usize {
self.ends.len()
}
pub fn is_empty(&self) -> bool {
self.ends.is_empty()
}
pub fn get(&self, code: u32) -> &[u8] {
let i = code as usize;
let start = if i == 0 { 0 } else { self.ends[i - 1] as usize };
&self.bytes[start..self.ends[i] as usize]
}
pub fn code_of(&self, needle: &[u8]) -> Result<u32, u32> {
let mut lo = 0usize;
let mut hi = self.len();
while lo < hi {
let mid = (lo + hi) / 2;
match self.get(mid as u32).cmp(needle) {
std::cmp::Ordering::Less => lo = mid + 1,
std::cmp::Ordering::Greater => hi = mid,
std::cmp::Ordering::Equal => return Ok(mid as u32),
}
}
Err(lo as u32)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn inline_roundtrip() {
let bufs = StrBuffers::new();
for s in ["", "a", "abcd", "abcdefgh", "twelve chars"] {
let v = StrView::inline(s.as_bytes());
assert_eq!(v.bytes(&bufs), s.as_bytes(), "{s:?}");
}
}
#[test]
fn long_roundtrip_and_eq() {
let mut bufs = StrBuffers::new();
let payload: Arc<[u8]> =
Arc::from(&b"hello, this is a longer string sitting in a buffer"[..]);
let id = bufs.push(Arc::clone(&payload));
let a = StrView::long(&payload, id, 0);
assert_eq!(a.bytes(&bufs), &payload[..]);
let b = StrView::long(&payload, id, 0);
assert!(a.eq_with(&bufs, &b, &bufs));
let short = StrView::inline(b"hello");
assert!(!a.eq_with(&bufs, &short, &bufs));
}
#[test]
fn a_part_of_a_string_points_back_at_it() {
let mut bufs = StrBuffers::new();
let payload: Arc<[u8]> = Arc::from(&b" a string long enough to need a buffer "[..]);
let id = bufs.push(Arc::clone(&payload));
let whole = StrView::long(&payload, id, 0);
let bytes = whole.bytes(&bufs);
let inner = whole.sub(bytes, 2, payload.len() - 4);
assert!(!inner.is_inline());
assert_eq!(inner.bytes(&bufs), b"a string long enough to need a buffer");
let head = whole.sub(bytes, 2, 8);
assert!(head.is_inline(), "a short part carries its own bytes");
assert_eq!(head.bytes(&StrBuffers::new()), b"a string");
}
#[test]
fn prefix_rejects_before_buffer_touch() {
let mut bufs = StrBuffers::new();
let p1: Arc<[u8]> = Arc::from(&b"aaaaaaaaaaaaaaaaaaaa"[..]);
let p2: Arc<[u8]> = Arc::from(&b"bbbbbbbbbbbbbbbbbbbb"[..]);
let i1 = bufs.push(Arc::clone(&p1));
let i2 = bufs.push(Arc::clone(&p2));
let a = StrView::long(&p1, i1, 0);
let b = StrView::long(&p2, i2, 0);
assert!(!a.eq_with(&bufs, &b, &bufs));
}
#[test]
fn lent_bytes_read_where_they_lie() {
let outside: Arc<Vec<u8>> = Arc::new(b"a string long enough to need a buffer".to_vec());
let (ptr, len) = (outside.as_ptr(), outside.len());
let mut bufs = StrBuffers::new();
let id = unsafe { bufs.push_lent(ptr, len, Arc::clone(&outside) as Arc<_>) };
let view = StrView::long(&outside, id, 0);
assert_eq!(view.bytes(&bufs), &outside[..]);
assert_eq!(Arc::strong_count(&outside), 2);
drop(outside);
assert_eq!(view.bytes(&bufs), b"a string long enough to need a buffer");
}
#[test]
#[should_panic(expected = "runs past its")]
fn a_view_past_its_buffer_is_caught() {
let mut bufs = StrBuffers::new();
let id = bufs.push(Arc::from(&b"twenty bytes exactly"[..]));
bufs.slice(id, 16, 8);
}
#[test]
fn every_inline_length_round_trips() {
let all = b"abcdefghijkl";
for n in 0..=INLINE_LEN {
let want = &all[..n];
let view = StrView::inline(want);
assert_eq!(view.len(), n, "length of {n}");
assert_eq!(view.inline_bytes(), want, "bytes of {n}");
assert!(view.is_inline(), "{n} bytes should be inline");
assert!(view.eq_with(&NO_BUFFERS, &StrView::inline(want), &NO_BUFFERS));
}
}
#[test]
fn inline_views_of_different_lengths_stay_apart() {
let a = StrView::inline(b"abcdefgh");
let b = StrView::inline(b"abcdefghi");
assert!(!a.eq_with(&NO_BUFFERS, &b, &NO_BUFFERS));
assert_eq!(a.tail[4..], [0, 0, 0, 0]);
}
#[test]
fn dictionary_codes() {
let d = Dictionary::from_sorted(["apple", "banana", "cherry"]);
assert_eq!(d.code_of(b"banana"), Ok(1));
assert_eq!(d.code_of(b"blueberry"), Err(2));
assert_eq!(d.get(2), b"cherry");
}
}