Skip to content

Commit

Permalink
when buffer gets resized, store the resized buffer in the pool (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
lovromazgon authored Oct 18, 2024
1 parent c7e3a6c commit 306d09f
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions wasm/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@ var bufferPool = sync.Pool{

func NextCommand(cmdReq *processorv1.CommandRequest) error {
buffer := bufferPool.Get().([]byte)
defer bufferPool.Put(buffer)
defer func() {
bufferPool.Put(buffer)
}()

buffer, cmdSize, err := hostCall(_commandRequest, buffer[:cap(buffer)])
if err != nil {
Expand All @@ -49,12 +51,14 @@ func NextCommand(cmdReq *processorv1.CommandRequest) error {

func Reply(resp *processorv1.CommandResponse) error {
buffer := bufferPool.Get().([]byte)
defer bufferPool.Put(buffer)
defer func() {
bufferPool.Put(buffer)
}()

buffer, err := proto.MarshalOptions{}.MarshalAppend(buffer[:0], resp)
if err != nil {
return fmt.Errorf("failed marshalling proto type into bytes: %w", err)
}
_, _, err = hostCall(_commandResponse, buffer)
buffer, _, err = hostCall(_commandResponse, buffer)
return err
}

0 comments on commit 306d09f

Please sign in to comment.