From 2ca0b855933f4b9f72a995c81a69d7291268beca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Fri, 16 Oct 2020 16:43:42 +0200 Subject: [PATCH] Allocate less in lower_block_noalloc --- compiler/rustc_ast_lowering/src/lib.rs | 41 +++++++++++++------------- 1 file changed, 21 insertions(+), 20 deletions(-) diff --git a/compiler/rustc_ast_lowering/src/lib.rs b/compiler/rustc_ast_lowering/src/lib.rs index 549b66e2d36..ad3d3efe46a 100644 --- a/compiler/rustc_ast_lowering/src/lib.rs +++ b/compiler/rustc_ast_lowering/src/lib.rs @@ -2312,29 +2312,30 @@ fn lower_block(&mut self, b: &Block, targeted_by_break: bool) -> &'hir hir::Bloc } fn lower_block_noalloc(&mut self, b: &Block, targeted_by_break: bool) -> hir::Block<'hir> { - let mut stmts = vec![]; let mut expr: Option<&'hir _> = None; - for (index, stmt) in b.stmts.iter().enumerate() { - if index == b.stmts.len() - 1 { - if let StmtKind::Expr(ref e) = stmt.kind { - expr = Some(self.lower_expr(e)); - } else { - stmts.extend(self.lower_stmt(stmt)); - } - } else { - stmts.extend(self.lower_stmt(stmt)); - } - } + let stmts = self.arena.alloc_from_iter( + b.stmts + .iter() + .enumerate() + .filter_map(|(index, stmt)| { + if index == b.stmts.len() - 1 { + if let StmtKind::Expr(ref e) = stmt.kind { + expr = Some(self.lower_expr(e)); + None + } else { + Some(self.lower_stmt(stmt)) + } + } else { + Some(self.lower_stmt(stmt)) + } + }) + .flatten(), + ); + let rules = self.lower_block_check_mode(&b.rules); + let hir_id = self.lower_node_id(b.id); - hir::Block { - hir_id: self.lower_node_id(b.id), - stmts: self.arena.alloc_from_iter(stmts), - expr, - rules: self.lower_block_check_mode(&b.rules), - span: b.span, - targeted_by_break, - } + hir::Block { hir_id, stmts, expr, rules, span: b.span, targeted_by_break } } /// Lowers a block directly to an expression, presuming that it