Skip to content

Commit

Permalink
perf: UninhabitedEnumBranching void n^2
Browse files Browse the repository at this point in the history
Avoid n² complexity. This showed up in a profile for match-stress-enum that has 8192 variants
  • Loading branch information
simonvandel committed Oct 7, 2020
1 parent ea7e131 commit e231c47
Showing 1 changed file with 7 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! A pass that eliminates branches on uninhabited enum variants.

use crate::transform::MirPass;
use rustc_data_structures::stable_set::FxHashSet;
use rustc_middle::mir::{
BasicBlock, BasicBlockData, Body, Local, Operand, Rvalue, StatementKind, TerminatorKind,
};
Expand Down Expand Up @@ -52,9 +53,13 @@ fn variant_discriminants<'tcx>(
layout: &TyAndLayout<'tcx>,
ty: Ty<'tcx>,
tcx: TyCtxt<'tcx>,
) -> Vec<u128> {
) -> FxHashSet<u128> {
match &layout.variants {
Variants::Single { index } => vec![index.as_u32() as u128],
Variants::Single { index } => {
let mut res = FxHashSet::default();
res.insert(index.as_u32() as u128);
res
}
Variants::Multiple { variants, .. } => variants
.iter_enumerated()
.filter_map(|(idx, layout)| {
Expand Down

0 comments on commit e231c47

Please sign in to comment.