Skip to content

Commit 1c137b7

Browse files
Optimize core::char::CaseMappingIter layout
Godbolt says this saves a few instructions…
1 parent c8813dd commit 1c137b7

File tree

1 file changed

+27
-27
lines changed

1 file changed

+27
-27
lines changed

library/core/src/char/mod.rs

+27-27
Original file line numberDiff line numberDiff line change
@@ -443,10 +443,10 @@ impl ExactSizeIterator for ToUppercase {}
443443

444444
#[derive(Debug, Clone)]
445445
enum CaseMappingIter {
446-
Three(char, char, char),
447-
Two(char, char),
448-
One(char),
449446
Zero,
447+
One(char),
448+
Two([char; 2]),
449+
Three([char; 3]),
450450
}
451451

452452
impl CaseMappingIter {
@@ -455,10 +455,10 @@ impl CaseMappingIter {
455455
if chars[1] == '\0' {
456456
CaseMappingIter::One(chars[0]) // Including if chars[0] == '\0'
457457
} else {
458-
CaseMappingIter::Two(chars[0], chars[1])
458+
CaseMappingIter::Two([chars[0], chars[1]])
459459
}
460460
} else {
461-
CaseMappingIter::Three(chars[0], chars[1], chars[2])
461+
CaseMappingIter::Three([chars[0], chars[1], chars[2]])
462462
}
463463
}
464464
}
@@ -467,28 +467,28 @@ impl Iterator for CaseMappingIter {
467467
type Item = char;
468468
fn next(&mut self) -> Option<char> {
469469
match *self {
470-
CaseMappingIter::Three(a, b, c) => {
471-
*self = CaseMappingIter::Two(b, c);
472-
Some(a)
470+
CaseMappingIter::Zero => None,
471+
CaseMappingIter::One(c) => {
472+
*self = CaseMappingIter::Zero;
473+
Some(c)
473474
}
474-
CaseMappingIter::Two(b, c) => {
475+
CaseMappingIter::Two([b, c]) => {
475476
*self = CaseMappingIter::One(c);
476477
Some(b)
477478
}
478-
CaseMappingIter::One(c) => {
479-
*self = CaseMappingIter::Zero;
480-
Some(c)
479+
CaseMappingIter::Three([a, b, c]) => {
480+
*self = CaseMappingIter::Two([b, c]);
481+
Some(a)
481482
}
482-
CaseMappingIter::Zero => None,
483483
}
484484
}
485485

486486
fn size_hint(&self) -> (usize, Option<usize>) {
487487
let size = match self {
488-
CaseMappingIter::Three(..) => 3,
489-
CaseMappingIter::Two(..) => 2,
490-
CaseMappingIter::One(_) => 1,
491488
CaseMappingIter::Zero => 0,
489+
CaseMappingIter::One(_) => 1,
490+
CaseMappingIter::Two(..) => 2,
491+
CaseMappingIter::Three(..) => 3,
492492
};
493493
(size, Some(size))
494494
}
@@ -497,37 +497,37 @@ impl Iterator for CaseMappingIter {
497497
impl DoubleEndedIterator for CaseMappingIter {
498498
fn next_back(&mut self) -> Option<char> {
499499
match *self {
500-
CaseMappingIter::Three(a, b, c) => {
501-
*self = CaseMappingIter::Two(a, b);
500+
CaseMappingIter::Zero => None,
501+
CaseMappingIter::One(c) => {
502+
*self = CaseMappingIter::Zero;
502503
Some(c)
503504
}
504-
CaseMappingIter::Two(b, c) => {
505+
CaseMappingIter::Two([b, c]) => {
505506
*self = CaseMappingIter::One(b);
506507
Some(c)
507508
}
508-
CaseMappingIter::One(c) => {
509-
*self = CaseMappingIter::Zero;
509+
CaseMappingIter::Three([a, b, c]) => {
510+
*self = CaseMappingIter::Two([a, b]);
510511
Some(c)
511512
}
512-
CaseMappingIter::Zero => None,
513513
}
514514
}
515515
}
516516

517517
impl fmt::Display for CaseMappingIter {
518518
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
519519
match *self {
520-
CaseMappingIter::Three(a, b, c) => {
521-
f.write_char(a)?;
520+
CaseMappingIter::Zero => Ok(()),
521+
CaseMappingIter::One(c) => f.write_char(c),
522+
CaseMappingIter::Two([b, c]) => {
522523
f.write_char(b)?;
523524
f.write_char(c)
524525
}
525-
CaseMappingIter::Two(b, c) => {
526+
CaseMappingIter::Three([a, b, c]) => {
527+
f.write_char(a)?;
526528
f.write_char(b)?;
527529
f.write_char(c)
528530
}
529-
CaseMappingIter::One(c) => f.write_char(c),
530-
CaseMappingIter::Zero => Ok(()),
531531
}
532532
}
533533
}

0 commit comments

Comments
 (0)