Skip to content

api: updated response/future methods#501

Open
ermyar wants to merge 1 commit intomasterfrom
ermyar/gh-493-reduce-reader-allocations
Open

api: updated response/future methods#501
ermyar wants to merge 1 commit intomasterfrom
ermyar/gh-493-reduce-reader-allocations

Conversation

@ermyar
Copy link
Collaborator

@ermyar ermyar commented Nov 24, 2025

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 slices of bytes and buffers. Concretely, reducing allocs in read part and buffer allocation of reader function.

Fixes #493

I didn't forget about (remove if it is not applicable):

@ermyar ermyar requested a review from oleg-jukovec November 24, 2025 11:20
@ermyar ermyar force-pushed the ermyar/gh-493-reduce-reader-allocations branch from 1f3999a to e37fa1b Compare November 25, 2025 15:56
@ermyar ermyar marked this pull request as ready for review November 25, 2025 16:25
@ermyar ermyar force-pushed the ermyar/gh-493-reduce-reader-allocations branch from e37fa1b to bf0bbdc Compare November 25, 2025 16:31
@ermyar ermyar requested a review from bigbes November 28, 2025 10:08
Copy link
Collaborator

@oleg-jukovec oleg-jukovec left a comment

Choose a reason for hiding this comment

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

Thank you for the patch!

See issues of the current implementation, fix them and we will try again.

@ermyar ermyar force-pushed the ermyar/gh-493-reduce-reader-allocations branch 2 times, most recently from a15281c to 078cb7d Compare December 1, 2025 22:56
@ermyar ermyar requested a review from oleg-jukovec December 1, 2025 23:26
@ermyar ermyar force-pushed the ermyar/gh-493-reduce-reader-allocations branch 2 times, most recently from beda545 to 50c8953 Compare December 2, 2025 01:50
@ermyar ermyar force-pushed the ermyar/gh-493-reduce-reader-allocations branch from 50c8953 to 553c00f Compare December 5, 2025 14:20
@ermyar ermyar requested a review from bigbes December 8, 2025 19:07
@ermyar ermyar force-pushed the ermyar/gh-493-reduce-reader-allocations branch from 553c00f to 3540852 Compare December 19, 2025 13:28
@bigbes
Copy link
Collaborator

bigbes commented Jan 21, 2026

Actualize PR/commit title/description.
Right now "api: updated response/future methods" isn't very descriptive and i'm not sure, that "Connection now contains sync.Pool that was used to reduce allocations." isn't correct, as I've understand.

@ermyar ermyar force-pushed the ermyar/gh-493-reduce-reader-allocations branch from 3540852 to 90195f7 Compare February 10, 2026 15:37
}

func (resp *MockResponse) Release() {
// Releasing MockResponse data.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
// Releasing MockResponse data.
// Nothing to do.

Comment on lines +235 to +243
var err error

conn := test_helpers.ConnectWithValidation(b, dialer, opts)
defer conn.Close()

_, err = conn.Do(
NewReplaceRequest(spaceNo).
Tuple([]interface{}{uint(1111), "hello", "world"}),
).Get()
Copy link
Collaborator

Choose a reason for hiding this comment

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

This makes the test a bit shorter.

Suggested change
var err error
conn := test_helpers.ConnectWithValidation(b, dialer, opts)
defer conn.Close()
_, err = conn.Do(
NewReplaceRequest(spaceNo).
Tuple([]interface{}{uint(1111), "hello", "world"}),
).Get()
conn := test_helpers.ConnectWithValidation(b, dialer, opts)
defer conn.Close()
_, err := conn.Do(
NewReplaceRequest(spaceNo).
Tuple([]interface{}{uint(1111), "hello", "world"}),
).Get()

connection.go Outdated
buf := smallBuf{b: respBytes}
header, code, err := decodeHeader(conn.dec, &buf)

buf.tinyBuf = *respBytes
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems like a separate structure for the buffer is unnecessary, since we're essentially interested in a slice here.

Especially since the tinybuf is passed by value anyway, and the original pointer is lost anyway.

So I've propose do not create a separate structure for the purpose since it makes no sense. We already can use slices.

Suggested change
buf.tinyBuf = *respBytes
buf.b = respBytes

response.go Outdated
}

func (resp *baseResponse) Release() {
slicePool.Put(&resp.buf.tinyBuf)
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to release the resource only if the buffer is actually allocated by ourselves in the branch:

go-tarantool/response.go

Lines 44 to 46 in 90195f7

resp.header = header
resp.buf.b = buf.b
resp.buf.p = buf.p

So we need some flag in the baseResponse structure.

Suggested change
slicePool.Put(&resp.buf.tinyBuf)
if resp.allocated {
slicePool.Put(&resp.buf.tinyBuf)
}

response.go Outdated
Comment on lines +687 to +720
var selectResponsePool *sync.Pool = &sync.Pool{
New: func() interface{} {
return &SelectResponse{}
},
}

func createSelectResponse(header Header, body io.Reader) (*SelectResponse, error) {
resp := selectResponsePool.Get().(*SelectResponse)
if body == nil {
resp.header = header
return resp, nil
}
if buf, ok := body.(*smallBuf); ok {
resp.header = header
resp.buf.b = buf.b
resp.buf.p = buf.p
return resp, nil
}
data, err := io.ReadAll(body)
if err != nil {
return resp, err
}
resp.header = header
resp.buf.b = data
return resp, nil
}

func (resp *SelectResponse) Release() {
resp.baseResponse.Release()
resp.baseResponse = baseResponse{}
resp.pos = nil

selectResponsePool.Put(resp)
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nice, but let split the PR into two parts:

  1. With the reader() buffers optimization.
  2. With the requests optimization (the selected part).

It would be much easier to merge it one by one.

@oleg-jukovec
Copy link
Collaborator

Please, rebase & update the PR.

@oleg-jukovec
Copy link
Collaborator

@oleg-jukovec
Copy link
Collaborator

@ermyar ermyar force-pushed the ermyar/gh-493-reduce-reader-allocations branch from 90195f7 to 783a2c9 Compare February 24, 2026 06:27
Added method Release to Future and Response's interface,
that allows to free used data directly by calling.
Used to reduce allocations in `reader` function.

Closes #493
@ermyar ermyar force-pushed the ermyar/gh-493-reduce-reader-allocations branch from 783a2c9 to b2dbe37 Compare February 24, 2026 06:40
@ermyar ermyar requested a review from oleg-jukovec February 24, 2026 06:50
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

v3: design API to avoid allocations in responses

3 participants