]> Cypherpunks repositories - gostls13.git/commit
strconv: pre-allocate in appendQuotedWith
authorRob Pike <r@golang.org>
Thu, 18 Apr 2019 03:28:55 +0000 (13:28 +1000)
committerRob Pike <r@golang.org>
Thu, 18 Apr 2019 04:30:46 +0000 (04:30 +0000)
commitc226f6432d465ad9e2d21962353ba86834a2afcb
tree5a3c1aba4e0d03a6bea520cdae50fe7df8cbfe8e
parentcbaa8e5f93a9571c30271c0f6d7c874793ec49ce
strconv: pre-allocate in appendQuotedWith

The byte-at-a-time allocation done quoting strings in appendQuotedWith
grows the output incrementally, which is poor behavior for very large
strings. An easy fix is to make sure the buffer has enough room at
least for an unquoted string.

Add a benchmark with a megabyte of non-ASCII data.
Before: 39 allocations.
After: 7 allocations.

We could do better by doing a lot more work but this seems like a big
result for little effort.

Fixes #31472.

Change-Id: I852139e0a2bd13722c4dd329ded8ae1759abad5b
Reviewed-on: https://go-review.googlesource.com/c/go/+/172677
Reviewed-by: Ian Lance Taylor <iant@golang.org>
Run-TryBot: Ian Lance Taylor <iant@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
src/strconv/quote.go
src/strconv/strconv_test.go