Skip to content

Commit

Permalink
Avoid ref when using format! in compiler
Browse files Browse the repository at this point in the history
Clean up a few minor refs in `format!` macro, as it has a performance cost. Apparently the compiler is unable to inline `format!("{}", &variable)`, and does a run-time double-reference instead (format macro already does one level referencing).  Inlining format args prevents accidental `&` misuse.
  • Loading branch information
nyurik committed Jul 19, 2024
1 parent 0cd01aa commit d37ee56
Show file tree
Hide file tree
Showing 13 changed files with 20 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/bootstrap/src/core/build_steps/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ fn copy_sanitizers(
|| target == "x86_64-apple-ios"
{
// Update the library’s install name to reflect that it has been renamed.
apple_darwin_update_library_name(builder, &dst, &format!("@rpath/{}", &runtime.name));
apple_darwin_update_library_name(builder, &dst, &format!("@rpath/{}", runtime.name));
// Upon renaming the install name, the code signature of the file will invalidate,
// so we will sign it again.
apple_darwin_sign_file(builder, &dst);
Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/src/core/build_steps/llvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1411,7 +1411,7 @@ impl Step for Libunwind {
}
}
}
assert_eq!(cpp_len, count, "Can't get object files from {:?}", &out_dir);
assert_eq!(cpp_len, count, "Can't get object files from {out_dir:?}");

cc_cfg.compile("unwind");
out_dir
Expand Down
10 changes: 5 additions & 5 deletions src/bootstrap/src/utils/cc_detect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,15 +142,15 @@ pub fn find_target(build: &Build, target: TargetSelection) {
build.cxx.borrow_mut().insert(target, compiler);
}

build.verbose(|| println!("CC_{} = {:?}", &target.triple, build.cc(target)));
build.verbose(|| println!("CFLAGS_{} = {:?}", &target.triple, cflags));
build.verbose(|| println!("CC_{} = {:?}", target.triple, build.cc(target)));
build.verbose(|| println!("CFLAGS_{} = {cflags:?}", target.triple));
if let Ok(cxx) = build.cxx(target) {
let cxxflags = build.cflags(target, GitRepo::Rustc, CLang::Cxx);
build.verbose(|| println!("CXX_{} = {:?}", &target.triple, cxx));
build.verbose(|| println!("CXXFLAGS_{} = {:?}", &target.triple, cxxflags));
build.verbose(|| println!("CXX_{} = {cxx:?}", target.triple));
build.verbose(|| println!("CXXFLAGS_{} = {cxxflags:?}", target.triple));
}
if let Some(ar) = ar {
build.verbose(|| println!("AR_{} = {:?}", &target.triple, ar));
build.verbose(|| println!("AR_{} = {ar:?}", target.triple));
build.ar.borrow_mut().insert(target, ar);
}

Expand Down
2 changes: 1 addition & 1 deletion src/bootstrap/src/utils/tarball.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl<'a> Tarball<'a> {
cmd.arg("generate")
.arg("--image-dir")
.arg(&this.image_dir)
.arg(format!("--component-name={}", &component_name));
.arg(format!("--component-name={component_name}"));

if let Some((dir, dirs)) = this.bulk_dirs.split_first() {
let mut arg = dir.as_os_str().to_os_string();
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ fn resolved_path<'cx>(
}
}
if w.alternate() {
write!(w, "{}{:#}", &last.name, last.args.print(cx))?;
write!(w, "{}{:#}", last.name, last.args.print(cx))?;
} else {
let path = if use_absolute {
if let Ok((_, _, fqp)) = href(did, cx) {
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_lints/src/approx_const.rs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ impl ApproxConstant {
cx,
APPROX_CONSTANT,
e.span,
format!("approximate value of `{module}::consts::{}` found", &name),
format!("approximate value of `{module}::consts::{name}` found"),
None,
"consider using the constant directly",
);
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_lints/src/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ impl<'tcx> LateLintPass<'tcx> for Default {
.map(ToString::to_string)
.collect::<Vec<_>>()
.join(", ");
format!("{adt_def_ty_name}::<{}>", &tys_str)
format!("{adt_def_ty_name}::<{tys_str}>")
} else {
binding_type.to_string()
};
Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_lints/src/methods/open_options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ fn check_open_options(cx: &LateContext<'_>, settings: &[(OpenOption, Argument, S
cx,
NONSENSICAL_OPEN_OPTIONS,
prev_span,
format!("the method `{}` is called more than once", &option),
format!("the method `{option}` is called more than once"),
);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,9 +127,9 @@ pub(super) fn check<'tcx>(
.collect::<Vec<_>>()
.join(" and ");

format!("methods with the following characteristics: ({})", &s)
format!("methods with the following characteristics: ({s})")
} else {
format!("methods called {}", &conventions[0])
format!("methods called {}", conventions[0])
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/tools/clippy/clippy_lints/src/no_effect.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ fn check_unnecessary_operation(cx: &LateContext<'_>, stmt: &Stmt<'_>) {
}
let snippet =
if let (Some(arr), Some(func)) = (snippet_opt(cx, reduced[0].span), snippet_opt(cx, reduced[1].span)) {
format!("assert!({}.len() > {});", &arr, &func)
format!("assert!({arr}.len() > {func});")
} else {
return;
};
Expand Down
6 changes: 3 additions & 3 deletions src/tools/clippy/clippy_lints/src/types/borrowed_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ pub(super) fn check(cx: &LateContext<'_>, hir_ty: &hir::Ty<'_>, lt: &Lifetime, m
let inner_snippet = snippet(cx, inner.span, "..");
let suggestion = match &inner.kind {
TyKind::TraitObject(bounds, lt_bound, _) if bounds.len() > 1 || !lt_bound.is_elided() => {
format!("&{ltopt}({})", &inner_snippet)
format!("&{ltopt}({inner_snippet})")
},
TyKind::Path(qpath)
if get_bounds_if_impl_trait(cx, qpath, inner.hir_id)
.map_or(false, |bounds| bounds.len() > 1) =>
{
format!("&{ltopt}({})", &inner_snippet)
format!("&{ltopt}({inner_snippet})")
},
_ => format!("&{ltopt}{}", &inner_snippet),
_ => format!("&{ltopt}{inner_snippet}"),
};
span_lint_and_sugg(
cx,
Expand Down
2 changes: 1 addition & 1 deletion src/tools/compiletest/src/runtest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4349,7 +4349,7 @@ impl<'test> TestCx<'test> {
)"#
)
.replace_all(&output, |caps: &Captures<'_>| {
println!("{}", &caps[0]);
println!("{}", caps[0]);
caps[0].replace(r"\", "/")
})
.replace("\r\n", "\n")
Expand Down
2 changes: 1 addition & 1 deletion src/tools/linkchecker/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,7 @@ impl Checker {
} else {
report.errors += 1;
print!("{}:{}: broken intra-doc link - ", pretty_path, i + 1);
println!("{}", &broken_link[0]);
println!("{}", broken_link[0]);
}
}
}
Expand Down

0 comments on commit d37ee56

Please sign in to comment.