Skip to content

Commit 9f2ab5b

Browse files
committed
Use a SortedMap instead of a VecMap.
1 parent 1360871 commit 9f2ab5b

File tree

3 files changed

+19
-16
lines changed

3 files changed

+19
-16
lines changed

compiler/rustc_data_structures/src/sorted_map.rs

+17
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,23 @@ impl<K: Ord, V> SortedMap<K, V> {
9696
}
9797
}
9898

99+
/// Gets a mutable reference to the value in the entry, or insert a new one.
100+
#[inline]
101+
pub fn get_mut_or_insert_default(&mut self, key: K) -> &mut V
102+
where
103+
K: Eq,
104+
V: Default,
105+
{
106+
let index = match self.lookup_index_for(&key) {
107+
Ok(index) => index,
108+
Err(index) => {
109+
self.data.insert(index, (key, V::default()));
110+
index
111+
}
112+
};
113+
unsafe { &mut self.data.get_unchecked_mut(index).1 }
114+
}
115+
99116
#[inline]
100117
pub fn clear(&mut self) {
101118
self.data.clear();

compiler/rustc_data_structures/src/vec_map.rs

-14
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,6 @@ where
5353
self.0.iter_mut().find(|(key, _)| k == key.borrow()).map(|elem| &mut elem.1)
5454
}
5555

56-
/// Gets a mutable reference to the value in the entry, or insert a new one.
57-
pub fn get_mut_or_insert_default(&mut self, k: K) -> &mut V
58-
where
59-
K: Eq,
60-
V: Default,
61-
{
62-
let pos = self.0.iter().position(|(key, _)| &k == key).unwrap_or_else(|| {
63-
let pos = self.0.len();
64-
self.0.push((k, V::default()));
65-
pos
66-
});
67-
&mut self.0[pos].1
68-
}
69-
7056
/// Returns the any value corresponding to the supplied predicate filter.
7157
///
7258
/// The supplied predicate will be applied to each (key, value) pair and it will return a

compiler/rustc_middle/src/lint.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::cmp;
22

33
use rustc_data_structures::fx::FxHashMap;
4-
use rustc_data_structures::vec_map::VecMap;
4+
use rustc_data_structures::sorted_map::SortedMap;
55
use rustc_errors::{Diagnostic, DiagnosticBuilder, DiagnosticId, DiagnosticMessage, MultiSpan};
66
use rustc_hir::{HirId, ItemLocalId};
77
use rustc_session::lint::{
@@ -63,7 +63,7 @@ pub type LevelAndSource = (Level, LintLevelSource);
6363
/// by the attributes for *a single HirId*.
6464
#[derive(Default, Debug, HashStable)]
6565
pub struct ShallowLintLevelMap {
66-
pub specs: VecMap<ItemLocalId, FxHashMap<LintId, LevelAndSource>>,
66+
pub specs: SortedMap<ItemLocalId, FxHashMap<LintId, LevelAndSource>>,
6767
}
6868

6969
/// From an initial level and source, verify the effect of special annotations:

0 commit comments

Comments
 (0)