-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbuffer.go
More file actions
268 lines (228 loc) · 5.48 KB
/
Copy pathbuffer.go
File metadata and controls
268 lines (228 loc) · 5.48 KB
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package velocity
import (
"bytes"
"sync"
"time"
"unsafe"
)
// BufferPool manages byte buffers of various sizes for efficient logging output.
// Using a tiered pool reduces garbage collection pressure during high-throughput logging.
type BufferPool struct {
small sync.Pool
medium sync.Pool
large sync.Pool
xlarge sync.Pool
}
const (
// Buffer sizes optimised based on typical log message patterns.
// Sizes are powers of 2 for efficient memory alignment.
bufSmallSize = 512
bufMediumSize = 2048
bufLargeSize = 8192
bufXLargeSize = 32768
HintConsoleLog = 256
HintStructuredLog = 1024
HintStackTrace = 4096
)
var globalBufferPool = NewBufferPool()
func NewBufferPool() *BufferPool {
return &BufferPool{
small: sync.Pool{
New: func() any {
return bytes.NewBuffer(make([]byte, 0, bufSmallSize))
},
},
medium: sync.Pool{
New: func() any {
return bytes.NewBuffer(make([]byte, 0, bufMediumSize))
},
},
large: sync.Pool{
New: func() any {
return bytes.NewBuffer(make([]byte, 0, bufLargeSize))
},
},
xlarge: sync.Pool{
New: func() any {
return bytes.NewBuffer(make([]byte, 0, bufXLargeSize))
},
},
}
}
// Get retrieves a buffer from the pool, selecting the size based on the hint.
func (bp *BufferPool) Get(hint int) *bytes.Buffer {
var buf *bytes.Buffer
switch {
case hint <= 0:
if b, ok := bp.small.Get().(*bytes.Buffer); ok {
buf = b
} else {
buf = bytes.NewBuffer(make([]byte, 0, bufSmallSize))
}
case hint <= bufSmallSize:
if b, ok := bp.small.Get().(*bytes.Buffer); ok {
buf = b
} else {
buf = bytes.NewBuffer(make([]byte, 0, bufSmallSize))
}
case hint <= bufMediumSize:
if b, ok := bp.medium.Get().(*bytes.Buffer); ok {
buf = b
} else {
buf = bytes.NewBuffer(make([]byte, 0, bufMediumSize))
}
case hint <= bufLargeSize:
if b, ok := bp.large.Get().(*bytes.Buffer); ok {
buf = b
} else {
buf = bytes.NewBuffer(make([]byte, 0, bufLargeSize))
}
default:
if b, ok := bp.xlarge.Get().(*bytes.Buffer); ok {
buf = b
} else {
buf = bytes.NewBuffer(make([]byte, 0, bufXLargeSize))
}
}
buf.Reset()
return buf
}
// Put returns a buffer to the appropriate pool based on its capacity.
// Buffers are not resized to preserve their grown capacity for reuse.
func (bp *BufferPool) Put(buf *bytes.Buffer) {
if buf == nil {
return
}
// Don't pool buffers that grew beyond xlarge size
capacity := buf.Cap()
if capacity > bufXLargeSize*2 {
return
}
switch {
case capacity <= bufSmallSize:
bp.small.Put(buf)
case capacity <= bufMediumSize:
bp.medium.Put(buf)
case capacity <= bufLargeSize:
bp.large.Put(buf)
default:
bp.xlarge.Put(buf)
}
}
func GetBuffer(hint int) *bytes.Buffer {
return globalBufferPool.Get(hint)
}
func PutBuffer(buf *bytes.Buffer) {
globalBufferPool.Put(buf)
}
// ByteSlice provides zero-copy string to byte conversion.
// SAFETY: The returned slice must not be modified.
func ByteSlice(s string) []byte {
return unsafe.Slice(unsafe.StringData(s), len(s))
}
// UnsafeString provides zero-copy byte to string conversion.
// SAFETY: The byte slice must not be modified after conversion.
func UnsafeString(b []byte) string {
if len(b) == 0 {
return ""
}
return unsafe.String(&b[0], len(b))
}
func AppendString(b []byte, s string) []byte {
return append(b, ByteSlice(s)...)
}
type BytesBuffer struct {
buf *bytes.Buffer
}
func NewBytesBuffer(buf *bytes.Buffer) *BytesBuffer {
return &BytesBuffer{buf: buf}
}
func (b *BytesBuffer) WriteString(s string) {
b.buf.Write(ByteSlice(s))
}
func (b *BytesBuffer) WriteByte(c byte) error {
return b.buf.WriteByte(c)
}
func (b *BytesBuffer) WriteInt(i int64) {
var tmp [20]byte
written := formatInt(tmp[:], i)
b.buf.Write(tmp[:written])
}
func (b *BytesBuffer) Write(p []byte) (int, error) {
return b.buf.Write(p)
}
func (b *BytesBuffer) Bytes() []byte {
return b.buf.Bytes()
}
// AppendTime writes a formatted timestamp directly into the buffer,
// avoiding the intermediate string allocation from time.Format.
func (b *BytesBuffer) AppendTime(t time.Time, layout string) {
b.buf.Write(t.AppendFormat(b.buf.AvailableBuffer(), layout))
}
func (b *BytesBuffer) String() string {
return UnsafeString(b.buf.Bytes())
}
type Formatter struct {
buf *bytes.Buffer
}
func NewFormatter(hint int) *Formatter {
return &Formatter{
buf: GetBuffer(hint),
}
}
func (f *Formatter) WriteString(s string) *Formatter {
f.buf.Write(ByteSlice(s))
return f
}
func (f *Formatter) AppendByte(b byte) *Formatter {
_ = f.buf.WriteByte(b)
return f
}
func (f *Formatter) WriteInt(i int64) *Formatter {
var tmp [20]byte
written := formatInt(tmp[:], i)
f.buf.Write(tmp[:written])
return f
}
func (f *Formatter) Bytes() []byte {
return f.buf.Bytes()
}
func (f *Formatter) String() string {
return UnsafeString(f.buf.Bytes())
}
func (f *Formatter) Release() {
PutBuffer(f.buf)
f.buf = nil
}
func formatInt(b []byte, i int64) int {
if i == 0 {
if len(b) > 0 {
b[0] = '0'
return 1
}
return 0
}
neg := i < 0
// Convert to uint64 before negating to handle MinInt64, where -i overflows in int64.
var u uint64
if neg {
u = uint64(-(i + 1)) + 1 //nolint:gosec // G115: two-step abs to avoid MinInt64 overflow, not a value conversion
} else {
u = uint64(i) //nolint:gosec // G115: i is non-negative here, conversion is safe
}
idx := len(b)
for u > 0 && idx > 0 {
idx--
b[idx] = byte(u%10) + '0'
u /= 10
}
if neg && idx > 0 {
idx--
b[idx] = '-'
}
written := len(b) - idx
if idx > 0 {
copy(b, b[idx:])
}
return written
}