Skip to content

Commit 2ff01e8

Browse files
committed
Use swap_remove instead of shift_remove
`shift_remove` is very expensive
1 parent 0ecb033 commit 2ff01e8

File tree

4 files changed

+15
-46
lines changed

4 files changed

+15
-46
lines changed

compiler/rustc_resolve/src/ident.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -984,14 +984,9 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
984984

985985
// --- From now on we either have a glob resolution or no resolution. ---
986986

987-
// It is sorted before usage so ordering is not important.
988-
#[allow(rustc::potential_query_instability)]
989-
let mut single_imports: Vec<_> = resolution.single_imports.clone().into_iter().collect();
990-
single_imports.sort_by_key(|import| import.0.id());
991-
992987
// Check if one of single imports can still define the name,
993988
// if it can then our result is not determined and can be invalidated.
994-
for single_import in &single_imports {
989+
for single_import in &resolution.single_imports {
995990
if ignore_import == Some(*single_import) {
996991
// This branch handles a cycle in single imports.
997992
//

compiler/rustc_resolve/src/imports.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use std::cell::Cell;
44
use std::mem;
55

66
use rustc_ast::NodeId;
7-
use rustc_data_structures::fx::FxHashSet;
7+
use rustc_data_structures::fx::{FxHashSet, FxIndexSet};
88
use rustc_data_structures::intern::Interned;
99
use rustc_errors::codes::*;
1010
use rustc_errors::{Applicability, MultiSpan, pluralize, struct_span_code_err};
@@ -220,7 +220,7 @@ impl<'ra> ImportData<'ra> {
220220
pub(crate) struct NameResolution<'ra> {
221221
/// Single imports that may define the name in the namespace.
222222
/// Imports are arena-allocated, so it's ok to use pointers as keys.
223-
pub single_imports: FxHashSet<Import<'ra>>,
223+
pub single_imports: FxIndexSet<Import<'ra>>,
224224
/// The least shadowable known binding for this name, or None if there are no known bindings.
225225
pub binding: Option<NameBinding<'ra>>,
226226
pub shadowed_glob: Option<NameBinding<'ra>>,
@@ -482,7 +482,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
482482
let key = BindingKey::new(target, ns);
483483
let _ = this.try_define(import.parent_scope.module, key, dummy_binding, false);
484484
this.update_resolution(import.parent_scope.module, key, false, |_, resolution| {
485-
resolution.single_imports.remove(&import);
485+
resolution.single_imports.swap_remove(&import);
486486
})
487487
});
488488
self.record_use(target, dummy_binding, Used::Other);
@@ -837,7 +837,7 @@ impl<'ra, 'tcx> Resolver<'ra, 'tcx> {
837837
}
838838
let key = BindingKey::new(target, ns);
839839
this.update_resolution(parent, key, false, |_, resolution| {
840-
resolution.single_imports.remove(&import);
840+
resolution.single_imports.swap_remove(&import);
841841
});
842842
}
843843
}

compiler/rustc_resolve/src/late.rs

+6-10
Original file line numberDiff line numberDiff line change
@@ -259,7 +259,7 @@ impl RibKind<'_> {
259259
/// resolving, the name is looked up from inside out.
260260
#[derive(Debug)]
261261
pub(crate) struct Rib<'ra, R = Res> {
262-
pub bindings: FxHashMap<Ident, R>,
262+
pub bindings: FxIndexMap<Ident, R>,
263263
pub kind: RibKind<'ra>,
264264
}
265265

@@ -1548,7 +1548,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
15481548
// Allow all following defaults to refer to this type parameter.
15491549
forward_ty_ban_rib
15501550
.bindings
1551-
.remove(&Ident::with_dummy_span(param.ident.name));
1551+
.swap_remove(&Ident::with_dummy_span(param.ident.name));
15521552
}
15531553
GenericParamKind::Const { ref ty, kw_span: _, ref default } => {
15541554
// Const parameters can't have param bounds.
@@ -1576,7 +1576,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
15761576
// Allow all following defaults to refer to this const parameter.
15771577
forward_const_ban_rib
15781578
.bindings
1579-
.remove(&Ident::with_dummy_span(param.ident.name));
1579+
.swap_remove(&Ident::with_dummy_span(param.ident.name));
15801580
}
15811581
}
15821582
}
@@ -2795,12 +2795,8 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
27952795
break;
27962796
}
27972797

2798-
// It is sorted before usage so ordering is not important here.
2799-
#[allow(rustc::potential_query_instability)]
2800-
let mut idents: Vec<_> = parent_rib.bindings.keys().into_iter().collect();
2801-
idents.sort_by_key(|&ident| ident.span);
2802-
2803-
seen_bindings.extend(idents.into_iter().map(|ident| (*ident, ident.span)));
2798+
seen_bindings
2799+
.extend(parent_rib.bindings.keys().map(|ident| (*ident, ident.span)));
28042800
}
28052801
}
28062802

@@ -3872,7 +3868,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
38723868
}
38733869
}
38743870

3875-
fn innermost_rib_bindings(&mut self, ns: Namespace) -> &mut FxHashMap<Ident, Res> {
3871+
fn innermost_rib_bindings(&mut self, ns: Namespace) -> &mut FxIndexMap<Ident, Res> {
38763872
&mut self.ribs[ns].last_mut().unwrap().bindings
38773873
}
38783874

compiler/rustc_resolve/src/late/diagnostics.rs

+4-26
Original file line numberDiff line numberDiff line change
@@ -762,12 +762,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
762762
if let Some(rib) = &self.last_block_rib
763763
&& let RibKind::Normal = rib.kind
764764
{
765-
// It is sorted before usage so ordering is not important here.
766-
#[allow(rustc::potential_query_instability)]
767-
let mut bindings: Vec<_> = rib.bindings.clone().into_iter().collect();
768-
bindings.sort_by_key(|(ident, _)| ident.span);
769-
770-
for (ident, res) in &bindings {
765+
for (ident, &res) in &rib.bindings {
771766
if let Res::Local(_) = res
772767
&& path.len() == 1
773768
&& ident.span.eq_ctxt(path[0].ident.span)
@@ -949,12 +944,7 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
949944
if let Some(err_code) = err.code {
950945
if err_code == E0425 {
951946
for label_rib in &self.label_ribs {
952-
// It is sorted before usage so ordering is not important here.
953-
#[allow(rustc::potential_query_instability)]
954-
let mut bindings: Vec<_> = label_rib.bindings.clone().into_iter().collect();
955-
bindings.sort_by_key(|(ident, _)| ident.span);
956-
957-
for (label_ident, node_id) in &bindings {
947+
for (label_ident, node_id) in &label_rib.bindings {
958948
let ident = path.last().unwrap().ident;
959949
if format!("'{ident}") == label_ident.to_string() {
960950
err.span_label(label_ident.span, "a label with a similar name exists");
@@ -2152,8 +2142,6 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
21522142
};
21532143

21542144
// Locals and type parameters
2155-
// `names` is sorted below so ordering is not important here.
2156-
#[allow(rustc::potential_query_instability)]
21572145
for (ident, &res) in &rib.bindings {
21582146
if filter_fn(res) && ident.span.ctxt() == rib_ctxt {
21592147
names.push(TypoSuggestion::typo_from_ident(*ident, res));
@@ -2617,28 +2605,18 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
26172605
let within_scope = self.is_label_valid_from_rib(rib_index);
26182606

26192607
let rib = &self.label_ribs[rib_index];
2620-
// `names` is sorted below so ordering is not important here.
2621-
#[allow(rustc::potential_query_instability)]
2622-
let mut names = rib
2608+
let names = rib
26232609
.bindings
26242610
.iter()
26252611
.filter(|(id, _)| id.span.eq_ctxt(label.span))
26262612
.map(|(id, _)| id.name)
26272613
.collect::<Vec<Symbol>>();
26282614

2629-
// Make sure error reporting is deterministic.
2630-
names.sort();
2631-
26322615
find_best_match_for_name(&names, label.name, None).map(|symbol| {
2633-
// It is sorted before usage so ordering is not important here.
2634-
#[allow(rustc::potential_query_instability)]
2635-
let mut bindings: Vec<_> = rib.bindings.clone().into_iter().collect();
2636-
bindings.sort_by_key(|(ident, _)| ident.span);
2637-
26382616
// Upon finding a similar name, get the ident that it was from - the span
26392617
// contained within helps make a useful diagnostic. In addition, determine
26402618
// whether this candidate is within scope.
2641-
let (ident, _) = bindings.iter().find(|(ident, _)| ident.name == symbol).unwrap();
2619+
let (ident, _) = rib.bindings.iter().find(|(ident, _)| ident.name == symbol).unwrap();
26422620
(*ident, within_scope)
26432621
})
26442622
}

0 commit comments

Comments
 (0)