Skip to content

Commit 0f27c1b

Browse files
committed
defids are indexmapped
1 parent 093b9d5 commit 0f27c1b

File tree

4 files changed

+19
-11
lines changed

4 files changed

+19
-11
lines changed

Cargo.lock

+1
Original file line numberDiff line numberDiff line change
@@ -4504,6 +4504,7 @@ dependencies = [
45044504
name = "rustc_smir"
45054505
version = "0.0.0"
45064506
dependencies = [
4507+
"rustc_data_structures",
45074508
"rustc_driver",
45084509
"rustc_hir",
45094510
"rustc_interface",

compiler/rustc_smir/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ version = "0.0.0"
44
edition = "2021"
55

66
[dependencies]
7+
rustc_data_structures = { path = "../rustc_data_structures" }
78
rustc_driver = { path = "../rustc_driver" }
89
rustc_hir = { path = "../rustc_hir" }
910
rustc_interface = { path = "../rustc_interface" }

compiler/rustc_smir/src/rustc_internal/mod.rs

+15-10
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ use std::ops::{ControlFlow, Index};
77

88
use crate::rustc_internal;
99
use crate::rustc_smir::Tables;
10+
use rustc_data_structures::fx;
1011
use rustc_driver::{Callbacks, Compilation, RunCompiler};
1112
use rustc_interface::{interface, Queries};
1213
use rustc_middle::mir::interpret::AllocId;
@@ -20,7 +21,7 @@ impl<'tcx> Index<stable_mir::DefId> for Tables<'tcx> {
2021

2122
#[inline(always)]
2223
fn index(&self, index: stable_mir::DefId) -> &Self::Output {
23-
&self.def_ids[index.0]
24+
&self.def_ids.get_index(index.0).unwrap().0
2425
}
2526
}
2627

@@ -95,15 +96,13 @@ impl<'tcx> Tables<'tcx> {
9596
}
9697

9798
fn create_def_id(&mut self, did: DefId) -> stable_mir::DefId {
98-
// FIXME: this becomes inefficient when we have too many ids
99-
for (i, &d) in self.def_ids.iter().enumerate() {
100-
if d == did {
101-
return stable_mir::DefId(i);
102-
}
99+
if let Some(i) = self.def_ids.get(&did) {
100+
return *i;
101+
} else {
102+
let id = self.def_ids.len();
103+
self.def_ids.insert(did, stable_mir::DefId(id));
104+
stable_mir::DefId(id)
103105
}
104-
let id = self.def_ids.len();
105-
self.def_ids.push(did);
106-
stable_mir::DefId(id)
107106
}
108107

109108
fn create_alloc_id(&mut self, aid: AllocId) -> stable_mir::AllocId {
@@ -134,7 +133,13 @@ pub fn crate_num(item: &stable_mir::Crate) -> CrateNum {
134133

135134
pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
136135
stable_mir::run(
137-
Tables { tcx, def_ids: vec![], alloc_ids: vec![], spans: vec![], types: vec![] },
136+
Tables {
137+
tcx,
138+
def_ids: fx::FxIndexMap::default(),
139+
alloc_ids: vec![],
140+
spans: vec![],
141+
types: vec![],
142+
},
138143
f,
139144
);
140145
}

compiler/rustc_smir/src/rustc_smir/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
1010
use crate::rustc_smir::hir::def::DefKind;
1111
use crate::rustc_smir::stable_mir::ty::{BoundRegion, EarlyBoundRegion, Region};
12+
use rustc_data_structures::fx::FxIndexMap;
1213
use rustc_hir as hir;
1314
use rustc_middle::mir;
1415
use rustc_middle::mir::interpret::{alloc_range, AllocId};
@@ -194,7 +195,7 @@ impl<S, R: PartialEq> PartialEq<R> for MaybeStable<S, R> {
194195

195196
pub struct Tables<'tcx> {
196197
pub tcx: TyCtxt<'tcx>,
197-
pub def_ids: Vec<DefId>,
198+
pub def_ids: FxIndexMap<DefId, stable_mir::DefId>,
198199
pub alloc_ids: Vec<AllocId>,
199200
pub spans: Vec<rustc_span::Span>,
200201
pub types: Vec<MaybeStable<stable_mir::ty::TyKind, Ty<'tcx>>>,

0 commit comments

Comments
 (0)