Skip to content

Commit

Permalink
rpc_util: standardize buffer pool interface to use []byte in Put method
Browse files Browse the repository at this point in the history
  • Loading branch information
hueypark committed Jun 27, 2023
1 parent 63a360e commit 1f4bc35
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 11 deletions.
2 changes: 1 addition & 1 deletion rpc_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ func recv(p *parser, c baseCodec, s *transport.Stream, dc Decompressor, m interf
if payInfo != nil {
payInfo.uncompressedBytes = buf
} else {
p.recvBufferPool.Put(&buf)
p.recvBufferPool.Put(buf)
}
return nil
}
Expand Down
17 changes: 8 additions & 9 deletions shared_buffer_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ type SharedBufferPool interface {
Get(length int) []byte

// Put returns a buffer to the pool.
Put(*[]byte)
Put([]byte)
}

// NewSharedBufferPool creates a simple SharedBufferPool with buckets
Expand Down Expand Up @@ -68,8 +68,8 @@ func (p *simpleSharedBufferPool) Get(size int) []byte {
return p.pools[p.poolIdx(size)].Get(size)
}

func (p *simpleSharedBufferPool) Put(bs *[]byte) {
p.pools[p.poolIdx(cap(*bs))].Put(bs)
func (p *simpleSharedBufferPool) Put(bs []byte) {
p.pools[p.poolIdx(cap(bs))].Put(bs)
}

func (p *simpleSharedBufferPool) poolIdx(size int) int {
Expand Down Expand Up @@ -119,23 +119,22 @@ type bufferPool struct {
}

func (p *bufferPool) Get(size int) []byte {
bs := p.Pool.Get().(*[]byte)
bs := p.Pool.Get().([]byte)

if cap(*bs) < size {
if cap(bs) < size {
p.Pool.Put(bs)

Check failure on line 125 in shared_buffer_pool.go

View workflow job for this annotation

GitHub Actions / tests (vet, 1.20)

argument should be pointer-like to avoid allocations (SA6002)

return make([]byte, size)
}

return (*bs)[:size]
return (bs)[:size]
}

func newBytesPool(size int) simpleSharedBufferChildPool {
return &bufferPool{
Pool: sync.Pool{
New: func() interface{} {
bs := make([]byte, size)
return &bs
return make([]byte, size)
},
},
defaultSize: size,
Expand All @@ -150,5 +149,5 @@ func (nopBufferPool) Get(length int) []byte {
return make([]byte, length)
}

func (nopBufferPool) Put(*[]byte) {
func (nopBufferPool) Put([]byte) {
}
2 changes: 1 addition & 1 deletion shared_buffer_pool_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (s) TestSharedBufferPool(t *testing.T) {
t.Fatalf("Expected buffer of length %d, got %d", l, len(bs))
}

p.Put(&bs)
p.Put(bs)
}
}
}

0 comments on commit 1f4bc35

Please sign in to comment.