hermanschaaf/go-string-concat-benchmarks — pull request #3 by willglynn (2014-12-14 16:39:52+00:00)
Title: Add benchmarkJoinSize which pre-allocates the []string slice

`benchmarkJoin()` naïvely `append()`s strings onto a `[]string`. When the underlying slice reaches capacity, it is reallocated.

`benchmarkJoinSize()` uses numConcat to size the slice's initial capacity. This has different effects based on the relationship of this capacity number and the actual number of strings:

* If the slice is too small, it will be grown by `append()`, ultimately degenerating to `benchmarkJoin()`'s behavior.
* If the slice is too large, memory is wasted proportional to the overage.
* If the slice is sized correctly, there is no penalty at all.

This method is therefore useful outside synthetic benchmarks, in any situations where string concatenation speed is important and the caller has a guess as to the number of strings needing to be joined.

`benchmarkJoinSize` beats every other method at every size in terms of allocs/op. On my system, it's also the fastest in terms of ns/op at each size except 10000, where it loses to `BufferString` by 14%.

An updated benchmark is in my tree as 851b1e0e96bf0a7294e8b2e528cca3a96ed69c61, but is not included in this pull request.