Comment

Comment by kevinconaway

Most of these techniques sacrifice readability so before implementing them, you should really profile to make sure that the code in question is a hot spot in terms of allocations or CPU usage.

That said, the example using sync.Pool is not quite right. The New function should always return a pointer type to avoid incurring an additional allocation on Get [0]

The code should look like:

  var bufpool = sync.Pool{
    New: func() interface{} {
        buf := make([]byte, 512)
        return &buf
    }}

  b := *bufpool.Get().(*[]byte)

  defer bufpool.Put(&b)
[0] see example in https://golang.org/pkg/sync/#Pool

Discussion

Live from the official Hacker News API

A modern reader for Hacker News · SvelteKit + Skeleton