3
Github str::is_char_boundary - slight optimization by Soveu · Pull Request #8475...
source link: https://github.com/rust-lang/rust/pull/84751
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
Current str::is_char_boundary
implementation emits slightly more instructions, because it includes an additional branch for index == s.len()
pub fn is_char_boundary(s: &str, index: usize) -> bool { if index == 0 || index == s.len() { return true; } match s.as_bytes().get(index) { None => false, Some(&b) => (b as i8) >= -0x40, } }
Just changing the place of index == s.len()
merges it with index < s.len()
from s.as_bytes().get(index)
pub fn is_char_boundary2(s: &str, index: usize) -> bool { if index == 0 { return true; } match s.as_bytes().get(index) { // For some reason, LLVM likes this comparison here more None => index == s.len(), // This is bit magic equivalent to: b < 128 || b >= 192 Some(&b) => (b as i8) >= -0x40, } }
This one has better codegen on every platform, except powerpc
x86 codegenaarch64 codegenriscv64gc codegen@rustbot label: A-codegen
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK