-
Notifications
You must be signed in to change notification settings - Fork 492
Add memory pooling for buffers and field maps for improved memory usage and performance #691
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Implements sync.Pool for bytes.Buffer and map[string]*fieldToExec to reduce allocations and memory growth during GraphQL execution. Key improvements: - Buffer pool: 53-87% faster, 50-100% fewer allocations - Field map pool: 53% faster, 83% less memory per operation - GC pressure significantly reduced Changes: - Add internal/exec/pool.go with buffer and field map pooling - Update Execute(), execSelections(), execList() to use pools - Apply pooling to subscription handling - Add comprehensive tests and benchmarks Signed-off-by: Evan Owen <[email protected]> Co-authored-by: Amp <[email protected]>
27f6cc1 to
ec053b1
Compare
| maxBufferCap = 64 * 1024 | ||
| maxFieldMapSize = 128 | ||
| newFieldMapSize = 16 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder if these should be configurable through schema options, in case some projects have special requirements...
| for k := range m { | ||
| delete(m, k) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We replace this by clear maybe?
| for k := range m { | |
| delete(m, k) | |
| } | |
| clear(m) |
| m[string(rune(i))] = &fieldToExec{} | ||
| } | ||
|
|
||
| putFieldMap(m) // Should not be added to pool |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't you check this instead of only putting a comment?
| l := resolver.Len() | ||
| entryouts := make([]bytes.Buffer, l) | ||
| entryouts := make([]*bytes.Buffer, l) | ||
| for i := 0; i < l; i++ { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| for i := 0; i < l; i++ { | |
| for i := range l { |
| func putBuffer(buf *bytes.Buffer) { | ||
| if buf.Cap() > maxBufferCap { | ||
| return | ||
| } | ||
| bufferPool.Put(buf) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| func putBuffer(buf *bytes.Buffer) { | |
| if buf.Cap() > maxBufferCap { | |
| return | |
| } | |
| bufferPool.Put(buf) | |
| } | |
| func putBuffer(buf *bytes.Buffer) { | |
| if buf == nil || buf.Cap() > maxBufferCap { | |
| return | |
| } | |
| bufferPool.Put(buf) | |
| } |
| func putFieldMap(m map[string]*fieldToExec) { | ||
| if len(m) > maxFieldMapSize { | ||
| return | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
| func putFieldMap(m map[string]*fieldToExec) { | |
| if len(m) > maxFieldMapSize { | |
| return | |
| } | |
| func putFieldMap(m map[string]*fieldToExec) { | |
| if m == nil || len(m) > maxFieldMapSize { | |
| return | |
| } |
We've seen some high memory and GC pressure in our production deployment. One culprit seems to be
func (*Request) execSelections, which can create a lot of buffers that have to grow multiple times in large responses.One important optimization we can make here is buffer pooling. This PR implements sync.Pool for bytes.Buffer and map[string]*fieldToExec to reduce allocations, memory growth, and GC pressure during execution.
Changes:
Isolated Benchmarks
Summary:
Real-world Benchmarks (private to our app, includes db i/o)