Skip to content

Commit bbe8091

Browse files
authored
Merge pull request #237 from 404Setup/perf/write-match-fast-17350663453863150625
⚡ Bolt: Optimize write_match_fast and fix CRC32 benchmark
2 parents 0a0ba73 + 0168ed1 commit bbe8091

2 files changed

Lines changed: 9 additions & 6 deletions

File tree

benches/bench_main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ fn bench_crc32_slice8(c: &mut Criterion) {
2828
group.throughput(Throughput::Bytes(size as u64));
2929

3030
group.bench_with_input("libdeflate-rs slice8", &size, |b, &_size| {
31-
b.iter(|| crc32_slice8(0, &data));
31+
b.iter(|| crc32_slice8(0, std::hint::black_box(&data)));
3232
});
3333

3434
group.finish();
@@ -1668,14 +1668,14 @@ fn bench_crc32_slice8_tail(c: &mut Criterion) {
16681668
let data = vec![0u8; size];
16691669
group.throughput(Throughput::Bytes(size as u64));
16701670
group.bench_with_input("15 bytes", &size, |b, &_size| {
1671-
b.iter(|| crc32_slice8(0, &data));
1671+
b.iter(|| crc32_slice8(0, std::hint::black_box(&data)));
16721672
});
16731673

16741674
let size = 3;
16751675
let data = vec![0u8; size];
16761676
group.throughput(Throughput::Bytes(size as u64));
16771677
group.bench_with_input("3 bytes", &size, |b, &_size| {
1678-
b.iter(|| crc32_slice8(0, &data));
1678+
b.iter(|| crc32_slice8(0, std::hint::black_box(&data)));
16791679
});
16801680

16811681
group.finish();

src/compress/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1789,8 +1789,6 @@ impl Compressor {
17891789
let len_val = code | ((len as u32).wrapping_sub(base) << huff_len);
17901790
let len_len = huff_len + extra_bits;
17911791

1792-
bs.write_bits_unchecked_fast(len_val, len_len);
1793-
17941792
let entry = *self.offset_table.get_unchecked(off_slot);
17951793
let off_code = entry as u32;
17961794
let off_len = (entry >> 32) as u8 as u32;
@@ -1800,7 +1798,12 @@ impl Compressor {
18001798
let off_val = off_code | ((offset as u32).wrapping_sub(base) << off_len);
18011799
let off_len_total = off_len + extra_bits;
18021800

1803-
bs.write_bits_unchecked_fast(off_val, off_len_total);
1801+
if len_len + off_len_total <= 32 {
1802+
bs.write_bits_unchecked_fast(len_val | (off_val << len_len), len_len + off_len_total);
1803+
} else {
1804+
bs.write_bits_unchecked_fast(len_val, len_len);
1805+
bs.write_bits_unchecked_fast(off_val, off_len_total);
1806+
}
18041807
}
18051808

18061809
fn write_match(&self, bs: &mut Bitstream, len: usize, offset: usize, off_slot: usize) -> bool {

0 commit comments

Comments
 (0)