Skip to content

Commit 93261e6

Browse files
authored
perf(orch): pool NBD dispatch buffer (#2379)
Use sync.Pool for the 4 MiB dispatch buffer allocated per Handle call. Each NBD connection allocates this buffer for reading request packets; pooling it across connection lifecycles avoids repeated large allocations.
1 parent 8245988 commit 93261e6

1 file changed

Lines changed: 11 additions & 1 deletion

File tree

packages/orchestrator/pkg/sandbox/nbd/dispatch.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,14 @@ var (
3434

3535
var ErrShuttingDown = errors.New("shutting down. Cannot serve any new requests")
3636

37+
var dispatchBufPool = sync.Pool{
38+
New: func() any {
39+
b := make([]byte, dispatchBufferSize)
40+
41+
return &b
42+
},
43+
}
44+
3745
type Provider interface {
3846
storage.SeekableReader
3947
io.WriterAt
@@ -141,7 +149,9 @@ func (d *Dispatch) writeResponse(respError uint32, respHandle uint64, chunk []by
141149
*
142150
*/
143151
func (d *Dispatch) Handle(ctx context.Context) error {
144-
buffer := make([]byte, dispatchBufferSize)
152+
poolBuf := dispatchBufPool.Get().(*[]byte)
153+
defer dispatchBufPool.Put(poolBuf)
154+
buffer := *poolBuf
145155
wp := 0
146156

147157
request := Request{}

0 commit comments

Comments
 (0)