Skip to content

Commit 023fd7e

Browse files
committed
Auto merge of #52767 - ljedrz:avoid_format, r=petrochenkov
Prefer to_string() to format!() Simple benchmarks suggest in some cases it can be faster by even 37%: ``` test converting_f64_long ... bench: 339 ns/iter (+/- 199) test converting_f64_short ... bench: 136 ns/iter (+/- 34) test converting_i32_long ... bench: 87 ns/iter (+/- 16) test converting_i32_short ... bench: 87 ns/iter (+/- 49) test converting_str ... bench: 54 ns/iter (+/- 15) test formatting_f64_long ... bench: 349 ns/iter (+/- 176) test formatting_f64_short ... bench: 145 ns/iter (+/- 14) test formatting_i32_long ... bench: 98 ns/iter (+/- 14) test formatting_i32_short ... bench: 93 ns/iter (+/- 15) test formatting_str ... bench: 86 ns/iter (+/- 23) ```
2 parents a5c2d0f + 57a5a9b commit 023fd7e

File tree

36 files changed

+95
-95
lines changed

36 files changed

+95
-95
lines changed

src/bootstrap/builder.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@ impl<'a> Builder<'a> {
925925
cargo.env("RUSTC_VERIFY_LLVM_IR", "1");
926926
}
927927

928-
cargo.env("RUSTC_VERBOSE", format!("{}", self.verbosity));
928+
cargo.env("RUSTC_VERBOSE", self.verbosity.to_string());
929929

930930
// in std, we want to avoid denying warnings for stage 0 as that makes cfg's painful.
931931
if self.config.deny_warnings && !(mode == Mode::Std && stage == 0) {

src/libcore/tests/str_lossy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ fn chunks() {
7979
fn display() {
8080
assert_eq!(
8181
"Hello\u{FFFD}\u{FFFD} There\u{FFFD} Goodbye",
82-
&format!("{}", Utf8Lossy::from_bytes(b"Hello\xC0\x80 There\xE6\x83 Goodbye")));
82+
&Utf8Lossy::from_bytes(b"Hello\xC0\x80 There\xE6\x83 Goodbye").to_string());
8383
}
8484

8585
#[test]

src/librustc/infer/error_reporting/mod.rs

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
557557
// Output the lifetimes fot the first type
558558
let lifetimes = sub.regions()
559559
.map(|lifetime| {
560-
let s = format!("{}", lifetime);
560+
let s = lifetime.to_string();
561561
if s.is_empty() {
562562
"'_".to_string()
563563
} else {
@@ -582,7 +582,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
582582
value.0.extend((values.0).0);
583583
other_value.0.extend((values.1).0);
584584
} else {
585-
value.push_highlighted(format!("{}", type_arg));
585+
value.push_highlighted(type_arg.to_string());
586586
}
587587

588588
if len > 0 && i != len - 1 {
@@ -716,7 +716,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
716716
mutbl: hir::Mutability,
717717
s: &mut DiagnosticStyledString,
718718
) {
719-
let r = &format!("{}", r);
719+
let r = &r.to_string();
720720
s.push_highlighted(format!(
721721
"&{}{}{}",
722722
r,
@@ -727,7 +727,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
727727
""
728728
}
729729
));
730-
s.push_normal(format!("{}", ty));
730+
s.push_normal(ty.to_string());
731731
}
732732

733733
match (&t1.sty, &t2.sty) {
@@ -768,7 +768,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
768768
}
769769

770770
fn lifetime_display(lifetime: Region) -> String {
771-
let s = format!("{}", lifetime);
771+
let s = lifetime.to_string();
772772
if s.is_empty() {
773773
"'_".to_string()
774774
} else {
@@ -863,8 +863,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
863863
// We couldn't find anything in common, highlight everything.
864864
// let x: Bar<Qux> = y::<Foo<Zar>>();
865865
(
866-
DiagnosticStyledString::highlighted(format!("{}", t1)),
867-
DiagnosticStyledString::highlighted(format!("{}", t2)),
866+
DiagnosticStyledString::highlighted(t1.to_string()),
867+
DiagnosticStyledString::highlighted(t2.to_string()),
868868
)
869869
}
870870
}
@@ -873,12 +873,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
873873
(&ty::TyRef(r1, ref_ty1, mutbl1), _) if equals(&ref_ty1, &t2) => {
874874
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
875875
push_ty_ref(&r1, ref_ty1, mutbl1, &mut values.0);
876-
values.1.push_normal(format!("{}", t2));
876+
values.1.push_normal(t2.to_string());
877877
values
878878
}
879879
(_, &ty::TyRef(r2, ref_ty2, mutbl2)) if equals(&t1, &ref_ty2) => {
880880
let mut values = (DiagnosticStyledString::new(), DiagnosticStyledString::new());
881-
values.0.push_normal(format!("{}", t1));
881+
values.0.push_normal(t1.to_string());
882882
push_ty_ref(&r2, ref_ty2, mutbl2, &mut values.1);
883883
values
884884
}
@@ -902,8 +902,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
902902
} else {
903903
// We couldn't find anything in common, highlight everything.
904904
(
905-
DiagnosticStyledString::highlighted(format!("{}", t1)),
906-
DiagnosticStyledString::highlighted(format!("{}", t2)),
905+
DiagnosticStyledString::highlighted(t1.to_string()),
906+
DiagnosticStyledString::highlighted(t2.to_string()),
907907
)
908908
}
909909
}
@@ -1073,8 +1073,8 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
10731073
}
10741074

10751075
Some((
1076-
DiagnosticStyledString::highlighted(format!("{}", exp_found.expected)),
1077-
DiagnosticStyledString::highlighted(format!("{}", exp_found.found)),
1076+
DiagnosticStyledString::highlighted(exp_found.expected.to_string()),
1077+
DiagnosticStyledString::highlighted(exp_found.found.to_string()),
10781078
))
10791079
}
10801080

src/librustc/infer/error_reporting/nice_region_error/static_impl_trait.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl<'a, 'gcx, 'tcx> NiceRegionError<'a, 'gcx, 'tcx> {
5757
let lifetime_name = match sup_r {
5858
RegionKind::ReFree(FreeRegion {
5959
bound_region: BoundRegion::BrNamed(_, ref name), ..
60-
}) => format!("{}", name),
60+
}) => name.to_string(),
6161
_ => "'_".to_owned(),
6262
};
6363
if let Ok(snippet) = self.tcx.sess.codemap().span_to_snippet(return_sp) {

src/librustc/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2093,7 +2093,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
20932093

20942094
// When printing regions, add trailing space if necessary.
20952095
let region = if ppaux::verbose() || ppaux::identify_regions() {
2096-
let mut region = format!("{}", region);
2096+
let mut region = region.to_string();
20972097
if region.len() > 0 {
20982098
region.push(' ');
20992099
}

src/librustc/session/code_stats.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,8 @@ impl CodeStats {
135135
let VariantInfo { ref name, kind: _, align: _, size, ref fields } = *variant_info;
136136
let indent = if !struct_like {
137137
let name = match name.as_ref() {
138-
Some(name) => format!("{}", name),
139-
None => format!("{}", i),
138+
Some(name) => name.to_owned(),
139+
None => i.to_string(),
140140
};
141141
println!("print-type-size {}variant `{}`: {} bytes",
142142
indent, name, size - discr_size);

src/librustc/traits/error_reporting.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
543543
&data.parent_trait_ref);
544544
match self.get_parent_trait_ref(&data.parent_code) {
545545
Some(t) => Some(t),
546-
None => Some(format!("{}", parent_trait_ref.skip_binder().self_ty())),
546+
None => Some(parent_trait_ref.skip_binder().self_ty().to_string()),
547547
}
548548
}
549549
_ => None,
@@ -797,12 +797,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
797797
ty::TypeVariants::TyTuple(ref tys) => ArgKind::Tuple(
798798
Some(span),
799799
tys.iter()
800-
.map(|ty| ("_".to_owned(), format!("{}", ty.sty)))
800+
.map(|ty| ("_".to_owned(), ty.sty.to_string()))
801801
.collect::<Vec<_>>()
802802
),
803-
_ => ArgKind::Arg("_".to_owned(), format!("{}", t.sty)),
803+
_ => ArgKind::Arg("_".to_owned(), t.sty.to_string()),
804804
}).collect(),
805-
ref sty => vec![ArgKind::Arg("_".to_owned(), format!("{}", sty))],
805+
ref sty => vec![ArgKind::Arg("_".to_owned(), sty.to_string())],
806806
};
807807
if found.len() == expected.len() {
808808
self.report_closure_arg_mismatch(span,
@@ -989,7 +989,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
989989
}) => {
990990
(self.tcx.sess.codemap().def_span(span),
991991
fields.iter().map(|field| {
992-
ArgKind::Arg(format!("{}", field.ident), "_".to_string())
992+
ArgKind::Arg(field.ident.to_string(), "_".to_string())
993993
}).collect::<Vec<_>>())
994994
}
995995
hir::map::NodeStructCtor(ref variant_data) => {
@@ -1152,7 +1152,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
11521152
::rustc_target::spec::abi::Abi::Rust
11531153
)
11541154
};
1155-
format!("{}", ty::Binder::bind(sig))
1155+
ty::Binder::bind(sig).to_string()
11561156
}
11571157

11581158
let argument_is_closure = expected_ref.skip_binder().substs.type_at(0).is_closure();
@@ -1575,10 +1575,10 @@ impl ArgKind {
15751575
ty::TyTuple(ref tys) => ArgKind::Tuple(
15761576
None,
15771577
tys.iter()
1578-
.map(|ty| ("_".to_owned(), format!("{}", ty.sty)))
1578+
.map(|ty| ("_".to_owned(), ty.sty.to_string()))
15791579
.collect::<Vec<_>>()
15801580
),
1581-
_ => ArgKind::Arg("_".to_owned(), format!("{}", t.sty)),
1581+
_ => ArgKind::Arg("_".to_owned(), t.sty.to_string()),
15821582
}
15831583
}
15841584
}

src/librustc/ty/item_path.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
315315
ty::TyUint(_) |
316316
ty::TyFloat(_) |
317317
ty::TyStr => {
318-
buffer.push(&format!("{}", self_ty));
318+
buffer.push(&self_ty.to_string());
319319
}
320320

321321
_ => {

src/librustc/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ impl AssociatedItem {
225225
// late-bound regions, and we don't want method signatures to show up
226226
// `as for<'r> fn(&'r MyType)`. Pretty-printing handles late-bound
227227
// regions just fine, showing `fn(&MyType)`.
228-
format!("{}", tcx.fn_sig(self.def_id).skip_binder())
228+
tcx.fn_sig(self.def_id).skip_binder().to_string()
229229
}
230230
ty::AssociatedKind::Type => format!("type {};", self.ident),
231231
ty::AssociatedKind::Existential => format!("existential type {};", self.ident),

src/librustc/util/common.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ pub fn to_readable_str(mut val: usize) -> String {
242242
val /= 1000;
243243

244244
if val == 0 {
245-
groups.push(format!("{}", group));
245+
groups.push(group.to_string());
246246
break;
247247
} else {
248248
groups.push(format!("{:03}", group));

0 commit comments

Comments
 (0)