Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rpc_util: Reuse memory buffer for receiving message #5862

Merged
merged 42 commits into from
Jun 27, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
493be5d
rpc_util: add bytes pool for the parser
hueypark Jan 25, 2023
ecdb6d2
rpc_util: handle empty buffer case for copy payInfo.uncompressedBytes
hueypark Jan 25, 2023
cb8827d
rpc_util: Use a pool of bytes with varying sizes for the parser
hueypark Jan 31, 2023
bae37e3
grpc: Add useBytesPoolForParser option
hueypark Jan 31, 2023
cbaef54
rpc_util: Fix a. vet error
hueypark Jan 31, 2023
f9730dd
rpc_util: Copy the buffer only if the pool option is enabled
hueypark Feb 25, 2023
e746250
rpc_util: Rename the option to `useSharedRecvBuffers`
hueypark Feb 25, 2023
711db78
rpc_util: Introduce user created shared recv buffer pool option
hueypark Feb 25, 2023
c05feed
benchmark: Introduce shared receive buffers(nil and simple) feature
hueypark Feb 25, 2023
9390057
Merge branch 'master' into master
hueypark Mar 4, 2023
b1c6496
rpc_util: export a SharedBufferPool
hueypark Mar 15, 2023
086dd28
rpc_util: improve comment for the option function
hueypark Mar 15, 2023
d6c1d97
rpc_util: introduce simple pool implementation for new user
hueypark Mar 15, 2023
1b11bba
rpc_util: add a copyright
hueypark Mar 15, 2023
a44c200
rpc_util: set the default value of the sharedRecvBufferPool to nil
hueypark Mar 15, 2023
6b5000f
rpc_util: add experimental notices to SharedBufferPool
hueypark Mar 15, 2023
c34da60
rpc_util: provide more detail in SharedBufferPool comment
hueypark Mar 15, 2023
acec626
rpc_util: use multiple buckets for the simpleSharedBufferPool
hueypark Mar 15, 2023
3cbb6b5
rpc_util: add pool size comments
hueypark Mar 17, 2023
57b9c67
rpc_util: add a SharedBufferPool function comments
hueypark Mar 17, 2023
76caf74
rpc_util: use preconfigured size buffers for each pool
hueypark Mar 17, 2023
8f33b9b
rpc_util: remove duplicate Put function
hueypark Mar 22, 2023
5155566
rpc_util: add default recv buffer pool with nop functionality
hueypark Apr 9, 2023
15f820e
rpc_util: use array for the simple shared buffer pool
hueypark Apr 9, 2023
539eef3
Merge remote-tracking branch 'upstream/master'
hueypark Apr 9, 2023
056b3e5
rpc_util: use fallback bytes pool for the simple shared buffer pool
hueypark Apr 9, 2023
0d07cfc
rpc_util: remove a wrong test
hueypark Apr 11, 2023
7f6fdb2
Merge remote-tracking branch 'upstream/master'
hueypark Apr 19, 2023
ca9f6de
rpc_util: add limitation comment for the shared buffer pool
hueypark Apr 19, 2023
9be7889
rpc_util: exclude shared prefix from shared recv buffer pool
hueypark Apr 19, 2023
25b60e3
rpc_util: rename NewSimpleSharedBufferPool to NewsimpleSharedBufferPool
hueypark Apr 19, 2023
8e8f683
rpc_util: add a TestRecvBufferPool
hueypark Apr 19, 2023
96f5a27
rpc_util: rename NewsimpleSharedBufferPool to NewSharedBufferPool
hueypark May 20, 2023
fe1294f
rpc_util: merge fallback bytespool functionality into bytepool
hueypark May 20, 2023
5535416
rpc_util: add capacity checks for all buffer pool sizes
hueypark May 23, 2023
2678efb
rpc_util: return unused buffers to memory pool
hueypark May 23, 2023
8199eeb
Merge remote-tracking branch 'upstream/master'
hueypark May 28, 2023
a70df17
Merge remote-tracking branch 'upstream/master'
hueypark May 31, 2023
86d999f
Merge remote-tracking branch 'upstream/master'
hueypark Jun 27, 2023
63a360e
rpc_util: remove a useless newline
hueypark Jun 27, 2023
1f4bc35
rpc_util: standardize buffer pool interface to use []byte in Put method
hueypark Jun 27, 2023
3dcd833
Revert "rpc_util: standardize buffer pool interface to use []byte in …
hueypark Jun 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
rpc_util: add default recv buffer pool with nop functionality
This change eliminates the need for nil checks on the shared recv buffer pool, resulting in a more simplified codebase.
  • Loading branch information
hueypark committed Apr 9, 2023
commit 515556682270ce11a49bea548a2d31ca7eccdbe6
1 change: 1 addition & 0 deletions dialoptions.go
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,7 @@ func defaultDialOptions() dialOptions {
ReadBufferSize: defaultReadBufSize,
UseProxy: true,
},
sharedRecvBufferPool: nopBufferPool{},
}
}

Expand Down
18 changes: 3 additions & 15 deletions rpc_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -613,11 +613,7 @@ func (p *parser) recvMsg(maxReceiveMessageSize int) (pf payloadFormat, msg []byt
if int(length) > maxReceiveMessageSize {
return 0, nil, status.Errorf(codes.ResourceExhausted, "grpc: received message larger than max (%d vs. %d)", length, maxReceiveMessageSize)
}
if p.sharedRecvBufferPool != nil {
msg = p.sharedRecvBufferPool.Get(int(length))
} else {
msg = make([]byte, int(length))
}
msg = p.sharedRecvBufferPool.Get(int(length))
if _, err := p.r.Read(msg); err != nil {
if err == io.EOF {
err = io.ErrUnexpectedEOF
Expand Down Expand Up @@ -805,16 +801,8 @@ func recv(p *parser, c baseCodec, s *transport.Stream, dc Decompressor, m interf
return status.Errorf(codes.Internal, "grpc: failed to unmarshal the received message: %v", err)
}
if payInfo != nil {
if p.sharedRecvBufferPool != nil {
if len(buf) != 0 {
payInfo.uncompressedBytes = make([]byte, len(buf))
copy(payInfo.uncompressedBytes, buf)
}
} else {
payInfo.uncompressedBytes = buf
}
}
if p.sharedRecvBufferPool != nil {
payInfo.uncompressedBytes = buf
} else {
p.sharedRecvBufferPool.Put(&buf)
}
return nil
Expand Down
4 changes: 2 additions & 2 deletions rpc_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ func (s) TestSimpleParsing(t *testing.T) {
{append([]byte{0, 1, 0, 0, 0}, bigMsg...), nil, bigMsg, compressionNone},
} {
buf := fullReader{bytes.NewReader(test.p)}
parser := &parser{r: buf}
parser := &parser{r: buf, sharedRecvBufferPool: nopBufferPool{}}
pt, b, err := parser.recvMsg(math.MaxInt32)
if err != test.err || !bytes.Equal(b, test.b) || pt != test.pt {
t.Fatalf("parser{%v}.recvMsg(_) = %v, %v, %v\nwant %v, %v, %v", test.p, pt, b, err, test.pt, test.b, test.err)
Expand All @@ -77,7 +77,7 @@ func (s) TestMultipleParsing(t *testing.T) {
// Set a byte stream consists of 3 messages with their headers.
p := []byte{0, 0, 0, 0, 1, 'a', 0, 0, 0, 0, 2, 'b', 'c', 0, 0, 0, 0, 1, 'd'}
b := fullReader{bytes.NewReader(p)}
parser := &parser{r: b}
parser := &parser{r: b, sharedRecvBufferPool: nopBufferPool{}}

wantRecvs := []struct {
pt payloadFormat
Expand Down
1 change: 1 addition & 0 deletions server.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ var defaultServerOptions = serverOptions{
connectionTimeout: 120 * time.Second,
writeBufferSize: defaultWriteBufSize,
readBufferSize: defaultReadBufSize,
sharedRecvBufferPool: nopBufferPool{},
}
var globalServerOptions []ServerOption

Expand Down
12 changes: 12 additions & 0 deletions shared_buffer_pool.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,3 +151,15 @@ func makeFallbackBytesPool() bufferPool {
},
}
}

// nopBufferPool is a buffer pool just makes new buffer without pooling.
type nopBufferPool struct {
}

func (nopBufferPool) Get(length int) []byte {
return make([]byte, length)
}

func (nopBufferPool) Put(*[]byte) {

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: nix this newline.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. 63a360e

}