Skip to content

Commit 91cdbfe

Browse files
committed
optimize: replace manual repetition loops with slice fill in decompressor
In \`read_dynamic_huffman_header\`, the loops used to repeat symbol lengths (codes 16, 17, and 18) were doing per-element assignments and bounds checks. Replacing these with \`slice::fill\` is more idiomatic and performant, as it allows the compiler to optimize the fill operation and avoids unnecessary branching. Safety is maintained by using \`min\` to clamp the fill length to the remaining number of symbols.
1 parent 90c8d41 commit 91cdbfe

1 file changed

Lines changed: 9 additions & 18 deletions

File tree

src/decompress/mod.rs

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -462,38 +462,29 @@ impl Decompressor {
462462
let rep_count = 3 + ((self.bitbuf & 3) as usize);
463463
self.bitbuf >>= 2;
464464
self.bitsleft -= 2;
465-
for _ in 0..rep_count {
466-
if i < total_syms {
467-
self.lens[i] = rep_val;
468-
i += 1;
469-
}
470-
}
465+
let fill_len = min(rep_count, total_syms - i);
466+
self.lens[i..i + fill_len].fill(rep_val);
467+
i += fill_len;
471468
} else if presym == 17 {
472469
if self.bitsleft < 3 {
473470
return DecompressResult::ShortInput;
474471
}
475472
let rep_count = 3 + ((self.bitbuf & 7) as usize);
476473
self.bitbuf >>= 3;
477474
self.bitsleft -= 3;
478-
for _ in 0..rep_count {
479-
if i < total_syms {
480-
self.lens[i] = 0;
481-
i += 1;
482-
}
483-
}
475+
let fill_len = min(rep_count, total_syms - i);
476+
self.lens[i..i + fill_len].fill(0);
477+
i += fill_len;
484478
} else {
485479
if self.bitsleft < 7 {
486480
return DecompressResult::ShortInput;
487481
}
488482
let rep_count = 11 + ((self.bitbuf & 0x7F) as usize);
489483
self.bitbuf >>= 7;
490484
self.bitsleft -= 7;
491-
for _ in 0..rep_count {
492-
if i < total_syms {
493-
self.lens[i] = 0;
494-
i += 1;
495-
}
496-
}
485+
let fill_len = min(rep_count, total_syms - i);
486+
self.lens[i..i + fill_len].fill(0);
487+
i += fill_len;
497488
}
498489
}
499490
if i != total_syms {

0 commit comments

Comments
 (0)