Skip to content

Commit

Permalink
remove allocation
Browse files Browse the repository at this point in the history
  • Loading branch information
rukai committed Sep 18, 2024
1 parent 8f8e710 commit 02d2d85
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
46 changes: 32 additions & 14 deletions brro-compressor/src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,21 +97,39 @@ impl CompressorFrame {
.compressed_data;
} else {
// Run all the eligible compressors and choose smallest
let compressor_results = compressor_list.iter().map(|compressor| {
(
compressor.get_compress_bounded_results(data, max_error as f64),
compressor,
)
});
let best_compressor = compressor_results
.clone()
.filter(|(result, _)| result.error <= max_error as f64)
.min_by_key(|x| x.0.compressed_data.len())
.or_else(|| compressor_results.min_by_key(|x| x.0.compressed_data.len()));
let compressor_results: Vec<_> = compressor_list
.iter()
.map(|compressor| {
(
compressor.get_compress_bounded_results(data, max_error as f64),
*compressor,
)
})
.collect();

#[allow(
clippy::neg_cmp_op_on_partial_ord,
reason = "we need to exactly negate `result.error < max_error`, we can't apply de morgans to the expression due to NaN values"
)]
let best_compressor = if compressor_results
.iter()
.all(|(result, _)| !(result.error <= max_error as f64))
{
// To ensure we always have at least one result,
// if all results are above the max error just pick the smallest.
compressor_results
.into_iter()
.min_by_key(|x| x.0.compressed_data.len())
} else {
compressor_results
.into_iter()
.filter(|(result, _)| result.error <= max_error as f64)
.min_by_key(|x| x.0.compressed_data.len())
};

let selection = best_compressor.unwrap();
self.data = selection.0.compressed_data;
self.compressor = *selection.1;
let (result, compressor) = best_compressor.unwrap();
self.data = result.compressed_data;
self.compressor = compressor;
}
debug!("Auto Compressor Selection: {:?}", self.compressor);
}
Expand Down
2 changes: 1 addition & 1 deletion rust-toolchain.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[toolchain]
channel = "1.80"
channel = "1.81"
components = [ "rustfmt", "clippy" ]

0 comments on commit 02d2d85

Please sign in to comment.