diff options
| author | Michał Krasnoborski <mkrdln@gmail.com> | 2017-02-01 23:47:03 +0100 |
|---|---|---|
| committer | Michał Krasnoborski <mkrdln@gmail.com> | 2017-02-01 23:47:03 +0100 |
| commit | 7403ee9d07a1b096e9628871bd97e39f464c3aa5 (patch) | |
| tree | 4863c5d592d5f3843f69431fb49342339ceb2c8d /src/libcore | |
| parent | e74b55b9da5a163e02bbcbf97e7ea24d48742943 (diff) | |
| download | rust-7403ee9d07a1b096e9628871bd97e39f464c3aa5.tar.gz rust-7403ee9d07a1b096e9628871bd97e39f464c3aa5.zip | |
Adjust heuristics to better handle "{}..." format strings.
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/fmt/mod.rs | 21 |
1 files changed, 12 insertions, 9 deletions
diff --git a/src/libcore/fmt/mod.rs b/src/libcore/fmt/mod.rs index ed9c5d3337f..a870b6f88fb 100644 --- a/src/libcore/fmt/mod.rs +++ b/src/libcore/fmt/mod.rs @@ -281,17 +281,20 @@ impl<'a> Arguments<'a> { let pieces_length: W<usize> = self.pieces.iter() .map(|x| W(x.len())).sum(); - // If they are any arguments to format, the string will most likely - // double in size. So we're pre-doubling it here. - let multiplier = if self.args.is_empty() { W(1) } else { W(2) }; - - let capacity = multiplier * pieces_length; - if multiplier == W(2) && (W(1)..W(8)).contains(capacity) { - // Allocations smaller than 8 don't really make sense for String. - 8 + if self.args.is_empty() { + pieces_length.0 + } else if self.pieces[0] == "" && pieces_length < W(16) { + // If the format string starts with an argument, + // don't preallocate anything, unless length + // of pieces is significant. + 0 } else { - capacity.0 + // There are some arguments, so any additional push + // will reallocate the string. To avoid that, + // we're "pre-doubling" the capacity here. + (pieces_length * W(2)).0 } + } } |
