api: add PoolAllocator for response buffers#543
Merged
oleg-jukovec merged 2 commits intomasterfrom Apr 7, 2026
Merged
Conversation
Base automatically changed from
ermyar/gh-493-reduce-reader-allocations
to
master
March 5, 2026 12:59
Collaborator
|
Please, rebase the PR. |
e61434b to
05bb9da
Compare
oleg-jukovec
requested changes
Mar 5, 2026
Collaborator
oleg-jukovec
left a comment
There was a problem hiding this comment.
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.
0bfee57 to
0d3cbe1
Compare
bigbes
reviewed
Mar 12, 2026
bigbes
reviewed
Mar 12, 2026
bigbes
reviewed
Mar 12, 2026
0d3cbe1 to
322ad09
Compare
322ad09 to
9501898
Compare
oleg-jukovec
requested changes
Mar 19, 2026
Collaborator
oleg-jukovec
left a comment
There was a problem hiding this comment.
- If a user do not use the
Release()than the commits adds +1 allocation per request. Let the pool be a configurable feature withOpts.enabled by default. - We need to update our code (in pool, queue, crud etc.). To release requests to be consistent with the feature.
9501898 to
0c5a223
Compare
oleg-jukovec
requested changes
Mar 26, 2026
9519cd2 to
27fd5b2
Compare
The commit introduces ability to release resources allocated to read response data. Part of #493
5a31264 to
ed58ed1
Compare
73832e7 to
e5ea54d
Compare
oleg-jukovec
approved these changes
Apr 3, 2026
bigbes
reviewed
Apr 3, 2026
bigbes
reviewed
Apr 3, 2026
bigbes
reviewed
Apr 3, 2026
bigbes
reviewed
Apr 3, 2026
Previously, response buffer allocation was hardcoded with an internal
pooler implementation. Users had no control over memory management
for responses, which could be problematic for applications with
specific memory requirements or custom allocation strategies.
The new Allocator interface allows users to implement custom buffer
allocation and reuse strategies:
```
// Allocator is an interface for allocating and deallocating byte
// slices.
type Allocator interface {
// Get returns a pointer to a byte slice of at least the given
// length.
// The caller should not assume anything about the slice's
// capacity.
//
// If the allocator cannot allocate a buffer (e.g., invalid
// length), it returns nil. The caller must handle this case
// appropriately.
Get(length int) *[]byte
// Put returns a byte slice to the allocator for reuse.
// After calling Put, the caller must not use the slice.
//
// The caller must ensure that the slice length remains unchanged
// between Get and Put calls. Modifying the slice length before
// calling Put may prevent the allocator from properly reusing the
// buffer.
Put(buf *[]byte)
}
```
The PoolAllocator type provides a ready-to-use implementation based
on sync.Pool for power-of-two sized byte slices.
The Opts.Allocator option enables configuring a custom allocator
for a connection. This is useful for applications that need to
optimize memory usage or integrate with custom memory management
systems.
Closes #493
e5ea54d to
1f0b9b9
Compare
bigbes
approved these changes
Apr 7, 2026
patapenka-alexey
approved these changes
Apr 7, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The new Allocator interface allows users to implement custom buffer
allocation and reuse strategies:
The PoolAllocator type provides a ready-to-use implementation based
on sync.Pool for power-of-two sized byte slices.
The Opts.Allocator option enables configuring a custom allocator
for a connection. This is useful for applications that need to
optimize memory usage or integrate with custom memory management
systems.
Closes #493