Why you should like sync.Pool?

Comparing pool performance. Less than better.

Ok. What the hell?

The comments on sync/pool.go  say that:A Pool is a set of temporary objects that may be individually saved and retrieved.A Pool is safe for use by multiple goroutines simultaneously.

sync.Pool usage

var bufferPool = sync.Pool{
New: func() interface{} {
return new(bytes.Buffer)
},
}
buffer := bufferPool.Get().(*bytes.Buffer)
buffer.Reset()
bufferPool.Put(buffer)

Benchmarking

Encoding json to the bytes.Buffer

// That code tried to json encode structureBenchmarkReadStreamWithPool-8        5000000        384 ns/op        0 B/op        0 allocs/op
BenchmarkReadStreamWithoutPool-8 3000000 554 ns/op 160 B/op 2 allocs/op

Writing bytes to bufio.Writer

BenchmarkWriteBufioWithPool-8       10000000        123 ns/op      128 B/op        2 allocs/op
BenchmarkWriteBufioWithoutPool-8 2000000 651 ns/op 4288 B/op 4 allocs/op

Decoding json to struct

BenchmarkJsonDecodeWithPool-8        1000000       1729 ns/op     1128 B/op        8 allocs/op
BenchmarkJsonDecodeWithoutPool-8 1000000 1751 ns/op 1160 B/op 9 allocs/op

Gzip bytes

BenchmarkWriteGzipWithPool-8          500000       2339 ns/op      162 B/op        2 allocs/op
BenchmarkWriteGzipWithoutPool-8 10000 105288 ns/op 807088 B/op 16 allocs/op

Conclusion

--

--

Software Developer from VK

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store