aboutsummaryrefslogtreecommitdiff
path: root/internal/toolkit/toolkit.go
diff options
context:
space:
mode:
Diffstat (limited to 'internal/toolkit/toolkit.go')
-rw-r--r--internal/toolkit/toolkit.go23
1 files changed, 23 insertions, 0 deletions
diff --git a/internal/toolkit/toolkit.go b/internal/toolkit/toolkit.go
new file mode 100644
index 0000000..f87a496
--- /dev/null
+++ b/internal/toolkit/toolkit.go
@@ -0,0 +1,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)
+ }
+}