all: Use new Go 1.19 atomic types (#8772)
This commit is contained in:
+10
-10
@@ -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)
|
||||
}
|
||||
|
||||
@@ -108,13 +108,13 @@ func TestStressBufferPool(t *testing.T) {
|
||||
default:
|
||||
}
|
||||
|
||||
t.Log(bp.puts, bp.skips, bp.misses, bp.hits)
|
||||
if bp.puts == 0 || bp.skips == 0 || bp.misses == 0 {
|
||||
t.Log(bp.puts.Load(), bp.skips.Load(), bp.misses.Load(), bp.hits)
|
||||
if bp.puts.Load() == 0 || bp.skips.Load() == 0 || bp.misses.Load() == 0 {
|
||||
t.Error("didn't exercise some paths")
|
||||
}
|
||||
var hits int64
|
||||
for _, h := range bp.hits {
|
||||
hits += h
|
||||
for i := range bp.hits {
|
||||
hits += bp.hits[i].Load()
|
||||
}
|
||||
if hits == 0 {
|
||||
t.Error("didn't exercise some paths")
|
||||
|
||||
+17
-21
@@ -10,53 +10,49 @@ import (
|
||||
|
||||
type countingReader struct {
|
||||
io.Reader
|
||||
tot int64 // bytes (atomic, must remain 64-bit aligned)
|
||||
last int64 // unix nanos (atomic, must remain 64-bit aligned)
|
||||
tot atomic.Int64 // bytes
|
||||
last atomic.Int64 // unix nanos
|
||||
}
|
||||
|
||||
var (
|
||||
totalIncoming int64
|
||||
totalOutgoing int64
|
||||
totalIncoming atomic.Int64
|
||||
totalOutgoing atomic.Int64
|
||||
)
|
||||
|
||||
func (c *countingReader) Read(bs []byte) (int, error) {
|
||||
n, err := c.Reader.Read(bs)
|
||||
atomic.AddInt64(&c.tot, int64(n))
|
||||
atomic.AddInt64(&totalIncoming, int64(n))
|
||||
atomic.StoreInt64(&c.last, time.Now().UnixNano())
|
||||
c.tot.Add(int64(n))
|
||||
totalIncoming.Add(int64(n))
|
||||
c.last.Store(time.Now().UnixNano())
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (c *countingReader) Tot() int64 {
|
||||
return atomic.LoadInt64(&c.tot)
|
||||
}
|
||||
func (c *countingReader) Tot() int64 { return c.tot.Load() }
|
||||
|
||||
func (c *countingReader) Last() time.Time {
|
||||
return time.Unix(0, atomic.LoadInt64(&c.last))
|
||||
return time.Unix(0, c.last.Load())
|
||||
}
|
||||
|
||||
type countingWriter struct {
|
||||
io.Writer
|
||||
tot int64 // bytes (atomic, must remain 64-bit aligned)
|
||||
last int64 // unix nanos (atomic, must remain 64-bit aligned)
|
||||
tot atomic.Int64 // bytes
|
||||
last atomic.Int64 // unix nanos
|
||||
}
|
||||
|
||||
func (c *countingWriter) Write(bs []byte) (int, error) {
|
||||
n, err := c.Writer.Write(bs)
|
||||
atomic.AddInt64(&c.tot, int64(n))
|
||||
atomic.AddInt64(&totalOutgoing, int64(n))
|
||||
atomic.StoreInt64(&c.last, time.Now().UnixNano())
|
||||
c.tot.Add(int64(n))
|
||||
totalOutgoing.Add(int64(n))
|
||||
c.last.Store(time.Now().UnixNano())
|
||||
return n, err
|
||||
}
|
||||
|
||||
func (c *countingWriter) Tot() int64 {
|
||||
return atomic.LoadInt64(&c.tot)
|
||||
}
|
||||
func (c *countingWriter) Tot() int64 { return c.tot.Load() }
|
||||
|
||||
func (c *countingWriter) Last() time.Time {
|
||||
return time.Unix(0, atomic.LoadInt64(&c.last))
|
||||
return time.Unix(0, c.last.Load())
|
||||
}
|
||||
|
||||
func TotalInOut() (int64, int64) {
|
||||
return atomic.LoadInt64(&totalIncoming), atomic.LoadInt64(&totalOutgoing)
|
||||
return totalIncoming.Load(), totalOutgoing.Load()
|
||||
}
|
||||
|
||||
@@ -468,9 +468,9 @@ func TestWriteCompressed(t *testing.T) {
|
||||
|
||||
hdr := Header{Type: typeOf(msg)}
|
||||
size := int64(2 + hdr.ProtoSize() + 4 + msg.ProtoSize())
|
||||
if c.cr.tot > size {
|
||||
if c.cr.Tot() > size {
|
||||
t.Errorf("compression enlarged message from %d to %d",
|
||||
size, c.cr.tot)
|
||||
size, c.cr.Tot())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user