Skip to content

Commit a2051dd

Browse files
committed
Optimize some alloc_from_iter call sites.
There's no need to collect an iterator into a `Vec`, or to call `into_iter` at the call sites.
1 parent 816383c commit a2051dd

File tree

5 files changed

+29
-38
lines changed

5 files changed

+29
-38
lines changed

compiler/rustc_ast_lowering/src/format.rs

+18-29
Original file line numberDiff line numberDiff line change
@@ -410,15 +410,11 @@ fn expand_format_args<'hir>(
410410
let format_options = use_format_options.then(|| {
411411
// Generate:
412412
// &[format_spec_0, format_spec_1, format_spec_2]
413-
let elements: Vec<_> = fmt
414-
.template
415-
.iter()
416-
.filter_map(|piece| {
417-
let FormatArgsPiece::Placeholder(placeholder) = piece else { return None };
418-
Some(make_format_spec(ctx, macsp, placeholder, &mut argmap))
419-
})
420-
.collect();
421-
ctx.expr_array_ref(macsp, ctx.arena.alloc_from_iter(elements))
413+
let elements = ctx.arena.alloc_from_iter(fmt.template.iter().filter_map(|piece| {
414+
let FormatArgsPiece::Placeholder(placeholder) = piece else { return None };
415+
Some(make_format_spec(ctx, macsp, placeholder, &mut argmap))
416+
}));
417+
ctx.expr_array_ref(macsp, elements)
422418
});
423419

424420
let arguments = fmt.arguments.all_args();
@@ -477,10 +473,8 @@ fn expand_format_args<'hir>(
477473
// <core::fmt::Argument>::new_debug(&arg2),
478474
// …
479475
// ]
480-
let elements: Vec<_> = arguments
481-
.iter()
482-
.zip(argmap)
483-
.map(|(arg, ((_, ty), placeholder_span))| {
476+
let elements = ctx.arena.alloc_from_iter(arguments.iter().zip(argmap).map(
477+
|(arg, ((_, ty), placeholder_span))| {
484478
let placeholder_span =
485479
placeholder_span.unwrap_or(arg.expr.span).with_ctxt(macsp.ctxt());
486480
let arg_span = match arg.kind {
@@ -493,9 +487,9 @@ fn expand_format_args<'hir>(
493487
hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, arg),
494488
));
495489
make_argument(ctx, placeholder_span, ref_arg, ty)
496-
})
497-
.collect();
498-
ctx.expr_array_ref(macsp, ctx.arena.alloc_from_iter(elements))
490+
},
491+
));
492+
ctx.expr_array_ref(macsp, elements)
499493
} else {
500494
// Generate:
501495
// &match (&arg0, &arg1, &…) {
@@ -528,19 +522,14 @@ fn expand_format_args<'hir>(
528522
make_argument(ctx, placeholder_span, arg, ty)
529523
},
530524
));
531-
let elements: Vec<_> = arguments
532-
.iter()
533-
.map(|arg| {
534-
let arg_expr = ctx.lower_expr(&arg.expr);
535-
ctx.expr(
536-
arg.expr.span.with_ctxt(macsp.ctxt()),
537-
hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, arg_expr),
538-
)
539-
})
540-
.collect();
541-
let args_tuple = ctx
542-
.arena
543-
.alloc(ctx.expr(macsp, hir::ExprKind::Tup(ctx.arena.alloc_from_iter(elements))));
525+
let elements = ctx.arena.alloc_from_iter(arguments.iter().map(|arg| {
526+
let arg_expr = ctx.lower_expr(&arg.expr);
527+
ctx.expr(
528+
arg.expr.span.with_ctxt(macsp.ctxt()),
529+
hir::ExprKind::AddrOf(hir::BorrowKind::Ref, hir::Mutability::Not, arg_expr),
530+
)
531+
}));
532+
let args_tuple = ctx.arena.alloc(ctx.expr(macsp, hir::ExprKind::Tup(elements)));
544533
let array = ctx.arena.alloc(ctx.expr(macsp, hir::ExprKind::Array(args)));
545534
let match_arms = ctx.arena.alloc_from_iter([ctx.arm(args_pat, array)]);
546535
let match_expr = ctx.arena.alloc(ctx.expr_match(

compiler/rustc_hir_analysis/src/variance/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -192,5 +192,5 @@ fn variance_of_opaque(tcx: TyCtxt<'_>, item_def_id: LocalDefId) -> &[ty::Varianc
192192
}
193193
}
194194
}
195-
tcx.arena.alloc_from_iter(collector.variances.into_iter())
195+
tcx.arena.alloc_from_iter(collector.variances)
196196
}

compiler/rustc_middle/src/ty/codec.rs

+8-6
Original file line numberDiff line numberDiff line change
@@ -348,9 +348,10 @@ impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for ty::Const<'tcx> {
348348

349349
impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D> for [ty::ValTree<'tcx>] {
350350
fn decode(decoder: &mut D) -> &'tcx Self {
351-
decoder.interner().arena.alloc_from_iter(
352-
(0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::<Vec<_>>(),
353-
)
351+
decoder
352+
.interner()
353+
.arena
354+
.alloc_from_iter((0..decoder.read_usize()).map(|_| Decodable::decode(decoder)))
354355
}
355356
}
356357

@@ -368,9 +369,10 @@ impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for AdtDef<'tcx> {
368369

369370
impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D> for [(ty::Clause<'tcx>, Span)] {
370371
fn decode(decoder: &mut D) -> &'tcx Self {
371-
decoder.interner().arena.alloc_from_iter(
372-
(0..decoder.read_usize()).map(|_| Decodable::decode(decoder)).collect::<Vec<_>>(),
373-
)
372+
decoder
373+
.interner()
374+
.arena
375+
.alloc_from_iter((0..decoder.read_usize()).map(|_| Decodable::decode(decoder)))
374376
}
375377
}
376378

compiler/rustc_trait_selection/src/traits/vtable.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ fn vtable_entries<'tcx>(
316316
dump_vtable_entries(tcx, sp, trait_ref, &entries);
317317
}
318318

319-
tcx.arena.alloc_from_iter(entries.into_iter())
319+
tcx.arena.alloc_from_iter(entries)
320320
}
321321

322322
/// Find slot base for trait methods within vtable entries of another trait

compiler/rustc_ty_utils/src/consts.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ pub(crate) fn destructure_const<'tcx>(
7171
_ => bug!("cannot destructure constant {:?}", const_),
7272
};
7373

74-
let fields = tcx.arena.alloc_from_iter(fields.into_iter());
74+
let fields = tcx.arena.alloc_from_iter(fields);
7575

7676
ty::DestructuredConst { variant, fields }
7777
}

0 commit comments

Comments
 (0)