about summary refs log tree commit diff
diff options
context:
space:
mode:
authorOğuz Ağcayazı <ouz.agz@gmail.com>2023-10-09 13:03:58 +0300
committerOğuz Ağcayazı <ouz.agz@gmail.com>2023-10-09 13:03:58 +0300
commit77df2cd9a5cbb461a0fd430f7d36f2e181a0386a (patch)
treedd1580b0cd5760598a54c0f7cdd4c11d982392e9
parent5f079dd2ffd0d09a3f2f9250c7d302efff1df4d3 (diff)
downloadrust-77df2cd9a5cbb461a0fd430f7d36f2e181a0386a.tar.gz
rust-77df2cd9a5cbb461a0fd430f7d36f2e181a0386a.zip
spans are now indexmapped
-rw-r--r--compiler/rustc_smir/src/rustc_internal/mod.rs18
-rw-r--r--compiler/rustc_smir/src/rustc_smir/mod.rs2
2 files changed, 9 insertions, 11 deletions
diff --git a/compiler/rustc_smir/src/rustc_internal/mod.rs b/compiler/rustc_smir/src/rustc_internal/mod.rs
index d0515df9ad5..25c7bd748ee 100644
--- a/compiler/rustc_smir/src/rustc_internal/mod.rs
+++ b/compiler/rustc_smir/src/rustc_internal/mod.rs
@@ -30,7 +30,7 @@ impl<'tcx> Index<stable_mir::ty::Span> for Tables<'tcx> {
 
     #[inline(always)]
     fn index(&self, index: stable_mir::ty::Span) -> &Self::Output {
-        &self.spans[index.0]
+        &self.spans.get_index(index.0).unwrap().0
     }
 }
 
@@ -106,7 +106,6 @@ impl<'tcx> Tables<'tcx> {
     }
 
     fn create_alloc_id(&mut self, aid: AllocId) -> stable_mir::AllocId {
-        // FIXME: this becomes inefficient when we have too many ids
         if let Some(i) = self.alloc_ids.get(&aid) {
             return *i;
         } else {
@@ -117,14 +116,13 @@ impl<'tcx> Tables<'tcx> {
     }
 
     pub(crate) fn create_span(&mut self, span: Span) -> stable_mir::ty::Span {
-        for (i, &sp) in self.spans.iter().enumerate() {
-            if sp == span {
-                return stable_mir::ty::Span(i);
-            }
+        if let Some(i) = self.spans.get(&span) {
+            return *i;
+        } else {
+            let id = self.spans.len();
+            self.spans.insert(span, stable_mir::ty::Span(id));
+            stable_mir::ty::Span(id)
         }
-        let id = self.spans.len();
-        self.spans.push(span);
-        stable_mir::ty::Span(id)
     }
 }
 
@@ -138,7 +136,7 @@ pub fn run(tcx: TyCtxt<'_>, f: impl FnOnce()) {
             tcx,
             def_ids: fx::FxIndexMap::default(),
             alloc_ids: fx::FxIndexMap::default(),
-            spans: vec![],
+            spans: fx::FxIndexMap::default(),
             types: vec![],
         },
         f,
diff --git a/compiler/rustc_smir/src/rustc_smir/mod.rs b/compiler/rustc_smir/src/rustc_smir/mod.rs
index 42688613a61..757cfa82d3c 100644
--- a/compiler/rustc_smir/src/rustc_smir/mod.rs
+++ b/compiler/rustc_smir/src/rustc_smir/mod.rs
@@ -197,7 +197,7 @@ pub struct Tables<'tcx> {
     pub tcx: TyCtxt<'tcx>,
     pub def_ids: FxIndexMap<DefId, stable_mir::DefId>,
     pub alloc_ids: FxIndexMap<AllocId, stable_mir::AllocId>,
-    pub spans: Vec<rustc_span::Span>,
+    pub spans: FxIndexMap<rustc_span::Span, Span>,
     pub types: Vec<MaybeStable<stable_mir::ty::TyKind, Ty<'tcx>>>,
 }