Skip to content

Commit a9034cb

Browse files
Optimize Compressor by reusing matches vector (#375)
Moved the allocation of the `matches` vector from `calculate_block_size_near_optimal` and `compress_near_optimal_block` to the `Compressor` struct. This avoids repeated allocations for every block processed, improving Level 10 compression performance by approximately 5%. - Added `matches: Vec<(u16, u16)>` to `Compressor` struct. - Initialized `matches` in `Compressor::new`. - Reused `self.matches` in `calculate_block_size_near_optimal` and `compress_near_optimal_block`. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 85ee61e commit a9034cb

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

src/compress/mod.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -455,6 +455,7 @@ pub struct Compressor {
455455
dp_costs: Vec<u32>,
456456
dp_path: Vec<u32>,
457457
split_stats: BlockSplitStats,
458+
matches: Vec<(u16, u16)>,
458459
}
459460

460461
impl Compressor {
@@ -498,6 +499,11 @@ impl Compressor {
498499
Vec::new()
499500
},
500501
split_stats: BlockSplitStats::new(),
502+
matches: if level >= 10 {
503+
Vec::with_capacity(32)
504+
} else {
505+
Vec::new()
506+
},
501507
};
502508
c.init_params();
503509
c
@@ -960,7 +966,6 @@ impl Compressor {
960966
}
961967

962968
mf.reset();
963-
let mut matches = Vec::new();
964969
let mut pos = 0;
965970
while pos < processed {
966971
let cur_cost = self.dp_costs[pos];
@@ -980,10 +985,10 @@ impl Compressor {
980985
pos,
981986
self.max_search_depth,
982987
self.nice_match_length,
983-
&mut matches,
988+
&mut self.matches,
984989
);
985990
let mut best_len = 0;
986-
for &(len, offset) in &matches {
991+
for &(len, offset) in &self.matches {
987992
let len = len as usize;
988993
if pos + len > processed {
989994
continue;
@@ -1757,7 +1762,6 @@ impl Compressor {
17571762
}
17581763

17591764
mf.reset();
1760-
let mut matches = Vec::new();
17611765
let mut pos = 0;
17621766
while pos < processed {
17631767
let cur_cost = self.dp_costs[pos];
@@ -1777,10 +1781,10 @@ impl Compressor {
17771781
pos,
17781782
self.max_search_depth,
17791783
self.nice_match_length,
1780-
&mut matches,
1784+
&mut self.matches,
17811785
);
17821786
let mut best_len = 0;
1783-
for &(len, offset) in &matches {
1787+
for &(len, offset) in &self.matches {
17841788
let len = len as usize;
17851789
if pos + len > processed {
17861790
continue;

0 commit comments

Comments
 (0)