Allocate less in lower_block_noalloc

This commit is contained in:
Dániel Buga 2020-10-16 16:43:42 +02:00
parent 660d8a6550
commit 2ca0b85593

View File

@ -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> { 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; let mut expr: Option<&'hir _> = None;
for (index, stmt) in b.stmts.iter().enumerate() { let stmts = self.arena.alloc_from_iter(
if index == b.stmts.len() - 1 { b.stmts
if let StmtKind::Expr(ref e) = stmt.kind { .iter()
expr = Some(self.lower_expr(e)); .enumerate()
} else { .filter_map(|(index, stmt)| {
stmts.extend(self.lower_stmt(stmt)); if index == b.stmts.len() - 1 {
} if let StmtKind::Expr(ref e) = stmt.kind {
} else { expr = Some(self.lower_expr(e));
stmts.extend(self.lower_stmt(stmt)); 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::Block { hir_id, stmts, expr, rules, span: b.span, targeted_by_break }
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,
}
} }
/// Lowers a block directly to an expression, presuming that it /// Lowers a block directly to an expression, presuming that it