internal: Do not allocate unnecessarily when importing macros from parent modules

This commit is contained in:
Lukas Wirth 2023-06-16 18:41:06 +02:00
parent ff864fbb9f
commit 3484b5a116
3 changed files with 43 additions and 11 deletions

View file

@ -334,10 +334,6 @@ impl ItemScope {
) )
} }
pub(crate) fn collect_legacy_macros(&self) -> FxHashMap<Name, SmallVec<[MacroId; 1]>> {
self.legacy_macros.clone()
}
/// Marks everything that is not a procedural macro as private to `this_module`. /// Marks everything that is not a procedural macro as private to `this_module`.
pub(crate) fn censor_non_proc_macros(&mut self, this_module: ModuleId) { pub(crate) fn censor_non_proc_macros(&mut self, this_module: ModuleId) {
self.types self.types

View file

@ -3,7 +3,7 @@
//! `DefCollector::collect` contains the fixed-point iteration loop which //! `DefCollector::collect` contains the fixed-point iteration loop which
//! resolves imports and expands macros. //! resolves imports and expands macros.
use std::{iter, mem}; use std::{cmp::Ordering, iter, mem};
use base_db::{CrateId, Dependency, Edition, FileId}; use base_db::{CrateId, Dependency, Edition, FileId};
use cfg::{CfgExpr, CfgOptions}; use cfg::{CfgExpr, CfgOptions};
@ -1928,9 +1928,13 @@ impl ModCollector<'_, '_> {
let modules = &mut def_map.modules; let modules = &mut def_map.modules;
let res = modules.alloc(ModuleData::new(origin, vis)); let res = modules.alloc(ModuleData::new(origin, vis));
modules[res].parent = Some(self.module_id); modules[res].parent = Some(self.module_id);
for (name, mac) in modules[self.module_id].scope.collect_legacy_macros() {
for &mac in &mac { if let Some((target, source)) = Self::borrow_modules(modules.as_mut(), res, self.module_id)
modules[res].scope.define_legacy_macro(name.clone(), mac); {
for (name, macs) in source.scope.legacy_macros() {
for &mac in macs {
target.scope.define_legacy_macro(name.clone(), mac);
}
} }
} }
modules[self.module_id].children.insert(name.clone(), res); modules[self.module_id].children.insert(name.clone(), res);
@ -2226,14 +2230,40 @@ impl ModCollector<'_, '_> {
} }
fn import_all_legacy_macros(&mut self, module_id: LocalModuleId) { fn import_all_legacy_macros(&mut self, module_id: LocalModuleId) {
let macros = self.def_collector.def_map[module_id].scope.collect_legacy_macros(); let Some((source, target)) = Self::borrow_modules(self.def_collector.def_map.modules.as_mut(), module_id, self.module_id) else {
for (name, macs) in macros { return
};
for (name, macs) in source.scope.legacy_macros() {
macs.last().map(|&mac| { macs.last().map(|&mac| {
self.def_collector.define_legacy_macro(self.module_id, name.clone(), mac) target.scope.define_legacy_macro(name.clone(), mac);
}); });
} }
} }
/// Mutably borrow two modules at once, retu
fn borrow_modules(
modules: &mut [ModuleData],
a: LocalModuleId,
b: LocalModuleId,
) -> Option<(&mut ModuleData, &mut ModuleData)> {
let a = a.into_raw().into_u32() as usize;
let b = b.into_raw().into_u32() as usize;
let (a, b) = match a.cmp(&b) {
Ordering::Equal => return None,
Ordering::Less => {
let (prefix, b) = modules.split_at_mut(b);
(&mut prefix[a], &mut b[0])
}
Ordering::Greater => {
let (prefix, a) = modules.split_at_mut(a);
(&mut a[0], &mut prefix[b])
}
};
Some((a, b))
}
fn is_cfg_enabled(&self, cfg: &CfgExpr) -> bool { fn is_cfg_enabled(&self, cfg: &CfgExpr) -> bool {
self.def_collector.cfg_options.check(cfg) != Some(false) self.def_collector.cfg_options.check(cfg) != Some(false)
} }

View file

@ -451,6 +451,12 @@ impl<T> Arena<T> {
} }
} }
impl<T> AsMut<[T]> for Arena<T> {
fn as_mut(&mut self) -> &mut [T] {
self.data.as_mut()
}
}
impl<T> Default for Arena<T> { impl<T> Default for Arena<T> {
fn default() -> Arena<T> { fn default() -> Arena<T> {
Arena { data: Vec::new() } Arena { data: Vec::new() }