Skip to content

Commit 4143890

Browse files
committed
Auto merge of rust-lang#15065 - Veykril:remove-alloc, r=Veykril
internal: Do not allocate unnecessarily when importing macros from parent modules
2 parents 0cad484 + bd093d1 commit 4143890

File tree

4 files changed

+54
-25
lines changed

4 files changed

+54
-25
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
}

crates/ide-assists/src/handlers/generate_delegate_methods.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -72,39 +72,36 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
7272
let krate = ty.krate(ctx.db());
7373
ty.iterate_assoc_items(ctx.db(), krate, |item| {
7474
if let hir::AssocItem::Function(f) = item {
75+
let name = f.name(ctx.db());
7576
if f.self_param(ctx.db()).is_some()
7677
&& f.is_visible_from(ctx.db(), current_module)
77-
&& seen_names.insert(f.name(ctx.db()))
78+
&& seen_names.insert(name.clone())
7879
{
79-
methods.push(f)
80+
methods.push((name, f))
8081
}
8182
}
8283
Option::<()>::None
8384
});
8485
}
85-
86-
for method in methods {
86+
methods.sort_by(|(a, _), (b, _)| a.cmp(b));
87+
for (name, method) in methods {
8788
let adt = ast::Adt::Struct(strukt.clone());
88-
let name = method.name(ctx.db()).display(ctx.db()).to_string();
89+
let name = name.display(ctx.db()).to_string();
8990
// if `find_struct_impl` returns None, that means that a function named `name` already exists.
90-
let Some(impl_def) = find_struct_impl(ctx, &adt, &[name]) else { continue; };
91+
let Some(impl_def) = find_struct_impl(ctx, &adt, std::slice::from_ref(&name)) else { continue; };
9192
acc.add_group(
9293
&GroupLabel("Generate delegate methods…".to_owned()),
9394
AssistId("generate_delegate_methods", AssistKind::Generate),
94-
format!(
95-
"Generate delegate for `{field_name}.{}()`",
96-
method.name(ctx.db()).display(ctx.db())
97-
),
95+
format!("Generate delegate for `{field_name}.{name}()`",),
9896
target,
9997
|builder| {
10098
// Create the function
10199
let method_source = match method.source(ctx.db()) {
102100
Some(source) => source.value,
103101
None => return,
104102
};
105-
let method_name = method.name(ctx.db());
106103
let vis = method_source.visibility();
107-
let name = make::name(&method.name(ctx.db()).display(ctx.db()).to_string());
104+
let fn_name = make::name(&name);
108105
let params =
109106
method_source.param_list().unwrap_or_else(|| make::param_list(None, []));
110107
let type_params = method_source.generic_param_list();
@@ -114,7 +111,7 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
114111
};
115112
let tail_expr = make::expr_method_call(
116113
make::ext::field_from_idents(["self", &field_name]).unwrap(), // This unwrap is ok because we have at least 1 arg in the list
117-
make::name_ref(&method_name.display(ctx.db()).to_string()),
114+
make::name_ref(&name),
118115
arg_list,
119116
);
120117
let ret_type = method_source.ret_type();
@@ -126,7 +123,7 @@ pub(crate) fn generate_delegate_methods(acc: &mut Assists, ctx: &AssistContext<'
126123
let body = make::block_expr([], Some(tail_expr_finished));
127124
let f = make::fn_(
128125
vis,
129-
name,
126+
fn_name,
130127
type_params,
131128
None,
132129
params,

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)