Skip to content

api: added future/response releasing methods#543

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

api: added future/response releasing methods#543
ermyar wants to merge 1 commit intomasterfrom
ermyar/gh-493-reduce-read-allocations

Conversation

@ermyar
Copy link
Collaborator

@ermyar ermyar commented Mar 3, 2026

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. Concretely, reducing alloc's in read part.

Fixes #493

@ermyar ermyar requested a review from oleg-jukovec March 3, 2026 14:55
@ermyar ermyar linked an issue Mar 3, 2026 that may be closed by this pull request
Base automatically changed from ermyar/gh-493-reduce-reader-allocations to master March 5, 2026 12:59
@oleg-jukovec
Copy link
Collaborator

Please, rebase the PR.

@ermyar ermyar force-pushed the ermyar/gh-493-reduce-read-allocations branch from e61434b to 05bb9da Compare March 5, 2026 13:34
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.

Thanks for the patch!

Except for the comments, everything is fine here. But let's improve the solution a bit here before merge.

Let's use the approach from the grpc-go:

https://github.com/grpc/grpc-go/blob/master/mem/buffer_pool.go

Create multiple buffers with constant byte sizes instead of the single one. Response that does not fit into these buffers should be allocated separately without a pool and without a release.

It shouldn't be copy-paste, but inspirated the go-grpc's code.

Another option is that you can correct the current comments and we will merge the PR, and you will implement logic with buffer pools in another one.

response.go Outdated
Comment on lines +690 to +710
func createSelectResponse(header Header, body io.Reader) (*SelectResponse, error) {
resp := &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
resp.buf.ptr = buf.ptr
return resp, nil
}
data, err := io.ReadAll(body)
if err != nil {
return resp, err
}
resp.header = header
resp.buf.b = data
return resp, nil
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

The code shouldn't be in the PR. It should be in the #541 only.

Copy link
Collaborator Author

@ermyar ermyar Mar 5, 2026

Choose a reason for hiding this comment

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

Okay, it was used there to reduce buf allocation. It may be moved to #541

@ermyar ermyar force-pushed the ermyar/gh-493-reduce-read-allocations branch 5 times, most recently from 0bfee57 to 0d3cbe1 Compare March 12, 2026 00:30
@ermyar ermyar requested review from bigbes and oleg-jukovec March 12, 2026 06:35

// getInd returns least index of size slice,
// which is great or equal given size.
func (p *pooler) getInd(size int) int {
Copy link
Collaborator

Choose a reason for hiding this comment

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

if size == 0

slice_pool.go Outdated
}

// getInd returns least index of size slice,
// which is great or equal given size.
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
// which is great or equal given size.
// which is greater or equal given size.

slice_pool.go Outdated
}

// getSlice returns pointer of byte slice of given size,
// also clear returning array.
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
// also clear returning array.
// also clear returning slice.

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-read-allocations branch from 0d3cbe1 to 322ad09 Compare March 12, 2026 15:52
@ermyar ermyar requested a review from bigbes March 12, 2026 18:04
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