Skip to content

Commit cc42adf

Browse files
authored
Rollup merge of #65463 - nnethercote:rm-arena-allocation-from-expand_pattern, r=varkor
Avoid unnecessary arena allocations in `expand_pattern()`. `expand_pattern()` has two callsites. One of them needs arena allocation, but the other does not. This commit moves the arena allocation out of the function. This avoids the allocation of many 4 KiB page arena chunks that only hold a single small allocation. It reduces the number of bytes allocated by up to 2% for various benchmarks, albeit without only a very small improvement in runtime.
2 parents 2518bbd + c4deea2 commit cc42adf

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -189,8 +189,8 @@ use std::ops::RangeInclusive;
189189
use std::u128;
190190
use std::convert::TryInto;
191191

192-
pub fn expand_pattern<'a, 'tcx>(cx: &MatchCheckCtxt<'a, 'tcx>, pat: Pat<'tcx>) -> &'a Pat<'tcx> {
193-
cx.pattern_arena.alloc(LiteralExpander { tcx: cx.tcx }.fold_pattern(&pat))
192+
pub fn expand_pattern<'a, 'tcx>(cx: &MatchCheckCtxt<'a, 'tcx>, pat: Pat<'tcx>) -> Pat<'tcx> {
193+
LiteralExpander { tcx: cx.tcx }.fold_pattern(&pat)
194194
}
195195

196196
struct LiteralExpander<'tcx> {

src/librustc_mir/hair/pattern/check_match.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,8 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
154154
self.tables
155155
);
156156
patcx.include_lint_checks();
157-
let pattern = expand_pattern(cx, patcx.lower_pattern(&pat));
157+
let pattern =
158+
cx.pattern_arena.alloc(expand_pattern(cx, patcx.lower_pattern(&pat))) as &_;
158159
if !patcx.errors.is_empty() {
159160
patcx.report_inlining_errors(pat.span);
160161
have_errors = true;
@@ -253,8 +254,9 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
253254
patcx.include_lint_checks();
254255
let pattern = patcx.lower_pattern(pat);
255256
let pattern_ty = pattern.ty;
257+
let pattern = expand_pattern(cx, pattern);
256258
let pats: Matrix<'_, '_> = vec![smallvec![
257-
expand_pattern(cx, pattern)
259+
&pattern
258260
]].into_iter().collect();
259261

260262
let witnesses = match check_not_useful(cx, pattern_ty, &pats, pat.hir_id) {

0 commit comments

Comments
 (0)