Skip to content

Commit ae30939

Browse files
Weixing-ZhangWeixing Zhangpre-commit-ci[bot]
authored
refactor: remove redundant computation (#29)
Context: - Fixes a bug in headless_compress where this_size was not updated dynamically per iteration. Implemented: - Made `this_size` be updated per iteration - A unit test to ensure compression correctly handles inputs that do not fit exactly into page sizes. --------- Co-authored-by: Weixing Zhang <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 9cedbb3 commit ae30939

File tree

2 files changed

+42
-3
lines changed

2 files changed

+42
-3
lines changed

src/rust/integer_compression/fastpfor.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@ impl Skippable for FastPFOR {
3030
output_offset: &mut Cursor<u32>,
3131
) -> FastPForResult<()> {
3232
let inlength = helpers::greatest_multiple(input_length, self.block_size);
33-
let pos = input_offset.position() as u32;
34-
let final_inpos = pos + inlength;
33+
let final_inpos = input_offset.position() as u32 + inlength;
3534
while input_offset.position() as u32 != final_inpos {
36-
let this_size = std::cmp::min(self.page_size, final_inpos - pos);
35+
let this_size =
36+
std::cmp::min(self.page_size, final_inpos - input_offset.position() as u32);
3737
self.encode_page(input, this_size, input_offset, output, output_offset);
3838
}
3939
FastPForResult::Ok(())

tests/basic_tests.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,3 +376,42 @@ fn test_random_numbers() {
376376
}
377377
}
378378
}
379+
380+
#[test]
381+
fn test_fastpfor_headless_compress_unfit_pagesize() {
382+
// The input size is a multiple of 128 but does not fit the page size
383+
let test_input_size = 512 + BLOCK_SIZE_128;
384+
let page_size = 512;
385+
386+
let input: Vec<u32> = (0..test_input_size).collect();
387+
let mut output: Vec<u32> = vec![0; input.len()];
388+
let mut decoded: Vec<u32> = vec![0; input.len()];
389+
let mut input_offset = Cursor::new(0u32);
390+
let mut output_offset = Cursor::new(0u32);
391+
392+
let mut codec = FastPFOR::new(page_size, BLOCK_SIZE_128);
393+
codec
394+
.compress(
395+
&input,
396+
input.len() as u32,
397+
&mut input_offset,
398+
&mut output,
399+
&mut output_offset,
400+
)
401+
.expect("compression failed");
402+
403+
let compressed_len = output_offset.position() as usize;
404+
input_offset.set_position(0);
405+
406+
codec
407+
.uncompress(
408+
&output,
409+
compressed_len as u32,
410+
&mut input_offset,
411+
&mut decoded,
412+
&mut Cursor::new(0u32),
413+
)
414+
.expect("decompression failed");
415+
416+
assert_eq!(input, decoded, "Input and decompressed data do not match");
417+
}

0 commit comments

Comments
 (0)