From 91cdbfe7729733d2a4b555df089cd643f7d64dd6 Mon Sep 17 00:00:00 2001 From: 404Setup <153366651+404Setup@users.noreply.github.com> Date: Wed, 29 Apr 2026 07:02:28 +0000 Subject: [PATCH] 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. --- src/decompress/mod.rs | 27 +++++++++------------------ 1 file changed, 9 insertions(+), 18 deletions(-) diff --git a/src/decompress/mod.rs b/src/decompress/mod.rs index dd79637..96fe3dd 100644 --- a/src/decompress/mod.rs +++ b/src/decompress/mod.rs @@ -462,12 +462,9 @@ impl Decompressor { let rep_count = 3 + ((self.bitbuf & 3) as usize); self.bitbuf >>= 2; self.bitsleft -= 2; - for _ in 0..rep_count { - if i < total_syms { - self.lens[i] = rep_val; - i += 1; - } - } + let fill_len = min(rep_count, total_syms - i); + self.lens[i..i + fill_len].fill(rep_val); + i += fill_len; } else if presym == 17 { if self.bitsleft < 3 { return DecompressResult::ShortInput; @@ -475,12 +472,9 @@ impl Decompressor { let rep_count = 3 + ((self.bitbuf & 7) as usize); self.bitbuf >>= 3; self.bitsleft -= 3; - for _ in 0..rep_count { - if i < total_syms { - self.lens[i] = 0; - i += 1; - } - } + let fill_len = min(rep_count, total_syms - i); + self.lens[i..i + fill_len].fill(0); + i += fill_len; } else { if self.bitsleft < 7 { return DecompressResult::ShortInput; @@ -488,12 +482,9 @@ impl Decompressor { let rep_count = 11 + ((self.bitbuf & 0x7F) as usize); self.bitbuf >>= 7; self.bitsleft -= 7; - for _ in 0..rep_count { - if i < total_syms { - self.lens[i] = 0; - i += 1; - } - } + let fill_len = min(rep_count, total_syms - i); + self.lens[i..i + fill_len].fill(0); + i += fill_len; } } if i != total_syms {