about summary refs log tree commit diff
path: root/compiler/rustc_data_structures/src
diff options
context:
space:
mode:
authorCamille GILLOT <gillot.camille@gmail.com>2022-09-30 19:48:55 +0200
committerCamille GILLOT <gillot.camille@gmail.com>2022-10-01 16:24:30 +0200
commit9f2ab5b9adc4bac7048e5226e5a4580c31f7e533 (patch)
treeb21e76659005cf8412f9af9fb7faa54808aa0386 /compiler/rustc_data_structures/src
parent13608715d81928954412993e4baccf2779102058 (diff)
downloadrust-9f2ab5b9adc4bac7048e5226e5a4580c31f7e533.tar.gz
rust-9f2ab5b9adc4bac7048e5226e5a4580c31f7e533.zip
Use a SortedMap instead of a VecMap.
Diffstat (limited to 'compiler/rustc_data_structures/src')
-rw-r--r--compiler/rustc_data_structures/src/sorted_map.rs17
-rw-r--r--compiler/rustc_data_structures/src/vec_map.rs14
2 files changed, 17 insertions, 14 deletions
diff --git a/compiler/rustc_data_structures/src/sorted_map.rs b/compiler/rustc_data_structures/src/sorted_map.rs
index 937cb671573..fe257e10205 100644
--- a/compiler/rustc_data_structures/src/sorted_map.rs
+++ b/compiler/rustc_data_structures/src/sorted_map.rs
@@ -96,6 +96,23 @@ impl<K: Ord, V> SortedMap<K, V> {
         }
     }
 
+    /// Gets a mutable reference to the value in the entry, or insert a new one.
+    #[inline]
+    pub fn get_mut_or_insert_default(&mut self, key: K) -> &mut V
+    where
+        K: Eq,
+        V: Default,
+    {
+        let index = match self.lookup_index_for(&key) {
+            Ok(index) => index,
+            Err(index) => {
+                self.data.insert(index, (key, V::default()));
+                index
+            }
+        };
+        unsafe { &mut self.data.get_unchecked_mut(index).1 }
+    }
+
     #[inline]
     pub fn clear(&mut self) {
         self.data.clear();
diff --git a/compiler/rustc_data_structures/src/vec_map.rs b/compiler/rustc_data_structures/src/vec_map.rs
index f1493e5bdad..86be0bd8775 100644
--- a/compiler/rustc_data_structures/src/vec_map.rs
+++ b/compiler/rustc_data_structures/src/vec_map.rs
@@ -53,20 +53,6 @@ where
         self.0.iter_mut().find(|(key, _)| k == key.borrow()).map(|elem| &mut elem.1)
     }
 
-    /// Gets a mutable reference to the value in the entry, or insert a new one.
-    pub fn get_mut_or_insert_default(&mut self, k: K) -> &mut V
-    where
-        K: Eq,
-        V: Default,
-    {
-        let pos = self.0.iter().position(|(key, _)| &k == key).unwrap_or_else(|| {
-            let pos = self.0.len();
-            self.0.push((k, V::default()));
-            pos
-        });
-        &mut self.0[pos].1
-    }
-
     /// Returns the any value corresponding to the supplied predicate filter.
     ///
     /// The supplied predicate will be applied to each (key, value) pair and it will return a