aboutsummaryrefslogtreecommitdiff
path: root/internal/toolkit/toolkit.go
blob: f87a496f56bac8decee9d2fe7dc3e91c015bee91 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// Package toolkit contains useful, general-purpose utilities
package toolkit

import (
	"bytes"
	"sync"
)

var bufPool = sync.Pool{
	New: func() any {
		return new(bytes.Buffer)
	},
}

// GetBuffer returns an empty buffer, along with a function which can be used to
// return it to a global pool, which helps reduce allocations.
func GetBuffer() (*bytes.Buffer, func()) {
	buf := bufPool.Get().(*bytes.Buffer)
	return buf, func() {
		buf.Reset()
		bufPool.Put(buf)
	}
}