github.com/weedge/lib@v0.0.0-20230424045628-a36dcc1d90e4/pool/bufferpool/readme.md (about) 1 ##### 介绍 2 3 通过sync.Pool池化复用buffer, 使用场景:大量临时对象append操作,编码解码操作等,通过复用减少gc 4 5 ##### 对比 6 7 ```shell 8 go test -bench=. -run=none -benchmem=1 9 goos: darwin 10 goarch: amd64 11 pkg: github.com/weedge/lib/pool/bufferpool 12 Benchmark_TestStringAppend-8 328002 3629 ns/op 28672 B/op 4 allocs/op 13 Benchmark_TestBufferPool-8 7447075 157 ns/op 0 B/op 0 allocs/op 14 Benchmark_TestBuffer-8 300680 3643 ns/op 25216 B/op 3 allocs/op 15 BenchmarkJI_TestBufferPool_Parallel-8 32020629 34.6 ns/op 0 B/op 0 allocs/op 16 BenchmarkJI_TestBuffer_Parallel-8 315584 3517 ns/op 25216 B/op 3 allocs/op 17 BenchmarkJI_TestStringAppend_Parallel-8 271797 4231 ns/op 28672 B/op 4 allocs/op 18 ``` 19 20 总结: 使用池化buffer, 减少了对象分配次数,减少gc 21 22 Tips: buffer 在复用资源过程中,资源在逐渐增大,一直复用,也会导致资源消耗过多,到了一定大小之后,应该通过系统释放掉,参考使用 [bytebufferpool](https://github.com/valyala/bytebufferpool)