diff options
author | isharipo <iskander.sharipov@intel.com> | 2018-05-15 19:09:49 +0300 |
---|---|---|
committer | Brad Fitzpatrick <bradfitz@golang.org> | 2018-05-15 16:20:14 +0000 |
commit | 3fb3ca083c650b933919d3b6a45268fce339ac38 (patch) | |
tree | b5fcbc51454603aa032d9be02b5392fc9c9d93fd /src/mime | |
parent | 73b5951391a9c206414b2f460a043374b1383b94 (diff) | |
download | go-3fb3ca083c650b933919d3b6a45268fce339ac38.tar.gz go-3fb3ca083c650b933919d3b6a45268fce339ac38.zip |
mime: do a pre-allocation in encodeWord
The preallocated memory size is comparable to bytes.Buffer bootstraping
array length (bytes.Buffer was used before rewrite to strings.Builder).
Without preallocation, encodeWord does more than one allocation for
almost any possible input.
The regression happens because bytes.Buffer did a 80-bytes allocation
at the beginning of encodeWord while strings.Builder did several
smaller allocations (started with cap=0).
Comparison with reported regression:
name old time/op new time/op delta
QEncodeWord-4 781ns ± 1% 593ns ± 1% -24.08% (p=0.008 n=5+5)
name old alloc/op new alloc/op delta
QEncodeWord-4 152B ± 0% 80B ± 0% -47.37% (p=0.008 n=5+5)
name old allocs/op new allocs/op delta
QEncodeWord-4 5.00 ± 0% 2.00 ± 0% -60.00% (p=0.008 n=5+5)
Comparison with buffer solution (like before strings.Builder, but
without sync pool for buffer re-using):
name old time/op new time/op delta
QEncodeWord-4 595ns ± 1% 593ns ± 1% ~ (p=0.460 n=5+5)
name old alloc/op new alloc/op delta
QEncodeWord-4 160B ± 0% 80B ± 0% -50.00% (p=0.008 n=5+5)
name old allocs/op new allocs/op delta
QEncodeWord-4 2.00 ± 0% 2.00 ± 0% ~ (all equal)
We avoid allocation in buf.String(), as expected.
Fixes #25379
Change-Id: I19763f0e593a27390c1a549b86ce6507b489046b
Reviewed-on: https://go-review.googlesource.com/113235
Run-TryBot: Iskander Sharipov <iskander.sharipov@intel.com>
Reviewed-by: Brad Fitzpatrick <bradfitz@golang.org>
Diffstat (limited to 'src/mime')
-rw-r--r-- | src/mime/encodedword.go | 4 |
1 files changed, 4 insertions, 0 deletions
diff --git a/src/mime/encodedword.go b/src/mime/encodedword.go index d73c8f402c..58f60daec4 100644 --- a/src/mime/encodedword.go +++ b/src/mime/encodedword.go @@ -51,6 +51,10 @@ func needsEncoding(s string) bool { // encodeWord encodes a string into an encoded-word. func (e WordEncoder) encodeWord(charset, s string) string { var buf strings.Builder + // Could use a hint like len(s)*3, but that's not enough for cases + // with word splits and too much for simpler inputs. + // 48 is close to maxEncodedWordLen/2, but adjusted to allocator size class. + buf.Grow(48) e.openWord(&buf, charset) if e == BEncoding { |