Skip to content
This repository was archived by the owner on May 28, 2025. It is now read-only.

Commit 3f294f8

Browse files
committed
Auto merge of rust-lang#126784 - scottmcm:smaller-terminator, r=<try>
Save 2 pointers in `TerminatorKind` (96 → 80 bytes) These things don't need to be `Vec`s; boxed slices are enough. The frequent one here is call arguments, but MIR building knows the number of arguments from the THIR, so the collect is always getting the allocation right in the first place, and thus this shouldn't ever add the shrink-in-place overhead. r? ghost
2 parents e32ea48 + 42bc2d5 commit 3f294f8

File tree

14 files changed

+68
-69
lines changed

14 files changed

+68
-69
lines changed

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1817,11 +1817,11 @@ mod size_asserts {
18171817
use super::*;
18181818
use rustc_data_structures::static_assert_size;
18191819
// tidy-alphabetical-start
1820-
static_assert_size!(BasicBlockData<'_>, 144);
1820+
static_assert_size!(BasicBlockData<'_>, 128);
18211821
static_assert_size!(LocalDecl<'_>, 40);
18221822
static_assert_size!(SourceScopeData<'_>, 64);
18231823
static_assert_size!(Statement<'_>, 32);
1824-
static_assert_size!(Terminator<'_>, 112);
1824+
static_assert_size!(Terminator<'_>, 96);
18251825
static_assert_size!(VarDebugInfo<'_>, 88);
18261826
// tidy-alphabetical-end
18271827
}

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -730,7 +730,7 @@ pub enum TerminatorKind<'tcx> {
730730
/// reused across function calls without duplicating the contents.
731731
/// The span for each arg is also included
732732
/// (e.g. `a` and `b` in `x.foo(a, b)`).
733-
args: Vec<Spanned<Operand<'tcx>>>,
733+
args: Box<[Spanned<Operand<'tcx>>]>,
734734
/// Where the returned value will be written
735735
destination: Place<'tcx>,
736736
/// Where to go after this call returns. If none, the call necessarily diverges.
@@ -837,7 +837,7 @@ pub enum TerminatorKind<'tcx> {
837837
template: &'tcx [InlineAsmTemplatePiece],
838838

839839
/// The operands for the inline assembly, as `Operand`s or `Place`s.
840-
operands: Vec<InlineAsmOperand<'tcx>>,
840+
operands: Box<[InlineAsmOperand<'tcx>]>,
841841

842842
/// Miscellaneous options for the inline assembly.
843843
options: InlineAsmOptions,
@@ -849,7 +849,7 @@ pub enum TerminatorKind<'tcx> {
849849
/// Valid targets for the inline assembly.
850850
/// The first element is the fallthrough destination, unless
851851
/// InlineAsmOptions::NORETURN is set.
852-
targets: Vec<BasicBlock>,
852+
targets: Box<[BasicBlock]>,
853853

854854
/// Action to be taken if the inline assembly unwinds. This is present
855855
/// if and only if InlineAsmOptions::MAY_UNWIND is set.
@@ -1561,6 +1561,6 @@ mod size_asserts {
15611561
static_assert_size!(PlaceElem<'_>, 24);
15621562
static_assert_size!(Rvalue<'_>, 40);
15631563
static_assert_size!(StatementKind<'_>, 16);
1564-
static_assert_size!(TerminatorKind<'_>, 96);
1564+
static_assert_size!(TerminatorKind<'_>, 80);
15651565
// tidy-alphabetical-end
15661566
}

compiler/rustc_mir_build/src/build/custom/parse/instruction.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ impl<'tcx, 'body> ParseCtxt<'tcx, 'body> {
170170
.map(|arg|
171171
Ok(Spanned { node: self.parse_operand(*arg)?, span: self.thir.exprs[*arg].span } )
172172
)
173-
.collect::<PResult<Vec<_>>>()?;
173+
.collect::<PResult<Box<[_]>>>()?;
174174
Ok(TerminatorKind::Call {
175175
func: fun,
176176
args,

compiler/rustc_mir_build/src/build/expr/as_rvalue.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,10 +152,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
152152
source_info,
153153
TerminatorKind::Call {
154154
func: exchange_malloc,
155-
args: vec![
155+
args: [
156156
Spanned { node: Operand::Move(size), span: DUMMY_SP },
157157
Spanned { node: Operand::Move(align), span: DUMMY_SP },
158-
],
158+
]
159+
.into(),
159160
destination: storage,
160161
target: Some(success),
161162
unwind: UnwindAction::Continue,

compiler/rustc_mir_build/src/build/expr/into.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
238238
}
239239
ExprKind::Call { ty: _, fun, ref args, from_hir_call, fn_span } => {
240240
let fun = unpack!(block = this.as_local_operand(block, fun));
241-
let args: Vec<_> = args
241+
let args: Box<[_]> = args
242242
.into_iter()
243243
.copied()
244244
.map(|arg| Spanned {
@@ -485,7 +485,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
485485
operands,
486486
options,
487487
line_spans,
488-
targets,
488+
targets: targets.into_boxed_slice(),
489489
unwind: if options.contains(InlineAsmOptions::MAY_UNWIND) {
490490
UnwindAction::Continue
491491
} else {

compiler/rustc_mir_build/src/build/matches/test.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
324324
user_ty: None,
325325
const_: method,
326326
})),
327-
args: vec![Spanned { node: Operand::Move(ref_src), span }],
327+
args: [Spanned { node: Operand::Move(ref_src), span }].into(),
328328
destination: temp,
329329
target: Some(target_block),
330330
unwind: UnwindAction::Continue,
@@ -486,10 +486,11 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
486486

487487
const_: method,
488488
})),
489-
args: vec![
489+
args: [
490490
Spanned { node: Operand::Copy(val), span: DUMMY_SP },
491491
Spanned { node: expect, span: DUMMY_SP },
492-
],
492+
]
493+
.into(),
493494
destination: eq_result,
494495
target: Some(eq_block),
495496
unwind: UnwindAction::Continue,

compiler/rustc_mir_dataflow/src/elaborate_drops.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -650,10 +650,8 @@ where
650650
[ty.into()],
651651
self.source_info.span,
652652
),
653-
args: vec![Spanned {
654-
node: Operand::Move(Place::from(ref_place)),
655-
span: DUMMY_SP,
656-
}],
653+
args: [Spanned { node: Operand::Move(Place::from(ref_place)), span: DUMMY_SP }]
654+
.into(),
657655
destination: unit_temp,
658656
target: Some(succ),
659657
unwind: unwind.into_action(),

compiler/rustc_mir_transform/src/coroutine.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -677,8 +677,8 @@ fn transform_async_context<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
677677

678678
fn eliminate_get_context_call<'tcx>(bb_data: &mut BasicBlockData<'tcx>) -> Local {
679679
let terminator = bb_data.terminator.take().unwrap();
680-
if let TerminatorKind::Call { mut args, destination, target, .. } = terminator.kind {
681-
let arg = args.pop().unwrap();
680+
if let TerminatorKind::Call { args, destination, target, .. } = terminator.kind {
681+
let [arg] = *Box::try_from(args).unwrap();
682682
let local = arg.node.place().unwrap().local;
683683

684684
let arg = Rvalue::Use(arg.node);

compiler/rustc_mir_transform/src/inline.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -624,8 +624,7 @@ impl<'tcx> Inliner<'tcx> {
624624
};
625625

626626
// Copy the arguments if needed.
627-
let args: Vec<_> =
628-
self.make_call_args(args, &callsite, caller_body, &callee_body, return_block);
627+
let args = self.make_call_args(args, &callsite, caller_body, &callee_body, return_block);
629628

630629
let mut integrator = Integrator {
631630
args: &args,
@@ -736,12 +735,12 @@ impl<'tcx> Inliner<'tcx> {
736735

737736
fn make_call_args(
738737
&self,
739-
args: Vec<Spanned<Operand<'tcx>>>,
738+
args: Box<[Spanned<Operand<'tcx>>]>,
740739
callsite: &CallSite<'tcx>,
741740
caller_body: &mut Body<'tcx>,
742741
callee_body: &Body<'tcx>,
743742
return_block: Option<BasicBlock>,
744-
) -> Vec<Local> {
743+
) -> Box<[Local]> {
745744
let tcx = self.tcx;
746745

747746
// There is a bit of a mismatch between the *caller* of a closure and the *callee*.
@@ -768,7 +767,7 @@ impl<'tcx> Inliner<'tcx> {
768767
//
769768
// and the vector is `[closure_ref, tmp0, tmp1, tmp2]`.
770769
if callsite.fn_sig.abi() == Abi::RustCall && callee_body.spread_arg.is_none() {
771-
let mut args = args.into_iter();
770+
let mut args = Vec::from(args).into_iter();
772771
let self_ = self.create_temp_if_necessary(
773772
args.next().unwrap().node,
774773
callsite,
@@ -802,7 +801,8 @@ impl<'tcx> Inliner<'tcx> {
802801

803802
closure_ref_arg.chain(tuple_tmp_args).collect()
804803
} else {
805-
args.into_iter()
804+
Vec::from(args)
805+
.into_iter()
806806
.map(|a| self.create_temp_if_necessary(a.node, callsite, caller_body, return_block))
807807
.collect()
808808
}

compiler/rustc_mir_transform/src/instsimplify.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Performs various peephole optimizations.
22
33
use crate::simplify::simplify_duplicate_switch_targets;
4+
use crate::take_array;
45
use rustc_ast::attr;
56
use rustc_middle::bug;
67
use rustc_middle::mir::*;
@@ -285,7 +286,8 @@ impl<'tcx> InstSimplifyContext<'tcx, '_> {
285286
return;
286287
}
287288

288-
let Some(arg_place) = args.pop().unwrap().node.place() else { return };
289+
let Ok([arg]) = take_array(args) else { return };
290+
let Some(arg_place) = arg.node.place() else { return };
289291

290292
statements.push(Statement {
291293
source_info: terminator.source_info,

0 commit comments

Comments
 (0)