Skip to content

Commit

Permalink
Revert "rpc_util: standardize buffer pool interface to use []byte in …
Browse files Browse the repository at this point in the history
…Put method"

This reverts commit 1f4bc35.
  • Loading branch information
hueypark committed Jun 27, 2023
1 parent 1f4bc35 commit 3dcd833
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 10 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: 9 additions & 8 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,22 +119,23 @@ 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)

return make([]byte, size)
}

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

func newBytesPool(size int) simpleSharedBufferChildPool {
return &bufferPool{
Pool: sync.Pool{
New: func() interface{} {
return make([]byte, size)
bs := make([]byte, size)
return &bs
},
},
defaultSize: size,
Expand All @@ -149,5 +150,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 3dcd833

Please sign in to comment.