Skip to content

Commit 3982963

Browse files
committed
perf(codegen): remove branch (#12037)
Follow-on after #11782. Add `#[inline(always)]` to `is_script_close_tag`, to ensure compiler can deduce the slice is 8 bytes long, and remove a branch.
1 parent e0d70ef commit 3982963

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

crates/oxc_codegen/src/str.rs

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -723,19 +723,22 @@ pub fn cold_branch<F: FnOnce() -> T, T>(f: F) -> T {
723723
f()
724724
}
725725

726-
/// Check if the slice is `</script` regardless of case.
726+
/// Check if `slice` is `</script`, regardless of case.
727+
///
728+
/// `slice.len()` must be 8.
729+
//
730+
// `#[inline(always)]` so that compiler can see from caller that `slice.len() == 8`
731+
// and so `slice.try_into().unwrap()` cannot fail. This function is only 4 instructions.
732+
#[expect(clippy::inline_always)]
733+
#[inline(always)]
727734
pub fn is_script_close_tag(slice: &[u8]) -> bool {
728-
if slice.len() == 8 {
729-
// Compiler condenses these operations to an 8-byte read, u64 AND, and u64 compare.
730-
// https://godbolt.org/z/oGG16fK6v
731-
let mut slice: [u8; 8] = slice.try_into().unwrap();
732-
for b in slice.iter_mut().skip(2) {
733-
// `| 32` converts ASCII upper case letters to lower case.
734-
*b |= 32;
735-
}
736-
737-
slice == *b"</script"
738-
} else {
739-
false
735+
// Compiler condenses these operations to an 8-byte read, u64 AND, and u64 compare.
736+
// https://godbolt.org/z/K8q68WGn6
737+
let mut slice: [u8; 8] = slice.try_into().unwrap();
738+
for b in slice.iter_mut().skip(2) {
739+
// `| 32` converts ASCII upper case letters to lower case.
740+
*b |= 32;
740741
}
742+
743+
slice == *b"</script"
741744
}

0 commit comments

Comments
 (0)