Skip to content

Commit b1794df

Browse files
CommanderStormpre-commit-ci[bot]Copilot
authored
chore: start refactoring to only use bytes by refactoring the ByteBuffer internals (#47)
this is the first of many PR, otherwise I cannot make this reviewable. I am a abit tiread, will go over my changes tomorrow again Works towards #35 --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent c292ddb commit b1794df

5 files changed

Lines changed: 59 additions & 174 deletions

File tree

Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,10 @@ harness = false
2323
# Keeping it on for now to simplify development.
2424
default = ["cpp"]
2525
cpp = ["dep:cmake", "dep:cxx", "dep:cxx-build"]
26-
rust = ["dep:thiserror"]
26+
rust = ["dep:thiserror", "dep:bytes"]
2727

2828
[dependencies]
29+
bytes = { version = "1.11", optional = true }
2930
cxx = { version = "1.0.194", optional = true }
3031
thiserror = { version = "2.0.18", optional = true }
3132

src/rust/bytebuffer.rs

Lines changed: 0 additions & 115 deletions
This file was deleted.

src/rust/integer_compression/fastpfor.rs

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ use std::io::Cursor;
22

33
use crate::rust::cursor::IncrementCursor;
44
use crate::rust::integer_compression::{bitpacking, helpers};
5-
use crate::rust::{bytebuffer, FastPForResult, Integer, Skippable};
5+
use crate::rust::{FastPForResult, Integer, Skippable};
6+
use bytes::{Buf as _, BufMut as _, BytesMut};
67

78
/// Block size constant for 256 integers per block
89
pub const BLOCK_SIZE_256: u32 = 256;
@@ -29,7 +30,7 @@ pub struct FastPFOR {
2930
/// Exception values indexed by bit width difference
3031
pub data_to_be_packed: Vec<Vec<u32>>,
3132
/// Metadata buffer for encoding/decoding
32-
pub bytes_container: bytebuffer::ByteBuffer,
33+
pub bytes_container: BytesMut,
3334
/// Maximum integers per page
3435
pub page_size: u32,
3536
/// Position trackers for exception arrays
@@ -146,7 +147,9 @@ impl FastPFOR {
146147
FastPFOR {
147148
page_size,
148149
block_size,
149-
bytes_container: bytebuffer::ByteBuffer::new(3 * page_size / block_size + page_size),
150+
bytes_container: BytesMut::with_capacity(
151+
(3 * page_size / block_size + page_size) as usize,
152+
),
150153
data_to_be_packed: vec![vec![0; page_size as usize / 32 * 4]; 33],
151154
data_pointers: vec![0; 33],
152155
freqs: vec![0; 33],
@@ -188,10 +191,10 @@ impl FastPFOR {
188191
while tmp_input_offset <= final_input_offset {
189192
self.best_b_from_data(input, tmp_input_offset);
190193
let tmp_best_b = self.optimal_bits;
191-
self.bytes_container.put(self.optimal_bits as u8);
192-
self.bytes_container.put(self.exception_count as u8);
194+
self.bytes_container.put_u8(self.optimal_bits as u8);
195+
self.bytes_container.put_u8(self.exception_count as u8);
193196
if self.exception_count > 0 {
194-
self.bytes_container.put(self.max_bits as u8);
197+
self.bytes_container.put_u8(self.max_bits as u8);
195198
let index = self.max_bits - self.optimal_bits;
196199
if self.data_pointers[index as usize] + self.exception_count as usize
197200
>= self.data_to_be_packed[index as usize].len()
@@ -204,7 +207,7 @@ impl FastPFOR {
204207
}
205208
for k in 0..self.block_size {
206209
if (input[(k + tmp_input_offset) as usize] >> self.optimal_bits) != 0 {
207-
self.bytes_container.put(k as u8);
210+
self.bytes_container.put_u8(k as u8);
208211
self.data_to_be_packed[index as usize]
209212
[self.data_pointers[index as usize]] =
210213
input[(k + tmp_input_offset) as usize] >> tmp_best_b;
@@ -226,22 +229,23 @@ impl FastPFOR {
226229
}
227230
input_offset.set_position(u64::from(tmp_input_offset));
228231
output[header_pos] = tmp_output_offset - header_pos as u32;
229-
let byte_size = self.bytes_container.position();
230-
while (self.bytes_container.position() & 3) != 0 {
231-
self.bytes_container.put(0);
232+
let byte_size = self.bytes_container.len();
233+
while (self.bytes_container.len() & 3) != 0 {
234+
self.bytes_container.put_u8(0);
232235
}
233236
// Output should have 3 position as 4
234-
output[tmp_output_offset as usize] = byte_size;
237+
output[tmp_output_offset as usize] = byte_size as u32;
235238
tmp_output_offset += 1;
236-
let how_many_ints = self.bytes_container.position() / 4;
237-
self.bytes_container.flip();
238-
239-
self.bytes_container.as_int_buffer().get(
240-
output,
241-
tmp_output_offset as usize,
242-
how_many_ints as usize,
243-
);
244-
tmp_output_offset += how_many_ints;
239+
let how_many_ints = self.bytes_container.len() / 4;
240+
241+
for it in output
242+
.iter_mut()
243+
.skip(tmp_output_offset as usize)
244+
.take(how_many_ints)
245+
{
246+
*it = self.bytes_container.get_u32_le();
247+
}
248+
tmp_output_offset += how_many_ints as u32;
245249
let mut bitmap = 0;
246250
for k in 2..=32 {
247251
if self.data_pointers[k] != 0 {
@@ -343,10 +347,9 @@ impl FastPFOR {
343347
inexcept += 1;
344348
self.bytes_container.clear();
345349
let length = bytesize.div_ceil(4);
346-
self.bytes_container.buffer =
347-
self.bytes_container
348-
.as_int_buffer()
349-
.put(input, inexcept as usize, length);
350+
for i in inexcept..inexcept + length {
351+
self.bytes_container.put_u32_le(input[i as usize]);
352+
}
350353
inexcept += length;
351354

352355
let bitmap = input[inexcept as usize];
@@ -405,8 +408,8 @@ impl FastPFOR {
405408

406409
let run_end = thissize / self.block_size;
407410
for _ in 0..run_end {
408-
let b = u32::from(self.bytes_container.get());
409-
let cexcept = self.bytes_container.get();
411+
let b = u32::from(self.bytes_container.get_u8());
412+
let cexcept = self.bytes_container.get_u8();
410413
for k in (0..self.block_size).step_by(32) {
411414
bitpacking::fast_unpack(
412415
input,
@@ -418,16 +421,16 @@ impl FastPFOR {
418421
tmp_input_offset += b;
419422
}
420423
if cexcept > 0 {
421-
let maxbits = u32::from(self.bytes_container.get());
424+
let maxbits = u32::from(self.bytes_container.get_u8());
422425
let index = maxbits - b;
423426
if index == 1 {
424427
for _ in 0..cexcept {
425-
let pos = self.bytes_container.get();
428+
let pos = self.bytes_container.get_u8();
426429
output[pos as usize + tmp_output_offset as usize] |= 1 << b;
427430
}
428431
} else {
429432
for _ in 0..cexcept {
430-
let pos = self.bytes_container.get();
433+
let pos = self.bytes_container.get_u8();
431434
let except_value = self.data_to_be_packed[index as usize]
432435
[self.data_pointers[index as usize]];
433436
output[pos as usize + tmp_output_offset as usize] |= except_value << b;

src/rust/integer_compression/variable_byte.rs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
use std::io::Cursor;
22

3-
use crate::rust::bytebuffer::ByteBuffer;
43
use crate::rust::cursor::IncrementCursor;
54
use crate::rust::integer_compression::helpers::{extract7bits, extract_7bits_maskless};
65
use crate::rust::{FastPForError, FastPForResult, Integer, Skippable};
6+
use bytes::{Buf as _, BufMut as _, BytesMut};
77

88
#[derive(Debug)]
99
pub struct VariableByte;
@@ -35,43 +35,40 @@ impl Skippable for VariableByte {
3535
// Return early if there is no data to compress
3636
return Ok(());
3737
}
38-
let mut buf = ByteBuffer::new(input_length * 8);
38+
let mut buf = BytesMut::with_capacity(input_length as usize * 8);
3939
for k in input_offset.position()..(input_offset.position() + u64::from(input_length)) {
4040
let val = i64::from(input[k as usize]);
4141
if val < (1 << 7) {
42-
buf.put((val | (1 << 7)) as u8);
42+
buf.put_u8((val | (1 << 7)) as u8);
4343
} else if val < (1 << 14) {
44-
buf.put(extract7bits(0, val));
45-
buf.put(extract_7bits_maskless(1, val) | (1 << 7));
44+
buf.put_u8(extract7bits(0, val));
45+
buf.put_u8(extract_7bits_maskless(1, val) | (1 << 7));
4646
} else if val < (1 << 21) {
47-
buf.put(extract7bits(0, val));
48-
buf.put(extract7bits(1, val));
49-
buf.put(extract_7bits_maskless(2, val) | (1 << 7));
47+
buf.put_u8(extract7bits(0, val));
48+
buf.put_u8(extract7bits(1, val));
49+
buf.put_u8(extract_7bits_maskless(2, val) | (1 << 7));
5050
} else if val < (1 << 28) {
51-
buf.put(extract7bits(0, val));
52-
buf.put(extract7bits(1, val));
53-
buf.put(extract7bits(2, val));
54-
buf.put(extract_7bits_maskless(3, val) | (1 << 7));
51+
buf.put_u8(extract7bits(0, val));
52+
buf.put_u8(extract7bits(1, val));
53+
buf.put_u8(extract7bits(2, val));
54+
buf.put_u8(extract_7bits_maskless(3, val) | (1 << 7));
5555
} else {
56-
buf.put(extract7bits(0, val));
57-
buf.put(extract7bits(1, val));
58-
buf.put(extract7bits(2, val));
59-
buf.put(extract7bits(3, val));
60-
buf.put(extract_7bits_maskless(4, val) | (1 << 7));
56+
buf.put_u8(extract7bits(0, val));
57+
buf.put_u8(extract7bits(1, val));
58+
buf.put_u8(extract7bits(2, val));
59+
buf.put_u8(extract7bits(3, val));
60+
buf.put_u8(extract_7bits_maskless(4, val) | (1 << 7));
6161
}
6262
}
63-
while buf.position() % 4 != 0 {
64-
buf.put(0);
63+
while buf.len() % 4 != 0 {
64+
buf.put_u8(0);
6565
}
66-
let length = buf.position();
67-
buf.flip();
68-
let ibuf = buf.as_int_buffer();
69-
ibuf.get(
70-
output,
71-
output_offset.position() as usize,
72-
(length / 4) as usize,
73-
);
74-
output_offset.add(length / 4);
66+
let length = buf.len();
67+
let output_position = output_offset.position() as usize;
68+
for it in output.iter_mut().skip(output_position).take(length / 4) {
69+
*it = buf.get_u32_le();
70+
}
71+
output_offset.add(length as u32 / 4);
7572
input_offset.add(input_length);
7673

7774
Ok(())

src/rust/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
pub mod bytebuffer;
21
mod cursor;
32
mod error;
43
mod integer_compression;

0 commit comments

Comments
 (0)