Skip to content

Commit 23c7bad

Browse files
committed
Auto merge of rust-lang#148157 - nnethercote:undo-chain, r=saethlin
Adjust successor iterators. Because rust-lang#148054 was a slight perf regression. The problem was seemingly because this iterator structure: ``` slice_iter.chain(Option_iter.chain(Option_iter)) ``` changed to this: ``` slice_iter.chain(Option_iter).chain(Option_iter) ``` The commit also tweaks the `slice_iter` part, changing `into_iter` to `iter` and using `[]` instead of `(&[])`, for conciseness and consistency. r? `@saethlin`
2 parents 647f153 + d18b0b4 commit 23c7bad

File tree

1 file changed

+22
-23
lines changed

1 file changed

+22
-23
lines changed

compiler/rustc_middle/src/mir/terminator.rs

Lines changed: 22 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -501,30 +501,37 @@ pub use helper::*;
501501

502502
mod helper {
503503
use super::*;
504-
// Note: the methods below use a `slice.chain(Option).chain(Option)` pattern so that all paths
505-
// produce an iterator with the same concrete type.
506504
pub type Successors<'a> = impl DoubleEndedIterator<Item = BasicBlock> + 'a;
507505

506+
// Note: this method ensures all paths below produce an iterator with the same concrete type.
507+
#[inline]
508+
#[define_opaque(Successors)]
509+
fn mk_successors(
510+
slice: &[BasicBlock],
511+
option1: Option<BasicBlock>,
512+
option2: Option<BasicBlock>,
513+
) -> Successors<'_> {
514+
slice.iter().copied().chain(option1.into_iter().chain(option2))
515+
}
516+
508517
impl SwitchTargets {
509518
/// Like [`SwitchTargets::target_for_value`], but returning the same type as
510519
/// [`Terminator::successors`].
511520
#[inline]
512-
#[define_opaque(Successors)]
513521
pub fn successors_for_value(&self, value: u128) -> Successors<'_> {
514522
let target = self.target_for_value(value);
515-
(&[]).into_iter().copied().chain(Some(target)).chain(None)
523+
mk_successors(&[], Some(target), None)
516524
}
517525
}
518526

519527
impl<'tcx> TerminatorKind<'tcx> {
520528
#[inline]
521-
#[define_opaque(Successors)]
522529
pub fn successors(&self) -> Successors<'_> {
523530
use self::TerminatorKind::*;
524531
match *self {
525532
// 3-successors for async drop: target, unwind, dropline (parent coroutine drop)
526533
Drop { target: ref t, unwind: UnwindAction::Cleanup(u), drop: Some(d), .. } => {
527-
slice::from_ref(t).into_iter().copied().chain(Some(u)).chain(Some(d))
534+
mk_successors(slice::from_ref(t), Some(u), Some(d))
528535
}
529536
// 2-successors
530537
Call { target: Some(ref t), unwind: UnwindAction::Cleanup(u), .. }
@@ -533,7 +540,7 @@ mod helper {
533540
| Drop { target: ref t, unwind: _, drop: Some(u), .. }
534541
| Assert { target: ref t, unwind: UnwindAction::Cleanup(u), .. }
535542
| FalseUnwind { real_target: ref t, unwind: UnwindAction::Cleanup(u) } => {
536-
slice::from_ref(t).into_iter().copied().chain(Some(u)).chain(None)
543+
mk_successors(slice::from_ref(t), Some(u), None)
537544
}
538545
// single successor
539546
Goto { target: ref t }
@@ -543,7 +550,7 @@ mod helper {
543550
| Drop { target: ref t, unwind: _, .. }
544551
| Assert { target: ref t, unwind: _, .. }
545552
| FalseUnwind { real_target: ref t, unwind: _ } => {
546-
slice::from_ref(t).into_iter().copied().chain(None).chain(None)
553+
mk_successors(slice::from_ref(t), None, None)
547554
}
548555
// No successors
549556
UnwindResume
@@ -552,25 +559,17 @@ mod helper {
552559
| Return
553560
| Unreachable
554561
| TailCall { .. }
555-
| Call { target: None, unwind: _, .. } => {
556-
(&[]).into_iter().copied().chain(None).chain(None)
557-
}
562+
| Call { target: None, unwind: _, .. } => mk_successors(&[], None, None),
558563
// Multiple successors
559564
InlineAsm { ref targets, unwind: UnwindAction::Cleanup(u), .. } => {
560-
targets.iter().copied().chain(Some(u)).chain(None)
561-
}
562-
InlineAsm { ref targets, unwind: _, .. } => {
563-
targets.iter().copied().chain(None).chain(None)
564-
}
565-
SwitchInt { ref targets, .. } => {
566-
targets.targets.iter().copied().chain(None).chain(None)
565+
mk_successors(targets, Some(u), None)
567566
}
567+
InlineAsm { ref targets, unwind: _, .. } => mk_successors(targets, None, None),
568+
SwitchInt { ref targets, .. } => mk_successors(&targets.targets, None, None),
568569
// FalseEdge
569-
FalseEdge { ref real_target, imaginary_target } => slice::from_ref(real_target)
570-
.into_iter()
571-
.copied()
572-
.chain(Some(imaginary_target))
573-
.chain(None),
570+
FalseEdge { ref real_target, imaginary_target } => {
571+
mk_successors(slice::from_ref(real_target), Some(imaginary_target), None)
572+
}
574573
}
575574
}
576575

0 commit comments

Comments
 (0)