Skip to content

Commit 3484b5a

Browse files
committed
internal: Do not allocate unnecessarily when importing macros from parent modules
1 parent ff864fb commit 3484b5a

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
lines changed

crates/hir-def/src/item_scope.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,6 @@ impl ItemScope {
334334
)
335335
}
336336

337-
pub(crate) fn collect_legacy_macros(&self) -> FxHashMap<Name, SmallVec<[MacroId; 1]>> {
338-
self.legacy_macros.clone()
339-
}
340-
341337
/// Marks everything that is not a procedural macro as private to `this_module`.
342338
pub(crate) fn censor_non_proc_macros(&mut self, this_module: ModuleId) {
343339
self.types

crates/hir-def/src/nameres/collector.rs

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
//! `DefCollector::collect` contains the fixed-point iteration loop which
44
//! resolves imports and expands macros.
55
6-
use std::{iter, mem};
6+
use std::{cmp::Ordering, iter, mem};
77

88
use base_db::{CrateId, Dependency, Edition, FileId};
99
use cfg::{CfgExpr, CfgOptions};
@@ -1928,9 +1928,13 @@ impl ModCollector<'_, '_> {
19281928
let modules = &mut def_map.modules;
19291929
let res = modules.alloc(ModuleData::new(origin, vis));
19301930
modules[res].parent = Some(self.module_id);
1931-
for (name, mac) in modules[self.module_id].scope.collect_legacy_macros() {
1932-
for &mac in &mac {
1933-
modules[res].scope.define_legacy_macro(name.clone(), mac);
1931+
1932+
if let Some((target, source)) = Self::borrow_modules(modules.as_mut(), res, self.module_id)
1933+
{
1934+
for (name, macs) in source.scope.legacy_macros() {
1935+
for &mac in macs {
1936+
target.scope.define_legacy_macro(name.clone(), mac);
1937+
}
19341938
}
19351939
}
19361940
modules[self.module_id].children.insert(name.clone(), res);
@@ -2226,14 +2230,40 @@ impl ModCollector<'_, '_> {
22262230
}
22272231

22282232
fn import_all_legacy_macros(&mut self, module_id: LocalModuleId) {
2229-
let macros = self.def_collector.def_map[module_id].scope.collect_legacy_macros();
2230-
for (name, macs) in macros {
2233+
let Some((source, target)) = Self::borrow_modules(self.def_collector.def_map.modules.as_mut(), module_id, self.module_id) else {
2234+
return
2235+
};
2236+
2237+
for (name, macs) in source.scope.legacy_macros() {
22312238
macs.last().map(|&mac| {
2232-
self.def_collector.define_legacy_macro(self.module_id, name.clone(), mac)
2239+
target.scope.define_legacy_macro(name.clone(), mac);
22332240
});
22342241
}
22352242
}
22362243

2244+
/// Mutably borrow two modules at once, retu
2245+
fn borrow_modules(
2246+
modules: &mut [ModuleData],
2247+
a: LocalModuleId,
2248+
b: LocalModuleId,
2249+
) -> Option<(&mut ModuleData, &mut ModuleData)> {
2250+
let a = a.into_raw().into_u32() as usize;
2251+
let b = b.into_raw().into_u32() as usize;
2252+
2253+
let (a, b) = match a.cmp(&b) {
2254+
Ordering::Equal => return None,
2255+
Ordering::Less => {
2256+
let (prefix, b) = modules.split_at_mut(b);
2257+
(&mut prefix[a], &mut b[0])
2258+
}
2259+
Ordering::Greater => {
2260+
let (prefix, a) = modules.split_at_mut(a);
2261+
(&mut a[0], &mut prefix[b])
2262+
}
2263+
};
2264+
Some((a, b))
2265+
}
2266+
22372267
fn is_cfg_enabled(&self, cfg: &CfgExpr) -> bool {
22382268
self.def_collector.cfg_options.check(cfg) != Some(false)
22392269
}

lib/la-arena/src/lib.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,12 @@ impl<T> Arena<T> {
451451
}
452452
}
453453

454+
impl<T> AsMut<[T]> for Arena<T> {
455+
fn as_mut(&mut self) -> &mut [T] {
456+
self.data.as_mut()
457+
}
458+
}
459+
454460
impl<T> Default for Arena<T> {
455461
fn default() -> Arena<T> {
456462
Arena { data: Vec::new() }

0 commit comments

Comments
 (0)