Skip to content

Replace some often empty Vecs with boxed slices #14152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions crates/hir-def/src/item_tree/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -659,15 +659,16 @@ fn desugar_future_path(orig: TypeRef) -> Path {
let path = path![core::future::Future];
let mut generic_args: Vec<_> =
std::iter::repeat(None).take(path.segments().len() - 1).collect();
let mut last = GenericArgs::empty();
let binding = AssociatedTypeBinding {
name: name![Output],
args: None,
type_ref: Some(orig),
bounds: Vec::new(),
bounds: Box::default(),
};
last.bindings.push(binding);
generic_args.push(Some(Interned::new(last)));
generic_args.push(Some(Interned::new(GenericArgs {
bindings: Box::new([binding]),
..GenericArgs::empty()
})));

Path::from_known_path(path, generic_args)
}
Expand Down
10 changes: 5 additions & 5 deletions crates/hir-def/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,15 +49,15 @@ pub struct Path {
/// also includes bindings of associated types, like in `Iterator<Item = Foo>`.
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct GenericArgs {
pub args: Vec<GenericArg>,
pub args: Box<[GenericArg]>,
/// This specifies whether the args contain a Self type as the first
/// element. This is the case for path segments like `<T as Trait>`, where
/// `T` is actually a type parameter for the path `Trait` specifying the
/// Self type. Otherwise, when we have a path `Trait<X, Y>`, the Self type
/// is left out.
pub has_self_type: bool,
/// Associated type bindings like in `Iterator<Item = T>`.
pub bindings: Vec<AssociatedTypeBinding>,
pub bindings: Box<[AssociatedTypeBinding]>,
/// Whether these generic args were desugared from `Trait(Arg) -> Output`
/// parenthesis notation typically used for the `Fn` traits.
pub desugared_from_fn: bool,
Expand All @@ -77,7 +77,7 @@ pub struct AssociatedTypeBinding {
/// Bounds for the associated type, like in `Iterator<Item:
/// SomeOtherTrait>`. (This is the unstable `associated_type_bounds`
/// feature.)
pub bounds: Vec<Interned<TypeBound>>,
pub bounds: Box<[Interned<TypeBound>]>,
}

/// A single generic argument.
Expand Down Expand Up @@ -212,9 +212,9 @@ impl GenericArgs {

pub(crate) fn empty() -> GenericArgs {
GenericArgs {
args: Vec::new(),
args: Box::default(),
has_self_type: false,
bindings: Vec::new(),
bindings: Box::default(),
desugared_from_fn: false,
}
}
Expand Down
57 changes: 36 additions & 21 deletions crates/hir-def/src/path/lower.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//! Transforms syntax into `Path` objects, ideally with accounting for hygiene

use std::iter;

use crate::type_ref::ConstScalarOrPath;

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

let self_type = GenericArg::Type(self_type);

// Insert the type reference (T in the above example) as Self parameter for the trait
let last_segment = generic_args.get_mut(segments.len() - num_segments)?;
let mut args_inner = match last_segment {
Some(it) => it.as_ref().clone(),
None => GenericArgs::empty(),
};
args_inner.has_self_type = true;
args_inner.args.insert(0, GenericArg::Type(self_type));
*last_segment = Some(Interned::new(args_inner));
*last_segment = Some(Interned::new(match last_segment.take() {
Some(it) => GenericArgs {
args: iter::once(self_type)
.chain(it.args.iter().cloned())
.collect(),

has_self_type: true,
bindings: it.bindings.clone(),
desugared_from_fn: it.desugared_from_fn,
},
None => GenericArgs {
args: Box::new([self_type]),
has_self_type: true,
..GenericArgs::empty()
},
}));
}
}
}
Expand Down Expand Up @@ -187,7 +200,7 @@ pub(super) fn lower_generic_args(
.map(|it| Interned::new(TypeBound::from_ast(lower_ctx, it)))
.collect()
} else {
Vec::new()
Box::default()
};
bindings.push(AssociatedTypeBinding { name, args, type_ref, bounds });
}
Expand All @@ -208,7 +221,12 @@ pub(super) fn lower_generic_args(
if args.is_empty() && bindings.is_empty() {
return None;
}
Some(GenericArgs { args, has_self_type: false, bindings, desugared_from_fn: false })
Some(GenericArgs {
args: args.into_boxed_slice(),
has_self_type: false,
bindings: bindings.into_boxed_slice(),
desugared_from_fn: false,
})
}

/// Collect `GenericArgs` from the parts of a fn-like path, i.e. `Fn(X, Y)
Expand All @@ -218,33 +236,30 @@ fn lower_generic_args_from_fn_path(
params: Option<ast::ParamList>,
ret_type: Option<ast::RetType>,
) -> Option<GenericArgs> {
let mut args = Vec::new();
let mut bindings = Vec::new();
let params = params?;
let mut param_types = Vec::new();
for param in params.params() {
let type_ref = TypeRef::from_ast_opt(ctx, param.ty());
param_types.push(type_ref);
}
let arg = GenericArg::Type(TypeRef::Tuple(param_types));
args.push(arg);
if let Some(ret_type) = ret_type {
let args = Box::new([GenericArg::Type(TypeRef::Tuple(param_types))]);
let bindings = if let Some(ret_type) = ret_type {
let type_ref = TypeRef::from_ast_opt(ctx, ret_type.ty());
bindings.push(AssociatedTypeBinding {
Box::new([AssociatedTypeBinding {
name: name![Output],
args: None,
type_ref: Some(type_ref),
bounds: Vec::new(),
});
bounds: Box::default(),
}])
} else {
// -> ()
let type_ref = TypeRef::Tuple(Vec::new());
bindings.push(AssociatedTypeBinding {
Box::new([AssociatedTypeBinding {
name: name![Output],
args: None,
type_ref: Some(type_ref),
bounds: Vec::new(),
});
}
bounds: Box::default(),
}])
};
Some(GenericArgs { args, has_self_type: false, bindings, desugared_from_fn: true })
}
2 changes: 1 addition & 1 deletion crates/hir-def/src/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ pub(crate) fn print_generic_args(generics: &GenericArgs, buf: &mut dyn Write) ->
first = false;
print_generic_arg(arg, buf)?;
}
for binding in &generics.bindings {
for binding in generics.bindings.iter() {
if !first {
write!(buf, ", ")?;
}
Expand Down
6 changes: 3 additions & 3 deletions crates/hir-def/src/type_ref.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ impl TypeRef {
}
for segment in path.segments().iter() {
if let Some(args_and_bindings) = segment.args_and_bindings {
for arg in &args_and_bindings.args {
for arg in args_and_bindings.args.iter() {
match arg {
crate::path::GenericArg::Type(type_ref) => {
go(type_ref, f);
Expand All @@ -301,11 +301,11 @@ impl TypeRef {
| crate::path::GenericArg::Lifetime(_) => {}
}
}
for binding in &args_and_bindings.bindings {
for binding in args_and_bindings.bindings.iter() {
if let Some(type_ref) = &binding.type_ref {
go(type_ref, f);
}
for bound in &binding.bounds {
for bound in binding.bounds.iter() {
match bound.as_ref() {
TypeBound::Path(path, _) | TypeBound::ForLifetime(_, path) => {
go_path(path, f)
Expand Down
6 changes: 3 additions & 3 deletions crates/hir-ty/src/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1419,7 +1419,7 @@ impl HirDisplay for Path {

write!(f, "<")?;
let mut first = true;
for arg in &generic_args.args {
for arg in generic_args.args.iter() {
if first {
first = false;
if generic_args.has_self_type {
Expand All @@ -1431,7 +1431,7 @@ impl HirDisplay for Path {
}
arg.hir_fmt(f)?;
}
for binding in &generic_args.bindings {
for binding in generic_args.bindings.iter() {
if first {
first = false;
} else {
Expand All @@ -1445,7 +1445,7 @@ impl HirDisplay for Path {
}
None => {
write!(f, ": ")?;
f.write_joined(&binding.bounds, " + ")?;
f.write_joined(binding.bounds.iter(), " + ")?;
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions crates/hir-ty/src/lower.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,7 +1025,7 @@ impl<'a> TyLoweringContext<'a> {
last_segment
.into_iter()
.filter_map(|segment| segment.args_and_bindings)
.flat_map(|args_and_bindings| &args_and_bindings.bindings)
.flat_map(|args_and_bindings| args_and_bindings.bindings.iter())
.flat_map(move |binding| {
let found = associated_type_by_name_including_super_traits(
self.db,
Expand Down Expand Up @@ -1068,7 +1068,7 @@ impl<'a> TyLoweringContext<'a> {
AliasEq { alias: AliasTy::Projection(projection_ty.clone()), ty };
preds.push(crate::wrap_empty_binders(WhereClause::AliasEq(alias_eq)));
}
for bound in &binding.bounds {
for bound in binding.bounds.iter() {
preds.extend(self.lower_type_bound(
bound,
TyKind::Alias(AliasTy::Projection(projection_ty.clone())).intern(Interner),
Expand Down