From d08669c4fa51a864a46438c2e4bc9047931e1033 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Thu, 29 Sep 2022 21:07:06 +0200 Subject: Compute by owner instead of HirId. --- compiler/rustc_data_structures/src/vec_map.rs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'compiler/rustc_data_structures/src') diff --git a/compiler/rustc_data_structures/src/vec_map.rs b/compiler/rustc_data_structures/src/vec_map.rs index 86be0bd8775..f1493e5bdad 100644 --- a/compiler/rustc_data_structures/src/vec_map.rs +++ b/compiler/rustc_data_structures/src/vec_map.rs @@ -53,6 +53,20 @@ 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 -- cgit 1.4.1-3-g733a5 From 9f2ab5b9adc4bac7048e5226e5a4580c31f7e533 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 30 Sep 2022 19:48:55 +0200 Subject: Use a SortedMap instead of a VecMap. --- compiler/rustc_data_structures/src/sorted_map.rs | 17 +++++++++++++++++ compiler/rustc_data_structures/src/vec_map.rs | 14 -------------- compiler/rustc_middle/src/lint.rs | 4 ++-- 3 files changed, 19 insertions(+), 16 deletions(-) (limited to 'compiler/rustc_data_structures/src') 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 SortedMap { } } + /// 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 diff --git a/compiler/rustc_middle/src/lint.rs b/compiler/rustc_middle/src/lint.rs index d65fa30450c..f3e4f1faeb0 100644 --- a/compiler/rustc_middle/src/lint.rs +++ b/compiler/rustc_middle/src/lint.rs @@ -1,7 +1,7 @@ use std::cmp; use rustc_data_structures::fx::FxHashMap; -use rustc_data_structures::vec_map::VecMap; +use rustc_data_structures::sorted_map::SortedMap; use rustc_errors::{Diagnostic, DiagnosticBuilder, DiagnosticId, DiagnosticMessage, MultiSpan}; use rustc_hir::{HirId, ItemLocalId}; use rustc_session::lint::{ @@ -63,7 +63,7 @@ pub type LevelAndSource = (Level, LintLevelSource); /// by the attributes for *a single HirId*. #[derive(Default, Debug, HashStable)] pub struct ShallowLintLevelMap { - pub specs: VecMap>, + pub specs: SortedMap>, } /// From an initial level and source, verify the effect of special annotations: -- cgit 1.4.1-3-g733a5