dsi-bitstream 0.10.1

A Rust implementation of read/write bit streams supporting several types of instantaneous codes
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
/*
 * SPDX-FileCopyrightText: 2025 Tommaso Fontana
 * SPDX-FileCopyrightText: 2025 Sebastiano Vigna
 *
 * SPDX-License-Identifier: Apache-2.0 OR MIT
 */

//! A benchmark testing different variations of VByte codes, both as byte
//! streams and bit streams. We test (almost) all combinations of the following
//! alternatives:
//!
//! - continuation bits at the start vs. one bit on each byte; if continuation
//!   bits are all at the start, test if chains vs. `leading_ones`;
//!
//! - bits vs. byte streams;
//!
//! - little endian vs. big endian;
//!
//! - 1 or 0 as continuation bit;
//!
//! - the code is complete or not.
use criterion::{Criterion, criterion_group, criterion_main};
use dsi_bitstream::prelude::*;
use rand::{RngExt, SeedableRng, rngs::SmallRng};
use std::hint::black_box;
use std::io::{Read, Write};
use std::marker::PhantomData;
use std::time::Duration;

type Result<T> = std::result::Result<T, Box<dyn core::error::Error>>;

pub const GAMMA_DATA: usize = 1_000_000;
pub const CAPACITY: usize = 4 * GAMMA_DATA;

/// Computes a vector of `n` values sampled from a Zipf distribution with parameter `8/7`,
/// which is the implied distribution.
pub fn gen_vbyte_data(n: usize) -> Vec<u64> {
    let mut rng = SmallRng::seed_from_u64(0);
    let distr = rand_distr::Zipf::new(usize::MAX as f64, 8.0 / 7.0).unwrap();

    (0..n)
        .map(|_| rng.sample(distr) as u64 - 1)
        .collect::<Vec<_>>()
}

pub trait Format {
    const NAME: &'static str;
}
pub struct NonGrouped;
impl Format for NonGrouped {
    const NAME: &'static str = "non_grouped";
}
pub struct GroupedIfs;
impl Format for GroupedIfs {
    const NAME: &'static str = "grouped_ifs";
}
pub struct GroupedCLZ;
impl Format for GroupedCLZ {
    const NAME: &'static str = "grouped_clz";
}

pub trait ContinuationBit {
    const NAME: &'static str;
    const INT: u8;
}
pub struct ZeroCont;
impl ContinuationBit for ZeroCont {
    const NAME: &'static str = "zero_cont";
    const INT: u8 = 0;
}
pub struct OneCont;
impl ContinuationBit for OneCont {
    const NAME: &'static str = "one_cont";
    const INT: u8 = 1;
}

pub trait IsComplete {
    const NAME: &'static str;
}
pub struct Complete;
impl IsComplete for Complete {
    const NAME: &'static str = "complete";
}
pub struct NonComplete;
impl IsComplete for NonComplete {
    const NAME: &'static str = "non_complete";
}

pub trait WithName {
    fn name() -> String;
}

pub trait ByteCode {
    fn read(r: &mut impl Read) -> Result<u64>;
    fn write(value: u64, w: &mut impl Write) -> Result<usize>;
}

pub fn bench_byte_stream<C: ByteCode + WithName>(c: &mut Criterion) {
    let mut v = <Vec<u8>>::with_capacity(CAPACITY);
    // test that the impl works
    {
        let vals = (0..64)
            .map(|i| 1 << i)
            .chain(0..1024)
            .chain([u64::MAX])
            .collect::<Vec<_>>();
        let mut w = std::io::Cursor::new(&mut v);
        for v in &vals {
            C::write(*v, &mut w).unwrap();
        }
        let mut r = std::io::Cursor::new(v.as_slice());
        for v in &vals {
            assert_eq!(C::read(&mut r).unwrap(), *v);
        }
    }

    let s = gen_vbyte_data(GAMMA_DATA);

    c.bench_function(&format!("bytes/{}/write", C::name()), |b| {
        b.iter(|| {
            let mut w = std::io::Cursor::new(&mut v);
            for &v in &s {
                black_box(C::write(v, &mut w).unwrap());
            }
        })
    });

    // Encode the read input into its own buffer so the read benchmark runs
    // independently of the write benchmark: Criterion skips a filtered-out
    // benchmark's body, so a read-only run (e.g. `-- read`) must not depend on
    // the write closure having populated `v`.
    let mut read_buf = <Vec<u8>>::with_capacity(CAPACITY);
    {
        let mut w = std::io::Cursor::new(&mut read_buf);
        for &v in &s {
            C::write(v, &mut w).unwrap();
        }
    }

    c.bench_function(&format!("bytes/{}/read", C::name()), |b| {
        b.iter(|| {
            let mut r = std::io::Cursor::new(read_buf.as_slice());
            for _ in &s {
                black_box(C::read(&mut r).unwrap());
            }
        });
    });
}

pub trait BitCode {
    fn read<E: Endianness>(r: &mut impl BitRead<E>) -> Result<u64>;
    fn write<E: Endianness>(value: u64, w: &mut impl BitWrite<E>) -> Result<usize>;
}

pub fn bench_bitstream<C: BitCode + WithName>(c: &mut Criterion) {
    bench_bitstream_with_endianness::<C, BigEndian>(c);
    bench_bitstream_with_endianness::<C, LittleEndian>(c);
}

fn bench_bitstream_with_endianness<C: BitCode + WithName, E: Endianness>(c: &mut Criterion)
where
    for<'a> BufBitReader<E, MemWordReader<u32, &'a [u32]>>: BitRead<E>,
    for<'a> BufBitWriter<E, MemWordWriterVec<u64, &'a mut Vec<u64>>>: BitWrite<E>,
{
    let mut v = <Vec<u64>>::with_capacity(CAPACITY);
    // test that the impl works
    {
        let vals = (0..64)
            .map(|i| 1 << i)
            .chain(0..1024)
            .chain([u64::MAX])
            .collect::<Vec<_>>();
        let mut w = BufBitWriter::<E, _>::new(MemWordWriterVec::new(&mut v));
        for v in &vals {
            C::write(*v, &mut w).unwrap();
        }
        drop(w);
        // SAFETY: Vec<u64> is aligned to 8 bytes, which satisfies alignment
        // for u32.
        let v = unsafe { v.align_to::<u32>().1 };
        let mut r = BufBitReader::<E, _>::new(MemWordReader::new(v));
        for v in &vals {
            assert_eq!(C::read(&mut r).unwrap(), *v);
        }
    }

    let s = gen_vbyte_data(GAMMA_DATA);

    let endian = if E::NAME == "big" { "BE" } else { "LE" };
    c.bench_function(&format!("bits/{}/{}/write", endian, C::name()), |b| {
        b.iter(|| {
            let mut w = <BufBitWriter<E, _>>::new(MemWordWriterVec::new(&mut v));
            for &v in &s {
                black_box(C::write(v, &mut w).unwrap());
            }
        })
    });

    // Encode the read input into its own buffer so the read benchmark runs
    // independently of the write benchmark (see `bench_byte_stream`).
    let mut read_v = <Vec<u64>>::with_capacity(CAPACITY);
    {
        let mut w = BufBitWriter::<E, _>::new(MemWordWriterVec::new(&mut read_v));
        for &v in &s {
            C::write(v, &mut w).unwrap();
        }
        drop(w);
    }
    // SAFETY: Vec<u64> is aligned to 8 bytes, which satisfies alignment for
    // u32.
    let read_words = unsafe { read_v.align_to::<u32>().1 };

    c.bench_function(&format!("bits/{}/{}/read", endian, C::name()), |b| {
        b.iter(|| {
            let mut r = BufBitReader::<E, _>::new(MemWordReader::new(read_words));
            for _ in &s {
                black_box(C::read(&mut r).unwrap());
            }
        })
    });
}

#[derive(Debug, Default, Clone, Copy)]
pub struct ByteStreamVByte<E: Endianness, F: Format, B: IsComplete, C: ContinuationBit>(
    PhantomData<(E, F, B, C)>,
);

impl<E: Endianness, F: Format, B: IsComplete, C: ContinuationBit> WithName
    for ByteStreamVByte<E, F, B, C>
{
    fn name() -> String {
        let endian = if E::NAME == "big" { "be" } else { "le" };
        format!("vbyte_{}/{}/{}/{}", endian, F::NAME, B::NAME, C::NAME)
    }
}

impl ByteCode for ByteStreamVByte<BE, GroupedIfs, Complete, OneCont> {
    fn read(r: &mut impl Read) -> Result<u64> {
        Ok(dsi_bitstream::codes::vbyte::vbyte_read_be(r)?)
    }
    fn write(value: u64, w: &mut impl Write) -> Result<usize> {
        Ok(dsi_bitstream::codes::vbyte::vbyte_write::<BE, _>(value, w)?)
    }
}

impl ByteCode for ByteStreamVByte<LE, GroupedIfs, Complete, OneCont> {
    fn read(r: &mut impl Read) -> Result<u64> {
        Ok(dsi_bitstream::codes::vbyte::vbyte_read_le(r)?)
    }
    fn write(value: u64, w: &mut impl Write) -> Result<usize> {
        Ok(dsi_bitstream::codes::vbyte::vbyte_write::<LE, _>(value, w)?)
    }
}

const UPPER_BOUND_1: u64 = 128;
const UPPER_BOUND_2: u64 = 128_u64.pow(2);
const UPPER_BOUND_3: u64 = 128_u64.pow(3);
const UPPER_BOUND_4: u64 = 128_u64.pow(4);
const UPPER_BOUND_5: u64 = 128_u64.pow(5);
const UPPER_BOUND_6: u64 = 128_u64.pow(6);
const UPPER_BOUND_7: u64 = 128_u64.pow(7);
const UPPER_BOUND_8: u64 = 128_u64.pow(8);

impl ByteCode for ByteStreamVByte<BE, GroupedCLZ, NonComplete, OneCont> {
    fn read(r: &mut impl Read) -> Result<u64> {
        let mut buffer = [0; 8];
        r.read_exact(&mut buffer[..1])?;

        if buffer[0] == 0xFF {
            r.read_exact(&mut buffer)?;
            return Ok(u64::from_be_bytes(buffer));
        }

        let len = buffer[0].leading_ones() as usize;
        let result = buffer[0] as u64 & (0xFF >> (len + 1));
        buffer[0] = 0;
        r.read_exact(&mut buffer[8 - len..])?;
        Ok(result << (len * 8) | u64::from_be_bytes(buffer))
    }
    fn write(value: u64, w: &mut impl Write) -> Result<usize> {
        if value < UPPER_BOUND_1 {
            w.write_all(&[value as u8])?;
            return Ok(1);
        }
        if value < UPPER_BOUND_2 {
            debug_assert!((value >> 8) < (1 << 6));
            w.write_all(&[0x80 | (value >> 8) as u8, value as u8])?;
            return Ok(2);
        }
        if value < UPPER_BOUND_3 {
            debug_assert!((value >> 16) < (1 << 5));
            w.write_all(&[0xC0 | (value >> 16) as u8, (value >> 8) as u8, value as u8])?;
            return Ok(3);
        }
        if value < UPPER_BOUND_4 {
            debug_assert!((value >> 24) < (1 << 4));
            w.write_all(&[
                0xE0 | (value >> 24) as u8,
                (value >> 16) as u8,
                (value >> 8) as u8,
                value as u8,
            ])?;
            return Ok(4);
        }
        if value < UPPER_BOUND_5 {
            debug_assert!((value >> 32) < (1 << 3));
            w.write_all(&[
                0xF0 | (value >> 32) as u8,
                (value >> 24) as u8,
                (value >> 16) as u8,
                (value >> 8) as u8,
                value as u8,
            ])?;
            return Ok(5);
        }
        if value < UPPER_BOUND_6 {
            debug_assert!((value >> 40) < (1 << 2));
            w.write_all(&[
                0xF8 | (value >> 40) as u8,
                (value >> 32) as u8,
                (value >> 24) as u8,
                (value >> 16) as u8,
                (value >> 8) as u8,
                value as u8,
            ])?;
            return Ok(6);
        }
        if value < UPPER_BOUND_7 {
            debug_assert!((value >> 48) < (1 << 1));
            w.write_all(&[
                0xFC | (value >> 48) as u8,
                (value >> 40) as u8,
                (value >> 32) as u8,
                (value >> 24) as u8,
                (value >> 16) as u8,
                (value >> 8) as u8,
                value as u8,
            ])?;
            return Ok(7);
        }
        if value < UPPER_BOUND_8 {
            w.write_all(&[
                0xFE,
                (value >> 48) as u8,
                (value >> 40) as u8,
                (value >> 32) as u8,
                (value >> 24) as u8,
                (value >> 16) as u8,
                (value >> 8) as u8,
                value as u8,
            ])?;
            return Ok(8);
        }

        w.write_all(&[
            0xFF,
            (value >> 56) as u8,
            (value >> 48) as u8,
            (value >> 40) as u8,
            (value >> 32) as u8,
            (value >> 24) as u8,
            (value >> 16) as u8,
            (value >> 8) as u8,
            value as u8,
        ])?;
        Ok(9)
    }
}

/// LLVM's implementation https://llvm.org/doxygen/LEB128_8h_source.html#l00080
impl<B: IsComplete + 'static, C: ContinuationBit> ByteCode
    for ByteStreamVByte<LittleEndian, NonGrouped, B, C>
{
    fn read(r: &mut impl Read) -> Result<u64> {
        let mut result = 0;
        let mut shift = 0;
        let mut buffer = [0; 1];
        loop {
            r.read_exact(&mut buffer)?;
            let byte = buffer[0];
            result += ((byte & 0x7F) as u64) << shift;
            if (byte >> 7) == (1 - C::INT) {
                break;
            }
            shift += 7;
            if core::any::TypeId::of::<B>() == core::any::TypeId::of::<Complete>() {
                result += 1 << shift;
            }
        }
        Ok(result)
    }
    fn write(mut value: u64, w: &mut impl Write) -> Result<usize> {
        let mut len = 1;
        loop {
            let byte = (value & 0x7F) as u8;
            value >>= 7;
            if value != 0 {
                w.write_all(&[byte | (0x80 * C::INT)])?;
            } else {
                w.write_all(&[byte | (0x80 * (1 - C::INT))])?;
                break;
            }
            if core::any::TypeId::of::<B>() == core::any::TypeId::of::<Complete>() {
                value -= 1;
            }
            len += 1;
        }
        Ok(len)
    }
}

/// Git implementation https://github.com/git/git/blob/7fb6aefd2aaffe66e614f7f7b83e5b7ab16d4806/varint.c#L4
impl<B: IsComplete + 'static, C: ContinuationBit> ByteCode
    for ByteStreamVByte<BigEndian, NonGrouped, B, C>
{
    fn read(r: &mut impl Read) -> Result<u64> {
        let mut buf = [0u8; 1];
        let mut value: u64;
        r.read_exact(&mut buf)?;
        value = (buf[0] & 0x7F) as u64;
        while (buf[0] >> 7) == C::INT {
            if core::any::TypeId::of::<B>() == core::any::TypeId::of::<Complete>() {
                value += 1;
            }
            r.read_exact(&mut buf)?;
            value = (value << 7) | ((buf[0] & 0x7F) as u64);
        }
        Ok(value)
    }

    fn write(mut value: u64, w: &mut impl Write) -> Result<usize> {
        let mut buf = [0u8; 10];
        let mut pos = buf.len() - 1;
        buf[pos] = (0x80 * (1 - C::INT)) | (value & 0x7F) as u8;
        value >>= 7;
        while value != 0 {
            if core::any::TypeId::of::<B>() == core::any::TypeId::of::<Complete>() {
                value -= 1;
            }
            pos -= 1;
            buf[pos] = (0x80 * C::INT) | ((value & 0x7F) as u8);
            value >>= 7;
        }
        let bytes_to_write = buf.len() - pos;
        w.write_all(&buf[pos..])?;
        Ok(bytes_to_write)
    }
}

#[derive(Debug, Default, Clone, Copy)]
pub struct BitStreamVByteBE<F: Format, B: IsComplete, C: ContinuationBit>(PhantomData<(F, B, C)>);

impl<F: Format, B: IsComplete, C: ContinuationBit> WithName for BitStreamVByteBE<F, B, C> {
    fn name() -> String {
        format!("vbyte_be/{}/{}/{}", F::NAME, B::NAME, C::NAME)
    }
}

impl BitCode for BitStreamVByteBE<GroupedIfs, Complete, OneCont> {
    #[inline(always)]
    fn read<E: Endianness>(r: &mut impl BitRead<E>) -> Result<u64> {
        Ok(r.read_vbyte_be()?)
    }
    #[inline(always)]
    fn write<E: Endianness>(value: u64, w: &mut impl BitWrite<E>) -> Result<usize> {
        Ok(w.write_vbyte_be(value)?)
    }
}

#[derive(Debug, Default, Clone, Copy)]
pub struct BitStreamVByteLE<F: Format, B: IsComplete, C: ContinuationBit>(PhantomData<(F, B, C)>);

impl<F: Format, B: IsComplete, C: ContinuationBit> WithName for BitStreamVByteLE<F, B, C> {
    fn name() -> String {
        format!("vbyte_le/{}/{}/{}", F::NAME, B::NAME, C::NAME)
    }
}

impl BitCode for BitStreamVByteLE<GroupedIfs, Complete, OneCont> {
    #[inline(always)]
    fn read<E: Endianness>(r: &mut impl BitRead<E>) -> Result<u64> {
        Ok(r.read_vbyte_le()?)
    }
    #[inline(always)]
    fn write<E: Endianness>(value: u64, w: &mut impl BitWrite<E>) -> Result<usize> {
        Ok(w.write_vbyte_le(value)?)
    }
}

pub fn benchmark(c: &mut Criterion) {
    bench_byte_stream::<ByteStreamVByte<LE, NonGrouped, Complete, OneCont>>(c);
    bench_byte_stream::<ByteStreamVByte<LE, NonGrouped, Complete, ZeroCont>>(c);
    bench_byte_stream::<ByteStreamVByte<LE, NonGrouped, NonComplete, OneCont>>(c);
    bench_byte_stream::<ByteStreamVByte<LE, NonGrouped, NonComplete, ZeroCont>>(c);

    bench_byte_stream::<ByteStreamVByte<BE, NonGrouped, Complete, OneCont>>(c);
    bench_byte_stream::<ByteStreamVByte<BE, NonGrouped, Complete, ZeroCont>>(c);
    bench_byte_stream::<ByteStreamVByte<BE, NonGrouped, NonComplete, OneCont>>(c);
    bench_byte_stream::<ByteStreamVByte<BE, NonGrouped, NonComplete, ZeroCont>>(c);

    bench_byte_stream::<ByteStreamVByte<LE, GroupedIfs, Complete, OneCont>>(c);
    bench_byte_stream::<ByteStreamVByte<BE, GroupedIfs, Complete, OneCont>>(c);

    bench_bitstream::<BitStreamVByteBE<GroupedIfs, Complete, OneCont>>(c);
    bench_bitstream::<BitStreamVByteLE<GroupedIfs, Complete, OneCont>>(c);

    bench_byte_stream::<ByteStreamVByte<BE, GroupedCLZ, NonComplete, OneCont>>(c);
}

criterion_group! {
    name = vbyte_benches;
    config = Criterion::default()
        .warm_up_time(Duration::from_secs(1))
        .measurement_time(Duration::from_secs(5))
        .sample_size(100);
    targets = benchmark
}
criterion_main!(vbyte_benches);