-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmemory.back.go
More file actions
52 lines (44 loc) · 1.17 KB
/
memory.back.go
File metadata and controls
52 lines (44 loc) · 1.17 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
//go:build !wasm
package fmt
import "sync"
// Reuse Conv objects to eliminate the 53.67% allocation hotspot from newConv()
var convPool = sync.Pool{
New: func() any {
return &Conv{
out: make([]byte, 0, 64),
work: make([]byte, 0, 64),
err: make([]byte, 0, 64),
// TODO: Add bufFmt when struct is updated
}
},
}
// GetConv gets a reusable Conv from the pool
// FIXED: Ensures object is completely clean to prevent race conditions
func GetConv() *Conv {
c := convPool.Get().(*Conv)
// Defensive cleanup: ensure object is completely clean
c.resetAllBuffers()
c.out = c.out[:0]
c.work = c.work[:0]
c.err = c.err[:0]
c.dataPtr = nil
c.kind = K.String
return c
}
// PutConv returns a Conv to the pool after resetting it
func (c *Conv) PutConv() {
// Reset all buffer positions using centralized method
c.resetAllBuffers()
// Clear buffer contents (keep capacity for reuse)
c.out = c.out[:0]
c.work = c.work[:0]
c.err = c.err[:0]
// Reset other fields to default state - only keep dataPtr and Kind
c.dataPtr = nil
c.kind = K.String
convPool.Put(c)
}
// putConv returns a Conv to the pool after resetting it (internal)
func (c *Conv) putConv() {
c.PutConv()
}