Skip to content

Commit 8b2da3a

Browse files
committed
Auto merge of rust-lang#139205 - m-ou-se:format-args-opt-remove2, r=<try>
Remove 'simple array' lowering of format_args!() for >1 args. This is rust-lang#139175 but keeps the 'simple array' lowering for the case with 1 argument. This is an experiment to see the impact.
2 parents ed20157 + b11035c commit 8b2da3a

File tree

1 file changed

+4
-43
lines changed

1 file changed

+4
-43
lines changed

compiler/rustc_ast_lowering/src/format.rs

+4-43
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
use core::ops::ControlFlow;
21
use std::borrow::Cow;
32

4-
use rustc_ast::visit::Visitor;
53
use rustc_ast::*;
64
use rustc_data_structures::fx::FxIndexMap;
75
use rustc_hir as hir;
@@ -476,16 +474,10 @@ fn expand_format_args<'hir>(
476474
return hir::ExprKind::Call(new, new_args);
477475
}
478476

479-
// If the args array contains exactly all the original arguments once,
480-
// in order, we can use a simple array instead of a `match` construction.
481-
// However, if there's a yield point in any argument except the first one,
482-
// we don't do this, because an Argument cannot be kept across yield points.
483-
//
484-
// This is an optimization, speeding up compilation about 1-2% in some cases.
485-
// See https://github.com/rust-lang/rust/pull/106770#issuecomment-1380790609
486-
let use_simple_array = argmap.len() == arguments.len()
487-
&& argmap.iter().enumerate().all(|(i, (&(j, _), _))| i == j)
488-
&& arguments.iter().skip(1).all(|arg| !may_contain_yield_point(&arg.expr));
477+
// If the args array contains just one argument,
478+
// we can use a simple array instead of a `match` construction.
479+
let use_simple_array =
480+
argmap.len() == 1 && arguments.len() == 1 && argmap.first().unwrap().0.0 == 0;
489481

490482
let args = if arguments.is_empty() {
491483
// Generate:
@@ -514,9 +506,6 @@ fn expand_format_args<'hir>(
514506
// Generate:
515507
// &[
516508
// <core::fmt::Argument>::new_display(&arg0),
517-
// <core::fmt::Argument>::new_lower_hex(&arg1),
518-
// <core::fmt::Argument>::new_debug(&arg2),
519-
// …
520509
// ]
521510
let elements = ctx.arena.alloc_from_iter(arguments.iter().zip(argmap).map(
522511
|(arg, ((_, ty), placeholder_span))| {
@@ -635,34 +624,6 @@ fn expand_format_args<'hir>(
635624
}
636625
}
637626

638-
fn may_contain_yield_point(e: &ast::Expr) -> bool {
639-
struct MayContainYieldPoint;
640-
641-
impl Visitor<'_> for MayContainYieldPoint {
642-
type Result = ControlFlow<()>;
643-
644-
fn visit_expr(&mut self, e: &ast::Expr) -> ControlFlow<()> {
645-
if let ast::ExprKind::Await(_, _) | ast::ExprKind::Yield(_) = e.kind {
646-
ControlFlow::Break(())
647-
} else {
648-
visit::walk_expr(self, e)
649-
}
650-
}
651-
652-
fn visit_mac_call(&mut self, _: &ast::MacCall) -> ControlFlow<()> {
653-
// Macros should be expanded at this point.
654-
unreachable!("unexpanded macro in ast lowering");
655-
}
656-
657-
fn visit_item(&mut self, _: &ast::Item) -> ControlFlow<()> {
658-
// Do not recurse into nested items.
659-
ControlFlow::Continue(())
660-
}
661-
}
662-
663-
MayContainYieldPoint.visit_expr(e).is_break()
664-
}
665-
666627
fn for_all_argument_indexes(template: &mut [FormatArgsPiece], mut f: impl FnMut(&mut usize)) {
667628
for piece in template {
668629
let FormatArgsPiece::Placeholder(placeholder) = piece else { continue };

0 commit comments

Comments
 (0)