Skip to content

Commit c4deea2

Browse files
committed
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.
1 parent 237d54f commit c4deea2

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

Diff for: src/librustc_mir/hair/pattern/_match.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ use std::ops::RangeInclusive;
188188
use std::u128;
189189
use std::convert::TryInto;
190190

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

195195
struct LiteralExpander<'tcx> {

Diff for: src/librustc_mir/hair/pattern/check_match.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,8 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
153153
self.tables
154154
);
155155
patcx.include_lint_checks();
156-
let pattern = expand_pattern(cx, patcx.lower_pattern(&pat));
156+
let pattern =
157+
cx.pattern_arena.alloc(expand_pattern(cx, patcx.lower_pattern(&pat))) as &_;
157158
if !patcx.errors.is_empty() {
158159
patcx.report_inlining_errors(pat.span);
159160
have_errors = true;
@@ -252,8 +253,9 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
252253
patcx.include_lint_checks();
253254
let pattern = patcx.lower_pattern(pat);
254255
let pattern_ty = pattern.ty;
256+
let pattern = expand_pattern(cx, pattern);
255257
let pats: Matrix<'_, '_> = vec![smallvec![
256-
expand_pattern(cx, pattern)
258+
&pattern
257259
]].into_iter().collect();
258260

259261
let witnesses = match check_not_useful(cx, pattern_ty, &pats) {

0 commit comments

Comments
 (0)