forked from btcsuite/btclog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuffer.go
51 lines (41 loc) · 1.34 KB
/
buffer.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// Copyright 2022 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Adapted from go/src/log/slog/internal/buffer.go
package btclog
import "sync"
type buffer []byte
// bufferPool defines a concurrent safe free list of byte slices used to provide
// temporary buffers for formatting log messages prior to outputting them.
var bufferPool = sync.Pool{
New: func() any {
b := make([]byte, 0, 1024)
return (*buffer)(&b)
},
}
// newBuffer returns a byte slice from the free list. A new buffer is allocated
// if there are not any available on the free list. The returned byte slice
// should be returned to the fee list by using the recycleBuffer function when
// the caller is done with it.
func newBuffer() *buffer {
return bufferPool.Get().(*buffer)
}
// free puts the provided byte slice, which should have been obtained via the
// newBuffer function, back on the free list.
func (b *buffer) free() {
// To reduce peak allocation, return only smaller buffers to the pool.
const maxBufferSize = 16 << 10
if cap(*b) <= maxBufferSize {
*b = (*b)[:0]
bufferPool.Put(b)
}
}
func (b *buffer) writeByte(p byte) {
*b = append(*b, p)
}
func (b *buffer) writeBytes(p []byte) {
*b = append(*b, p...)
}
func (b *buffer) writeString(s string) {
*b = append(*b, s...)
}