refactor the error handling and optimize the heap allocation
This commit is contained in:
40
cmd/slicewriter/dummy_test.go
Normal file
40
cmd/slicewriter/dummy_test.go
Normal file
@@ -0,0 +1,40 @@
|
||||
package slicewriter
|
||||
|
||||
import (
|
||||
"sync"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestDummy(t *testing.T) {
|
||||
var (
|
||||
p sync.Pool
|
||||
a, b []byte
|
||||
)
|
||||
|
||||
p = sync.Pool{
|
||||
New: func() any {
|
||||
return make([]byte, 4096)
|
||||
},
|
||||
}
|
||||
|
||||
a = p.Get().([]byte)
|
||||
t.Logf("retrieve a from pool: len %d, cap %d", len(a), cap(a))
|
||||
|
||||
b = p.Get().([]byte)
|
||||
t.Logf("retrieve b from pool: len %d, cap %d", len(b), cap(b))
|
||||
|
||||
a = a[:1024]
|
||||
b = b[:1024]
|
||||
t.Logf("resize a : len %d, cap %d", len(a), cap(a))
|
||||
t.Logf("resize b : len %d, cap %d", len(b), cap(b))
|
||||
|
||||
p.Put(a[:cap(a)])
|
||||
p.Put(b)
|
||||
|
||||
t.Log("after putting back")
|
||||
a = p.Get().([]byte)
|
||||
t.Logf("retrieve a from pool: len %d, cap %d", len(a), cap(a))
|
||||
|
||||
b = p.Get().([]byte)
|
||||
t.Logf("retrieve b from pool: len %d, cap %d", len(b), cap(b))
|
||||
}
|
||||
Reference in New Issue
Block a user