Skip to content

Commit 37608f3

Browse files
committed
Auto merge of #14152 - Veykril:path-opt, r=Veykril
Replace some often empty `Vec`s with boxed slices
2 parents 523fea8 + 4aee911 commit 37608f3

File tree

7 files changed

+55
-39
lines changed

7 files changed

+55
-39
lines changed

crates/hir-def/src/item_tree/lower.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -659,15 +659,16 @@ fn desugar_future_path(orig: TypeRef) -> Path {
659659
let path = path![core::future::Future];
660660
let mut generic_args: Vec<_> =
661661
std::iter::repeat(None).take(path.segments().len() - 1).collect();
662-
let mut last = GenericArgs::empty();
663662
let binding = AssociatedTypeBinding {
664663
name: name![Output],
665664
args: None,
666665
type_ref: Some(orig),
667-
bounds: Vec::new(),
666+
bounds: Box::default(),
668667
};
669-
last.bindings.push(binding);
670-
generic_args.push(Some(Interned::new(last)));
668+
generic_args.push(Some(Interned::new(GenericArgs {
669+
bindings: Box::new([binding]),
670+
..GenericArgs::empty()
671+
})));
671672

672673
Path::from_known_path(path, generic_args)
673674
}

crates/hir-def/src/path.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,15 +49,15 @@ pub struct Path {
4949
/// also includes bindings of associated types, like in `Iterator<Item = Foo>`.
5050
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
5151
pub struct GenericArgs {
52-
pub args: Vec<GenericArg>,
52+
pub args: Box<[GenericArg]>,
5353
/// This specifies whether the args contain a Self type as the first
5454
/// element. This is the case for path segments like `<T as Trait>`, where
5555
/// `T` is actually a type parameter for the path `Trait` specifying the
5656
/// Self type. Otherwise, when we have a path `Trait<X, Y>`, the Self type
5757
/// is left out.
5858
pub has_self_type: bool,
5959
/// Associated type bindings like in `Iterator<Item = T>`.
60-
pub bindings: Vec<AssociatedTypeBinding>,
60+
pub bindings: Box<[AssociatedTypeBinding]>,
6161
/// Whether these generic args were desugared from `Trait(Arg) -> Output`
6262
/// parenthesis notation typically used for the `Fn` traits.
6363
pub desugared_from_fn: bool,
@@ -77,7 +77,7 @@ pub struct AssociatedTypeBinding {
7777
/// Bounds for the associated type, like in `Iterator<Item:
7878
/// SomeOtherTrait>`. (This is the unstable `associated_type_bounds`
7979
/// feature.)
80-
pub bounds: Vec<Interned<TypeBound>>,
80+
pub bounds: Box<[Interned<TypeBound>]>,
8181
}
8282

8383
/// A single generic argument.
@@ -212,9 +212,9 @@ impl GenericArgs {
212212

213213
pub(crate) fn empty() -> GenericArgs {
214214
GenericArgs {
215-
args: Vec::new(),
215+
args: Box::default(),
216216
has_self_type: false,
217-
bindings: Vec::new(),
217+
bindings: Box::default(),
218218
desugared_from_fn: false,
219219
}
220220
}

crates/hir-def/src/path/lower.rs

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Transforms syntax into `Path` objects, ideally with accounting for hygiene
22
3+
use std::iter;
4+
35
use crate::type_ref::ConstScalarOrPath;
46

57
use either::Either;
@@ -86,15 +88,26 @@ pub(super) fn lower_path(mut path: ast::Path, ctx: &LowerCtx<'_>) -> Option<Path
8688
generic_args.resize(segments.len(), None);
8789
}
8890

91+
let self_type = GenericArg::Type(self_type);
92+
8993
// Insert the type reference (T in the above example) as Self parameter for the trait
9094
let last_segment = generic_args.get_mut(segments.len() - num_segments)?;
91-
let mut args_inner = match last_segment {
92-
Some(it) => it.as_ref().clone(),
93-
None => GenericArgs::empty(),
94-
};
95-
args_inner.has_self_type = true;
96-
args_inner.args.insert(0, GenericArg::Type(self_type));
97-
*last_segment = Some(Interned::new(args_inner));
95+
*last_segment = Some(Interned::new(match last_segment.take() {
96+
Some(it) => GenericArgs {
97+
args: iter::once(self_type)
98+
.chain(it.args.iter().cloned())
99+
.collect(),
100+
101+
has_self_type: true,
102+
bindings: it.bindings.clone(),
103+
desugared_from_fn: it.desugared_from_fn,
104+
},
105+
None => GenericArgs {
106+
args: Box::new([self_type]),
107+
has_self_type: true,
108+
..GenericArgs::empty()
109+
},
110+
}));
98111
}
99112
}
100113
}
@@ -187,7 +200,7 @@ pub(super) fn lower_generic_args(
187200
.map(|it| Interned::new(TypeBound::from_ast(lower_ctx, it)))
188201
.collect()
189202
} else {
190-
Vec::new()
203+
Box::default()
191204
};
192205
bindings.push(AssociatedTypeBinding { name, args, type_ref, bounds });
193206
}
@@ -208,7 +221,12 @@ pub(super) fn lower_generic_args(
208221
if args.is_empty() && bindings.is_empty() {
209222
return None;
210223
}
211-
Some(GenericArgs { args, has_self_type: false, bindings, desugared_from_fn: false })
224+
Some(GenericArgs {
225+
args: args.into_boxed_slice(),
226+
has_self_type: false,
227+
bindings: bindings.into_boxed_slice(),
228+
desugared_from_fn: false,
229+
})
212230
}
213231

214232
/// Collect `GenericArgs` from the parts of a fn-like path, i.e. `Fn(X, Y)
@@ -218,33 +236,30 @@ fn lower_generic_args_from_fn_path(
218236
params: Option<ast::ParamList>,
219237
ret_type: Option<ast::RetType>,
220238
) -> Option<GenericArgs> {
221-
let mut args = Vec::new();
222-
let mut bindings = Vec::new();
223239
let params = params?;
224240
let mut param_types = Vec::new();
225241
for param in params.params() {
226242
let type_ref = TypeRef::from_ast_opt(ctx, param.ty());
227243
param_types.push(type_ref);
228244
}
229-
let arg = GenericArg::Type(TypeRef::Tuple(param_types));
230-
args.push(arg);
231-
if let Some(ret_type) = ret_type {
245+
let args = Box::new([GenericArg::Type(TypeRef::Tuple(param_types))]);
246+
let bindings = if let Some(ret_type) = ret_type {
232247
let type_ref = TypeRef::from_ast_opt(ctx, ret_type.ty());
233-
bindings.push(AssociatedTypeBinding {
248+
Box::new([AssociatedTypeBinding {
234249
name: name![Output],
235250
args: None,
236251
type_ref: Some(type_ref),
237-
bounds: Vec::new(),
238-
});
252+
bounds: Box::default(),
253+
}])
239254
} else {
240255
// -> ()
241256
let type_ref = TypeRef::Tuple(Vec::new());
242-
bindings.push(AssociatedTypeBinding {
257+
Box::new([AssociatedTypeBinding {
243258
name: name![Output],
244259
args: None,
245260
type_ref: Some(type_ref),
246-
bounds: Vec::new(),
247-
});
248-
}
261+
bounds: Box::default(),
262+
}])
263+
};
249264
Some(GenericArgs { args, has_self_type: false, bindings, desugared_from_fn: true })
250265
}

crates/hir-def/src/pretty.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub(crate) fn print_generic_args(generics: &GenericArgs, buf: &mut dyn Write) ->
7171
first = false;
7272
print_generic_arg(arg, buf)?;
7373
}
74-
for binding in &generics.bindings {
74+
for binding in generics.bindings.iter() {
7575
if !first {
7676
write!(buf, ", ")?;
7777
}

crates/hir-def/src/type_ref.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ impl TypeRef {
292292
}
293293
for segment in path.segments().iter() {
294294
if let Some(args_and_bindings) = segment.args_and_bindings {
295-
for arg in &args_and_bindings.args {
295+
for arg in args_and_bindings.args.iter() {
296296
match arg {
297297
crate::path::GenericArg::Type(type_ref) => {
298298
go(type_ref, f);
@@ -301,11 +301,11 @@ impl TypeRef {
301301
| crate::path::GenericArg::Lifetime(_) => {}
302302
}
303303
}
304-
for binding in &args_and_bindings.bindings {
304+
for binding in args_and_bindings.bindings.iter() {
305305
if let Some(type_ref) = &binding.type_ref {
306306
go(type_ref, f);
307307
}
308-
for bound in &binding.bounds {
308+
for bound in binding.bounds.iter() {
309309
match bound.as_ref() {
310310
TypeBound::Path(path, _) | TypeBound::ForLifetime(_, path) => {
311311
go_path(path, f)

crates/hir-ty/src/display.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1419,7 +1419,7 @@ impl HirDisplay for Path {
14191419

14201420
write!(f, "<")?;
14211421
let mut first = true;
1422-
for arg in &generic_args.args {
1422+
for arg in generic_args.args.iter() {
14231423
if first {
14241424
first = false;
14251425
if generic_args.has_self_type {
@@ -1431,7 +1431,7 @@ impl HirDisplay for Path {
14311431
}
14321432
arg.hir_fmt(f)?;
14331433
}
1434-
for binding in &generic_args.bindings {
1434+
for binding in generic_args.bindings.iter() {
14351435
if first {
14361436
first = false;
14371437
} else {
@@ -1445,7 +1445,7 @@ impl HirDisplay for Path {
14451445
}
14461446
None => {
14471447
write!(f, ": ")?;
1448-
f.write_joined(&binding.bounds, " + ")?;
1448+
f.write_joined(binding.bounds.iter(), " + ")?;
14491449
}
14501450
}
14511451
}

crates/hir-ty/src/lower.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1025,7 +1025,7 @@ impl<'a> TyLoweringContext<'a> {
10251025
last_segment
10261026
.into_iter()
10271027
.filter_map(|segment| segment.args_and_bindings)
1028-
.flat_map(|args_and_bindings| &args_and_bindings.bindings)
1028+
.flat_map(|args_and_bindings| args_and_bindings.bindings.iter())
10291029
.flat_map(move |binding| {
10301030
let found = associated_type_by_name_including_super_traits(
10311031
self.db,
@@ -1068,7 +1068,7 @@ impl<'a> TyLoweringContext<'a> {
10681068
AliasEq { alias: AliasTy::Projection(projection_ty.clone()), ty };
10691069
preds.push(crate::wrap_empty_binders(WhereClause::AliasEq(alias_eq)));
10701070
}
1071-
for bound in &binding.bounds {
1071+
for bound in binding.bounds.iter() {
10721072
preds.extend(self.lower_type_bound(
10731073
bound,
10741074
TyKind::Alias(AliasTy::Projection(projection_ty.clone())).intern(Interner),

0 commit comments

Comments
 (0)