Skip to content

Commit 2ca0b85

Browse files
committed
Allocate less in lower_block_noalloc
1 parent 660d8a6 commit 2ca0b85

File tree

1 file changed

+21
-20
lines changed
  • compiler/rustc_ast_lowering/src

1 file changed

+21
-20
lines changed

compiler/rustc_ast_lowering/src/lib.rs

Lines changed: 21 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2312,29 +2312,30 @@ impl<'a, 'hir> LoweringContext<'a, 'hir> {
23122312
}
23132313

23142314
fn lower_block_noalloc(&mut self, b: &Block, targeted_by_break: bool) -> hir::Block<'hir> {
2315-
let mut stmts = vec![];
23162315
let mut expr: Option<&'hir _> = None;
23172316

2318-
for (index, stmt) in b.stmts.iter().enumerate() {
2319-
if index == b.stmts.len() - 1 {
2320-
if let StmtKind::Expr(ref e) = stmt.kind {
2321-
expr = Some(self.lower_expr(e));
2322-
} else {
2323-
stmts.extend(self.lower_stmt(stmt));
2324-
}
2325-
} else {
2326-
stmts.extend(self.lower_stmt(stmt));
2327-
}
2328-
}
2317+
let stmts = self.arena.alloc_from_iter(
2318+
b.stmts
2319+
.iter()
2320+
.enumerate()
2321+
.filter_map(|(index, stmt)| {
2322+
if index == b.stmts.len() - 1 {
2323+
if let StmtKind::Expr(ref e) = stmt.kind {
2324+
expr = Some(self.lower_expr(e));
2325+
None
2326+
} else {
2327+
Some(self.lower_stmt(stmt))
2328+
}
2329+
} else {
2330+
Some(self.lower_stmt(stmt))
2331+
}
2332+
})
2333+
.flatten(),
2334+
);
2335+
let rules = self.lower_block_check_mode(&b.rules);
2336+
let hir_id = self.lower_node_id(b.id);
23292337

2330-
hir::Block {
2331-
hir_id: self.lower_node_id(b.id),
2332-
stmts: self.arena.alloc_from_iter(stmts),
2333-
expr,
2334-
rules: self.lower_block_check_mode(&b.rules),
2335-
span: b.span,
2336-
targeted_by_break,
2337-
}
2338+
hir::Block { hir_id, stmts, expr, rules, span: b.span, targeted_by_break }
23382339
}
23392340

23402341
/// Lowers a block directly to an expression, presuming that it

0 commit comments

Comments
 (0)