Skip to content

Commit ed6edce

Browse files
committed
api: added future/response releasing methods
Added method Release to Future and Response's interface, that allows to free used data directly by calling. Used to reduce allocations in creating and using SelectResponse. Closes #493
1 parent 68a7cd9 commit ed6edce

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

request.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -620,11 +620,11 @@ func (req *SelectRequest) Context(ctx context.Context) *SelectRequest {
620620

621621
// Response creates a response for the SelectRequest.
622622
func (req *SelectRequest) Response(header Header, body io.Reader) (Response, error) {
623-
baseResp, err := createBaseResponse(header, body)
623+
SelectResp, err := createSelectResponse(header, body)
624624
if err != nil {
625625
return nil, err
626626
}
627-
return &SelectResponse{baseResponse: *baseResp}, nil
627+
return SelectResp, nil
628628
}
629629

630630
// InsertRequest helps you to create an insert request object for execution

response.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package tarantool
33
import (
44
"fmt"
55
"io"
6+
"sync"
67

78
"github.com/tarantool/go-iproto"
89
"github.com/vmihailenco/msgpack/v5"
@@ -676,9 +677,38 @@ func (resp *baseResponse) Header() Header {
676677
return resp.header
677678
}
678679

680+
var selectResponsePool *sync.Pool = &sync.Pool{
681+
New: func() interface{} {
682+
return &SelectResponse{}
683+
},
684+
}
685+
686+
func createSelectResponse(header Header, body io.Reader) (*SelectResponse, error) {
687+
resp := selectResponsePool.Get().(*SelectResponse)
688+
if body == nil {
689+
resp.header = header
690+
return resp, nil
691+
}
692+
if buf, ok := body.(*smallBuf); ok {
693+
resp.header = header
694+
resp.buf.b = buf.b
695+
resp.buf.p = buf.p
696+
return resp, nil
697+
}
698+
data, err := io.ReadAll(body)
699+
if err != nil {
700+
return resp, err
701+
}
702+
resp.header = header
703+
resp.buf.b = data
704+
return resp, nil
705+
}
706+
679707
func (resp *SelectResponse) Release() {
680708
resp.baseResponse.Release()
681709
resp.pos = nil
710+
711+
selectResponsePool.Put(resp)
682712
}
683713

684714
// Pos returns a position descriptor of the last selected tuple for the SelectResponse.

0 commit comments

Comments
 (0)