Skip to content

Commit 656c1b6

Browse files
authored
Merge pull request #238 from 404Setup/bolt-literal-unroll-4217107458636207726
⚡ Bolt: Unroll literal writing loop for performance
2 parents bbe8091 + dd95dd3 commit 656c1b6

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

src/compress/mod.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,18 @@ impl Compressor {
11451145
// We add 16 bytes margin for the last flush (8 bytes) and safety.
11461146
if bs.out_idx + 16 + (seq.litrunlen as usize * 2) < bs.output.len() {
11471147
let mut lit_remain = seq.litrunlen as usize;
1148+
while lit_remain >= 4 {
1149+
// SAFETY: We verified sufficient buffer space above.
1150+
// `write_literals_2` writes at most 30 bits and may flush 4 bytes.
1151+
// We do this twice, so max 60 bits + flush overhead.
1152+
// The loop precondition checks for space.
1153+
unsafe {
1154+
self.write_literals_2(bs, input[in_pos], input[in_pos + 1]);
1155+
self.write_literals_2(bs, input[in_pos + 2], input[in_pos + 3]);
1156+
}
1157+
in_pos += 4;
1158+
lit_remain -= 4;
1159+
}
11481160
while lit_remain >= 2 {
11491161
// SAFETY: We verified sufficient buffer space above.
11501162
// `write_literals_2` writes at most 30 bits and may flush 4 bytes.
@@ -1355,6 +1367,14 @@ impl Compressor {
13551367
if seq.litrunlen > 0 {
13561368
if bs.out_idx + 16 + (seq.litrunlen as usize * 2) < bs.output.len() {
13571369
let mut lit_remain = seq.litrunlen as usize;
1370+
while lit_remain >= 4 {
1371+
unsafe {
1372+
self.write_literals_2(bs, input[in_pos], input[in_pos + 1]);
1373+
self.write_literals_2(bs, input[in_pos + 2], input[in_pos + 3]);
1374+
}
1375+
in_pos += 4;
1376+
lit_remain -= 4;
1377+
}
13581378
while lit_remain >= 2 {
13591379
unsafe { self.write_literals_2(bs, input[in_pos], input[in_pos + 1]) };
13601380
in_pos += 2;

0 commit comments

Comments
 (0)