Skip to content

Commit f2707bc

Browse files
bors[bot]Veykril
andauthored
Merge #10819
10819: internal: Replace some `Vec` occurences with `Box` r=Veykril a=Veykril Shaves off ~15mb from self analysis Co-authored-by: Lukas Wirth <[email protected]>
2 parents 5f5c84d + 91def93 commit f2707bc

File tree

8 files changed

+78
-100
lines changed

8 files changed

+78
-100
lines changed

crates/hir_def/src/body/lower.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -130,11 +130,7 @@ impl ExprCollector<'_> {
130130
self.body.params.push(param_pat);
131131
}
132132

133-
for param in param_list.params() {
134-
let pat = match param.pat() {
135-
None => continue,
136-
Some(pat) => pat,
137-
};
133+
for pat in param_list.params().filter_map(|param| param.pat()) {
138134
let param_pat = self.collect_pat(pat);
139135
self.body.params.push(param_pat);
140136
}
@@ -160,7 +156,7 @@ impl ExprCollector<'_> {
160156
self.make_expr(expr, Err(SyntheticSyntax))
161157
}
162158
fn unit(&mut self) -> ExprId {
163-
self.alloc_expr_desugared(Expr::Tuple { exprs: Vec::new() })
159+
self.alloc_expr_desugared(Expr::Tuple { exprs: Box::default() })
164160
}
165161
fn missing_expr(&mut self) -> ExprId {
166162
self.alloc_expr_desugared(Expr::Missing)
@@ -235,7 +231,8 @@ impl ExprCollector<'_> {
235231
expr: else_branch.unwrap_or_else(|| self.unit()),
236232
guard: None,
237233
},
238-
];
234+
]
235+
.into();
239236
return Some(
240237
self.alloc_expr(Expr::Match { expr: match_expr, arms }, syntax_ptr),
241238
);
@@ -300,7 +297,8 @@ impl ExprCollector<'_> {
300297
let arms = vec![
301298
MatchArm { pat, expr: body, guard: None },
302299
MatchArm { pat: placeholder_pat, expr: break_, guard: None },
303-
];
300+
]
301+
.into();
304302
let match_expr =
305303
self.alloc_expr_desugared(Expr::Match { expr: match_expr, arms });
306304
return Some(
@@ -324,7 +322,7 @@ impl ExprCollector<'_> {
324322
let args = if let Some(arg_list) = e.arg_list() {
325323
arg_list.args().filter_map(|e| self.maybe_collect_expr(e)).collect()
326324
} else {
327-
Vec::new()
325+
Box::default()
328326
};
329327
self.alloc_expr(Expr::Call { callee, args }, syntax_ptr)
330328
}
@@ -333,7 +331,7 @@ impl ExprCollector<'_> {
333331
let args = if let Some(arg_list) = e.arg_list() {
334332
arg_list.args().filter_map(|e| self.maybe_collect_expr(e)).collect()
335333
} else {
336-
Vec::new()
334+
Box::default()
337335
};
338336
let method_name = e.name_ref().map(|nr| nr.as_name()).unwrap_or_else(Name::missing);
339337
let generic_args = e
@@ -367,7 +365,7 @@ impl ExprCollector<'_> {
367365
})
368366
.collect()
369367
} else {
370-
Vec::new()
368+
Box::default()
371369
};
372370
self.alloc_expr(Expr::Match { expr, arms }, syntax_ptr)
373371
}
@@ -429,7 +427,7 @@ impl ExprCollector<'_> {
429427
let spread = nfl.spread().map(|s| self.collect_expr(s));
430428
Expr::RecordLit { path, fields, spread }
431429
} else {
432-
Expr::RecordLit { path, fields: Vec::new(), spread: None }
430+
Expr::RecordLit { path, fields: Box::default(), spread: None }
433431
};
434432

435433
self.alloc_expr(record_lit, syntax_ptr)
@@ -496,7 +494,10 @@ impl ExprCollector<'_> {
496494
.and_then(|r| r.ty())
497495
.map(|it| Interned::new(TypeRef::from_ast(&self.ctx(), it)));
498496
let body = self.collect_expr_opt(e.body());
499-
self.alloc_expr(Expr::Lambda { args, arg_types, ret_type, body }, syntax_ptr)
497+
self.alloc_expr(
498+
Expr::Lambda { args: args.into(), arg_types: arg_types.into(), ret_type, body },
499+
syntax_ptr,
500+
)
500501
}
501502
ast::Expr::BinExpr(e) => {
502503
let lhs = self.collect_expr_opt(e.lhs());
@@ -718,7 +719,7 @@ impl ExprCollector<'_> {
718719
self.statements_in_scope.pop();
719720
}
720721
let tail = tail;
721-
let statements = std::mem::replace(&mut self.statements_in_scope, prev_statements);
722+
let statements = std::mem::replace(&mut self.statements_in_scope, prev_statements).into();
722723
let syntax_node_ptr = AstPtr::new(&block.into());
723724
let expr_id = self.alloc_expr(
724725
Expr::Block { id: block_id, statements, tail, label: None },
@@ -812,7 +813,7 @@ impl ExprCollector<'_> {
812813
ast::Pat::RecordPat(p) => {
813814
let path =
814815
p.path().and_then(|path| self.expander.parse_path(self.db, path)).map(Box::new);
815-
let args: Vec<_> = p
816+
let args = p
816817
.record_pat_field_list()
817818
.expect("every struct should have a field list")
818819
.fields()
@@ -902,7 +903,7 @@ impl ExprCollector<'_> {
902903
}
903904
}
904905

905-
fn collect_tuple_pat(&mut self, args: AstChildren<ast::Pat>) -> (Vec<PatId>, Option<usize>) {
906+
fn collect_tuple_pat(&mut self, args: AstChildren<ast::Pat>) -> (Box<[PatId]>, Option<usize>) {
906907
// Find the location of the `..`, if there is one. Note that we do not
907908
// consider the possibility of there being multiple `..` here.
908909
let ellipsis = args.clone().position(|p| matches!(p, ast::Pat::RestPat(_)));
@@ -961,7 +962,7 @@ impl From<ast::LiteralKind> for Literal {
961962
Literal::Float(Default::default(), ty)
962963
}
963964
LiteralKind::ByteString(bs) => {
964-
let text = bs.value().map(Vec::from).unwrap_or_else(Default::default);
965+
let text = bs.value().map(Box::from).unwrap_or_else(Default::default);
965966
Literal::ByteString(text)
966967
}
967968
LiteralKind::String(_) => Literal::String(Default::default()),

crates/hir_def/src/body/scope.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,7 @@ fn compute_expr_scopes(expr: ExprId, body: &Body, scopes: &mut ExprScopes, scope
204204
}
205205
Expr::Match { expr, arms } => {
206206
compute_expr_scopes(*expr, body, scopes, scope);
207-
for arm in arms {
207+
for arm in arms.iter() {
208208
let mut scope = scopes.new_scope(scope);
209209
scopes.add_bindings(body, scope, arm.pat);
210210
match arm.guard {

crates/hir_def/src/expr.rs

Lines changed: 37 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,8 @@ pub type LabelId = Idx<Label>;
4040

4141
#[derive(Debug, Clone, Eq, PartialEq)]
4242
pub enum Literal {
43-
String(String),
44-
ByteString(Vec<u8>),
43+
String(Box<str>),
44+
ByteString(Box<[u8]>),
4545
Char(char),
4646
Bool(bool),
4747
Int(i128, Option<BuiltinInt>),
@@ -61,7 +61,7 @@ pub enum Expr {
6161
},
6262
Block {
6363
id: BlockId,
64-
statements: Vec<Statement>,
64+
statements: Box<[Statement]>,
6565
tail: Option<ExprId>,
6666
label: Option<LabelId>,
6767
},
@@ -82,17 +82,17 @@ pub enum Expr {
8282
},
8383
Call {
8484
callee: ExprId,
85-
args: Vec<ExprId>,
85+
args: Box<[ExprId]>,
8686
},
8787
MethodCall {
8888
receiver: ExprId,
8989
method_name: Name,
90-
args: Vec<ExprId>,
90+
args: Box<[ExprId]>,
9191
generic_args: Option<Box<GenericArgs>>,
9292
},
9393
Match {
9494
expr: ExprId,
95-
arms: Vec<MatchArm>,
95+
arms: Box<[MatchArm]>,
9696
},
9797
Continue {
9898
label: Option<Name>,
@@ -109,7 +109,7 @@ pub enum Expr {
109109
},
110110
RecordLit {
111111
path: Option<Box<Path>>,
112-
fields: Vec<RecordLitField>,
112+
fields: Box<[RecordLitField]>,
113113
spread: Option<ExprId>,
114114
},
115115
Field {
@@ -162,13 +162,13 @@ pub enum Expr {
162162
index: ExprId,
163163
},
164164
Lambda {
165-
args: Vec<PatId>,
166-
arg_types: Vec<Option<Interned<TypeRef>>>,
165+
args: Box<[PatId]>,
166+
arg_types: Box<[Option<Interned<TypeRef>>]>,
167167
ret_type: Option<Interned<TypeRef>>,
168168
body: ExprId,
169169
},
170170
Tuple {
171-
exprs: Vec<ExprId>,
171+
exprs: Box<[ExprId]>,
172172
},
173173
Unsafe {
174174
body: ExprId,
@@ -182,7 +182,7 @@ pub enum Expr {
182182

183183
#[derive(Debug, Clone, Eq, PartialEq)]
184184
pub enum Array {
185-
ElementList(Vec<ExprId>),
185+
ElementList(Box<[ExprId]>),
186186
Repeat { initializer: ExprId, repeat: ExprId },
187187
}
188188

@@ -228,23 +228,23 @@ impl Expr {
228228
Expr::If { condition, then_branch, else_branch } => {
229229
f(*condition);
230230
f(*then_branch);
231-
if let Some(else_branch) = else_branch {
232-
f(*else_branch);
231+
if let &Some(else_branch) = else_branch {
232+
f(else_branch);
233233
}
234234
}
235235
Expr::Block { statements, tail, .. } => {
236-
for stmt in statements {
236+
for stmt in statements.iter() {
237237
match stmt {
238238
Statement::Let { initializer, .. } => {
239-
if let Some(expr) = initializer {
240-
f(*expr);
239+
if let &Some(expr) = initializer {
240+
f(expr);
241241
}
242242
}
243243
Statement::Expr { expr: expression, .. } => f(*expression),
244244
}
245245
}
246-
if let Some(expr) = tail {
247-
f(*expr);
246+
if let &Some(expr) = tail {
247+
f(expr);
248248
}
249249
}
250250
Expr::TryBlock { body }
@@ -262,34 +262,28 @@ impl Expr {
262262
}
263263
Expr::Call { callee, args } => {
264264
f(*callee);
265-
for arg in args {
266-
f(*arg);
267-
}
265+
args.iter().copied().for_each(f);
268266
}
269267
Expr::MethodCall { receiver, args, .. } => {
270268
f(*receiver);
271-
for arg in args {
272-
f(*arg);
273-
}
269+
args.iter().copied().for_each(f);
274270
}
275271
Expr::Match { expr, arms } => {
276272
f(*expr);
277-
for arm in arms {
278-
f(arm.expr);
279-
}
273+
arms.iter().map(|arm| arm.expr).for_each(f);
280274
}
281275
Expr::Continue { .. } => {}
282276
Expr::Break { expr, .. } | Expr::Return { expr } | Expr::Yield { expr } => {
283-
if let Some(expr) = expr {
284-
f(*expr);
277+
if let &Some(expr) = expr {
278+
f(expr);
285279
}
286280
}
287281
Expr::RecordLit { fields, spread, .. } => {
288-
for field in fields {
282+
for field in fields.iter() {
289283
f(field.expr);
290284
}
291-
if let Some(expr) = spread {
292-
f(*expr);
285+
if let &Some(expr) = spread {
286+
f(expr);
293287
}
294288
}
295289
Expr::Lambda { body, .. } => {
@@ -300,11 +294,11 @@ impl Expr {
300294
f(*rhs);
301295
}
302296
Expr::Range { lhs, rhs, .. } => {
303-
if let Some(lhs) = rhs {
304-
f(*lhs);
297+
if let &Some(lhs) = rhs {
298+
f(lhs);
305299
}
306-
if let Some(rhs) = lhs {
307-
f(*rhs);
300+
if let &Some(rhs) = lhs {
301+
f(rhs);
308302
}
309303
}
310304
Expr::Index { base, index } => {
@@ -320,17 +314,9 @@ impl Expr {
320314
| Expr::Box { expr } => {
321315
f(*expr);
322316
}
323-
Expr::Tuple { exprs } => {
324-
for expr in exprs {
325-
f(*expr);
326-
}
327-
}
317+
Expr::Tuple { exprs } => exprs.iter().copied().for_each(f),
328318
Expr::Array(a) => match a {
329-
Array::ElementList(exprs) => {
330-
for expr in exprs {
331-
f(*expr);
332-
}
333-
}
319+
Array::ElementList(exprs) => exprs.iter().copied().for_each(f),
334320
Array::Repeat { initializer, repeat } => {
335321
f(*initializer);
336322
f(*repeat)
@@ -386,15 +372,15 @@ pub struct RecordFieldPat {
386372
pub enum Pat {
387373
Missing,
388374
Wild,
389-
Tuple { args: Vec<PatId>, ellipsis: Option<usize> },
390-
Or(Vec<PatId>),
391-
Record { path: Option<Box<Path>>, args: Vec<RecordFieldPat>, ellipsis: bool },
375+
Tuple { args: Box<[PatId]>, ellipsis: Option<usize> },
376+
Or(Box<[PatId]>),
377+
Record { path: Option<Box<Path>>, args: Box<[RecordFieldPat]>, ellipsis: bool },
392378
Range { start: ExprId, end: ExprId },
393-
Slice { prefix: Vec<PatId>, slice: Option<PatId>, suffix: Vec<PatId> },
379+
Slice { prefix: Box<[PatId]>, slice: Option<PatId>, suffix: Box<[PatId]> },
394380
Path(Box<Path>),
395381
Lit(ExprId),
396382
Bind { mode: BindingAnnotation, name: Name, subpat: Option<PatId> },
397-
TupleStruct { path: Option<Box<Path>>, args: Vec<PatId>, ellipsis: Option<usize> },
383+
TupleStruct { path: Option<Box<Path>>, args: Box<[PatId]>, ellipsis: Option<usize> },
398384
Ref { pat: PatId, mutability: Mutability },
399385
Box { inner: PatId },
400386
ConstBlock(ExprId),

0 commit comments

Comments
 (0)