Optimize core::char::CaseMappingIter layout

Godbolt says this saves a few instructions…
This commit is contained in:
Jules Bertholet 2024-03-16 23:27:50 -04:00
parent c8813ddd6d
commit 1c137b7582
No known key found for this signature in database
GPG Key ID: 32034DAFC38C1BFC

View File

@ -443,10 +443,10 @@ impl ExactSizeIterator for ToUppercase {}
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
enum CaseMappingIter { enum CaseMappingIter {
Three(char, char, char),
Two(char, char),
One(char),
Zero, Zero,
One(char),
Two([char; 2]),
Three([char; 3]),
} }
impl CaseMappingIter { impl CaseMappingIter {
@ -455,10 +455,10 @@ impl CaseMappingIter {
if chars[1] == '\0' { if chars[1] == '\0' {
CaseMappingIter::One(chars[0]) // Including if chars[0] == '\0' CaseMappingIter::One(chars[0]) // Including if chars[0] == '\0'
} else { } else {
CaseMappingIter::Two(chars[0], chars[1]) CaseMappingIter::Two([chars[0], chars[1]])
} }
} else { } else {
CaseMappingIter::Three(chars[0], chars[1], chars[2]) CaseMappingIter::Three([chars[0], chars[1], chars[2]])
} }
} }
} }
@ -467,28 +467,28 @@ impl Iterator for CaseMappingIter {
type Item = char; type Item = char;
fn next(&mut self) -> Option<char> { fn next(&mut self) -> Option<char> {
match *self { match *self {
CaseMappingIter::Three(a, b, c) => { CaseMappingIter::Zero => None,
*self = CaseMappingIter::Two(b, c);
Some(a)
}
CaseMappingIter::Two(b, c) => {
*self = CaseMappingIter::One(c);
Some(b)
}
CaseMappingIter::One(c) => { CaseMappingIter::One(c) => {
*self = CaseMappingIter::Zero; *self = CaseMappingIter::Zero;
Some(c) Some(c)
} }
CaseMappingIter::Zero => None, CaseMappingIter::Two([b, c]) => {
*self = CaseMappingIter::One(c);
Some(b)
}
CaseMappingIter::Three([a, b, c]) => {
*self = CaseMappingIter::Two([b, c]);
Some(a)
}
} }
} }
fn size_hint(&self) -> (usize, Option<usize>) { fn size_hint(&self) -> (usize, Option<usize>) {
let size = match self { let size = match self {
CaseMappingIter::Three(..) => 3,
CaseMappingIter::Two(..) => 2,
CaseMappingIter::One(_) => 1,
CaseMappingIter::Zero => 0, CaseMappingIter::Zero => 0,
CaseMappingIter::One(_) => 1,
CaseMappingIter::Two(..) => 2,
CaseMappingIter::Three(..) => 3,
}; };
(size, Some(size)) (size, Some(size))
} }
@ -497,19 +497,19 @@ impl Iterator for CaseMappingIter {
impl DoubleEndedIterator for CaseMappingIter { impl DoubleEndedIterator for CaseMappingIter {
fn next_back(&mut self) -> Option<char> { fn next_back(&mut self) -> Option<char> {
match *self { match *self {
CaseMappingIter::Three(a, b, c) => { CaseMappingIter::Zero => None,
*self = CaseMappingIter::Two(a, b);
Some(c)
}
CaseMappingIter::Two(b, c) => {
*self = CaseMappingIter::One(b);
Some(c)
}
CaseMappingIter::One(c) => { CaseMappingIter::One(c) => {
*self = CaseMappingIter::Zero; *self = CaseMappingIter::Zero;
Some(c) Some(c)
} }
CaseMappingIter::Zero => None, CaseMappingIter::Two([b, c]) => {
*self = CaseMappingIter::One(b);
Some(c)
}
CaseMappingIter::Three([a, b, c]) => {
*self = CaseMappingIter::Two([a, b]);
Some(c)
}
} }
} }
} }
@ -517,17 +517,17 @@ impl DoubleEndedIterator for CaseMappingIter {
impl fmt::Display for CaseMappingIter { impl fmt::Display for CaseMappingIter {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match *self { match *self {
CaseMappingIter::Three(a, b, c) => { CaseMappingIter::Zero => Ok(()),
CaseMappingIter::One(c) => f.write_char(c),
CaseMappingIter::Two([b, c]) => {
f.write_char(b)?;
f.write_char(c)
}
CaseMappingIter::Three([a, b, c]) => {
f.write_char(a)?; f.write_char(a)?;
f.write_char(b)?; f.write_char(b)?;
f.write_char(c) f.write_char(c)
} }
CaseMappingIter::Two(b, c) => {
f.write_char(b)?;
f.write_char(c)
}
CaseMappingIter::One(c) => f.write_char(c),
CaseMappingIter::Zero => Ok(()),
} }
} }
} }