rust - Why are these ASCII methods inconsistent? -
when @ rust ascii operations feels there consistency issue between
is_lowercase/is_uppercase:
pub fn is_uppercase(&self) -> bool { (self.chr - b'a') < 26 }
is_alphabetic:
pub fn is_alphabetic(&self) -> bool { (self.chr >= 0x41 && self.chr <= 0x5a) || (self.chr >= 0x61 && self.chr <= 0x7a) }
is there reason? 2 methods totally equivalent or missing something? these functions marked stable i'm confused.
edit:
to make clearer, expect decide on best (in terms of performance/readability/common practice) implementation lower/upper have
pub fn is_alphabetic(&self) -> bool { self.is_lowercase() || self.is_uppercase() }
since question changed performance, i'll add second answer.
to start, created clone of ascii module (playpen):
pub struct alpha(u8); impl alpha { #[inline(never)] pub fn is_uppercase_sub(&self) -> bool { (self.0 - b'a') < 26 } #[inline(never)] pub fn is_uppercase_range(&self) -> bool { self.0 >= 0x41 && self.0 <= 0x5a } } fn main() { let yes = alpha(b'a'); let no = alpha(b'a'); println!("{}, {}", yes.is_uppercase_sub(), yes.is_uppercase_range()); }
in playpen, make sure optimization set -o2
, click ir
. shows llvm intermediate representation. it's higher-level assembly, if you'd like.
there's lots of output, sections fastcc
. i've removed various bits make code clearer, can see exact same function called, though our code calls 2 different implementations, 1 subtraction , 1 range:
%3 = call fastcc zeroext i1 @_zn5alpha16is_uppercase_sub20h63aa0b11479803f4laae %5 = call fastcc zeroext i1 @_zn5alpha16is_uppercase_sub20h63aa0b11479803f4laae
the llvm optimizer can tell these implementations same, it's developers preference. might able commit rust make them consistent, if you'd like! ^_^
asking is_alphabetic
harder; inlining come play here. if llvm inlines is_upper
, is_lower
is_alphabetic
, suggested change better. if doesn't, potentially 1 function call 3! could bad.
these types of questions lot harder answer @ level; 1 have looking (edit , profiling!) @ real rust code in large understand optimizer regards inlining.
Comments
Post a Comment