all: Use new Go 1.19 atomic types (#8772)

This commit is contained in:
greatroar
2023-02-07 12:07:34 +01:00
committed by GitHub
parent 882b711958
commit 38f2b34d29
19 changed files with 94 additions and 121 deletions

View File

@@ -13,24 +13,24 @@ import (
var BufferPool bufferPool
type bufferPool struct {
puts int64
skips int64
misses int64
puts atomic.Int64
skips atomic.Int64
misses atomic.Int64
pools []sync.Pool
hits []int64 // start of slice allocation is always aligned
hits []atomic.Int64
}
func newBufferPool() bufferPool {
return bufferPool{
pools: make([]sync.Pool, len(BlockSizes)),
hits: make([]int64, len(BlockSizes)),
hits: make([]atomic.Int64, len(BlockSizes)),
}
}
func (p *bufferPool) Get(size int) []byte {
// Too big, isn't pooled
if size > MaxBlockSize {
atomic.AddInt64(&p.skips, 1)
p.skips.Add(1)
return make([]byte, size)
}
@@ -38,13 +38,13 @@ func (p *bufferPool) Get(size int) []byte {
bkt := getBucketForLen(size)
for j := bkt; j < len(BlockSizes); j++ {
if intf := p.pools[j].Get(); intf != nil {
atomic.AddInt64(&p.hits[j], 1)
p.hits[j].Add(1)
bs := *intf.(*[]byte)
return bs[:size]
}
}
atomic.AddInt64(&p.misses, 1)
p.misses.Add(1)
// All pools are empty, must allocate. For very small slices where we
// didn't have a block to reuse, just allocate a small slice instead of
@@ -60,11 +60,11 @@ func (p *bufferPool) Get(size int) []byte {
func (p *bufferPool) Put(bs []byte) {
// Don't buffer slices outside of our pool range
if cap(bs) > MaxBlockSize || cap(bs) < MinBlockSize {
atomic.AddInt64(&p.skips, 1)
p.skips.Add(1)
return
}
atomic.AddInt64(&p.puts, 1)
p.puts.Add(1)
bkt := putBucketForCap(cap(bs))
p.pools[bkt].Put(&bs)
}