Replace with immutable map to avoid heavy cloning

This commit is contained in:
uHOOCCOOHu 2019-09-07 22:46:44 +08:00
parent 26b092bd3b
commit c90256429b
No known key found for this signature in database
GPG key ID: CED392DE0C483D00
4 changed files with 36 additions and 2 deletions

View file

@ -54,6 +54,7 @@ mod mod_resolution;
#[cfg(test)]
mod tests;
use std::hash::BuildHasherDefault;
use std::sync::Arc;
use once_cell::sync::Lazy;
@ -61,7 +62,7 @@ use ra_arena::{impl_arena_id, Arena, RawId};
use ra_db::{Edition, FileId};
use ra_prof::profile;
use ra_syntax::ast;
use rustc_hash::{FxHashMap, FxHashSet};
use rustc_hash::{FxHashMap, FxHashSet, FxHasher};
use test_utils::tested_by;
use crate::{
@ -73,6 +74,8 @@ use crate::{
AstId, BuiltinType, Crate, HirFileId, MacroDef, Module, ModuleDef, Name, Path, PathKind, Trait,
};
pub(crate) type ImmFxHashMap<K, V> = im::HashMap<K, V, BuildHasherDefault<FxHasher>>;
pub(crate) use self::raw::{ImportSourceMap, RawItems};
pub use self::{
@ -139,7 +142,7 @@ pub(crate) struct ModuleData {
pub struct ModuleScope {
items: FxHashMap<Name, Resolution>,
macros: FxHashMap<Name, MacroDef>,
textual_macros: FxHashMap<Name, MacroDef>,
textual_macros: ImmFxHashMap<Name, MacroDef>,
}
static BUILTIN_SCOPE: Lazy<FxHashMap<Name, Resolution>> = Lazy::new(|| {

View file

@ -631,6 +631,7 @@ where
modules[res].parent = Some(self.module_id);
modules[res].declaration = Some(declaration);
modules[res].definition = definition;
// Cloning immutable map is lazy and fast
modules[res].scope.textual_macros = modules[self.module_id].scope.textual_macros.clone();
modules[self.module_id].children.insert(name.clone(), res);
let resolution = Resolution {
@ -707,6 +708,8 @@ where
}
fn import_all_textual_macros(&mut self, module_id: CrateModuleId) {
// `clone()` is needed here to avoid mutable borrow `self.def_collector` when first borrow is alive
// Cloning immutable map is lazy and fast
let macros = self.def_collector.def_map[module_id].scope.textual_macros.clone();
for (name, macro_) in macros {
self.def_collector.define_textual_macro(self.module_id, name.clone(), macro_.id);