about summary refs log tree commit diff
path: root/src/libcoretest
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2017-02-03 20:09:36 +0000
committerbors <bors@rust-lang.org>2017-02-03 20:09:36 +0000
commit86d9ed6c82c6745fec46b9ecf2fa91be7924dd16 (patch)
tree00bac1c7b714a59f1e4f230a64cb5427de2b62eb /src/libcoretest
parentaed6410a7b0f15dc68536e0735787436526ba395 (diff)
parent0267529681e2fac6ef4560afe7d8d439d04e6303 (diff)
downloadrust-86d9ed6c82c6745fec46b9ecf2fa91be7924dd16.tar.gz
rust-86d9ed6c82c6745fec46b9ecf2fa91be7924dd16.zip
Auto merge of #39356 - krdln:format-with-capacity, r=aturon
Use `String::with_capacity` in `format!`

Add an `Arguments::estimated_capacity` to estimate the length of formatted text and use it in `std::fmt::format` as the initial capacity of the buffer.

The capacity is calculated based on the literal parts of format string, see the details in the implementation.

Some benches:
```rust
empty:       format!("{}", black_box(""))
literal:     format!("Literal")
long:        format!("Hello Hello Hello Hello, {}!", black_box("world"))
long_rev:    format!("{}, hello hello hello hello!", black_box("world"))
long_rev_2:  format!("{}{}, hello hello hello hello!", 1, black_box("world"))
short:       format!("Hello, {}!", black_box("world"))
short_rev:   format!("{}, hello!", black_box("world"))
short_rev_2: format!("{}{}, hello!", 1, black_box("world"))
surround:    format!("aaaaa{}ccccc{}eeeee", black_box("bbbbb"), black_box("eeeee"))
two_spaced:  format!("{} {}", black_box("bbbbb"), black_box("eeeee"))
worst_case:  format!("{} a long piece...", black_box("and even longer argument. not sure why it has to be so long"))
```
```
 empty        25            28                      3   12.00%
 literal      35            29                     -6  -17.14%
 long         80            46                    -34  -42.50%
 long_rev     79            45                    -34  -43.04%
 long_rev_2   111           66                    -45  -40.54%
 short        73            46                    -27  -36.99%
 short_rev    74            76                      2    2.70%
 short_rev_2  107           108                     1    0.93%
 surround     142           65                    -77  -54.23%
 two_spaced   111           115                     4    3.60%
 worst_case   89            101                    12   13.48%
```
Diffstat (limited to 'src/libcoretest')
-rw-r--r--src/libcoretest/fmt/mod.rs10
-rw-r--r--src/libcoretest/lib.rs1
2 files changed, 11 insertions, 0 deletions
diff --git a/src/libcoretest/fmt/mod.rs b/src/libcoretest/fmt/mod.rs
index ed33596e1c2..5d204c7d523 100644
--- a/src/libcoretest/fmt/mod.rs
+++ b/src/libcoretest/fmt/mod.rs
@@ -28,3 +28,13 @@ fn test_pointer_formats_data_pointer() {
     assert_eq!(format!("{:p}", s), format!("{:p}", s.as_ptr()));
     assert_eq!(format!("{:p}", b), format!("{:p}", b.as_ptr()));
 }
+
+#[test]
+fn test_estimated_capacity() {
+    assert_eq!(format_args!("").estimated_capacity(), 0);
+    assert_eq!(format_args!("{}", "").estimated_capacity(), 0);
+    assert_eq!(format_args!("Hello").estimated_capacity(), 5);
+    assert_eq!(format_args!("Hello, {}!", "").estimated_capacity(), 16);
+    assert_eq!(format_args!("{}, hello!", "World").estimated_capacity(), 0);
+    assert_eq!(format_args!("{}. 16-bytes piece", "World").estimated_capacity(), 32);
+}
diff --git a/src/libcoretest/lib.rs b/src/libcoretest/lib.rs
index 87f3afd6889..e06b757691e 100644
--- a/src/libcoretest/lib.rs
+++ b/src/libcoretest/lib.rs
@@ -34,6 +34,7 @@
 #![feature(ordering_chaining)]
 #![feature(ptr_unaligned)]
 #![feature(move_cell)]
+#![feature(fmt_internals)]
 
 extern crate core;
 extern crate test;