Skip to content

Commit 9c0c105

Browse files
committed
rotate_right_1: special-case the 2-byte case
1 parent 4a018a5 commit 9c0c105

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

libbz2-rs-sys/src/decompress.rs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1270,8 +1270,20 @@ fn initialize_mtfa(mtfa: &mut [u8; 4096], mtfbase: &mut [u16; 16], nextSym: u16)
12701270
}
12711271

12721272
fn rotate_right_1(slice: &mut [u8]) {
1273-
let Some(&last) = slice.last() else { return };
1274-
1275-
slice.copy_within(0..slice.len() - 1, 1);
1276-
slice[0] = last;
1273+
match slice {
1274+
[] | [_] => { /* ignore */ }
1275+
[a, b] => {
1276+
// The 2-element case is fairly common, and because we already branch on the length,
1277+
// the check for `len == 2` is very cheap.
1278+
//
1279+
// On x86_64 the `rol` instruction is used to swap the bytes with just 1 instruction.
1280+
// See https://godbolt.org/z/385K7qs91
1281+
core::mem::swap(a, b)
1282+
}
1283+
[.., last] => {
1284+
let last = *last;
1285+
slice.copy_within(0..slice.len() - 1, 1);
1286+
slice[0] = last;
1287+
}
1288+
}
12771289
}

0 commit comments

Comments
 (0)