Optimize memory allocations in query execution#20
Merged
Conversation
This change optimizes the main query execution paths to reduce memory allocations and improve performance. - In `QuerySet.Exec`, the slice for scanning row values is now allocated only once outside the loop. - In `execIntoMaps`, the slice for scannable values is also created only once. - In `resultIterator.Next`, the slice for scan values is now part of the iterator struct and is reused for each row, avoiding reallocation on each call. These changes significantly reduce the garbage collector pressure when processing large result sets.
This change optimizes the main query execution paths to reduce memory allocations and improve performance. - In `QuerySet.Exec`, the slice for scanning row values is now allocated only once outside the loop. - In `resultIterator.Next`, the slice for scan values is now part of the iterator struct and is reused for each row, avoiding reallocation on each call. These changes significantly reduce the garbage collector pressure when processing large result sets. An incorrect optimization for `execIntoMaps` was reverted.
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR optimizes memory allocations in the main query execution paths to reduce garbage collector pressure when processing large result sets. The changes focus on reusing slice allocations rather than creating new ones for each row iteration.
- Pre-allocates scan value slices outside of loops in QuerySet.Exec and resultIterator
- Reuses the scanValues slice as a struct field in resultIterator to avoid per-row allocations
- Updates test setup to include the new scanValues field initialization
Reviewed Changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| queryset.go | Pre-allocates scan values slice outside the row iteration loop and adds scanValues field to resultIterator initialization |
| iterator.go | Adds scanValues field to resultIterator struct and reuses it in Next() method instead of creating new slice each call |
| iterator_test.go | Updates test setup to initialize the new scanValues field in resultIterator |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
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.
This change optimizes the main query execution paths to reduce memory
allocations and improve performance.
QuerySet.Exec, the slice for scanning row values is nowallocated only once outside the loop.
execIntoMaps, the slice for scannable values is also createdonly once.
resultIterator.Next, the slice for scan values is now part of theiterator struct and is reused for each row, avoiding reallocation
on each call.
These changes significantly reduce the garbage collector pressure when
processing large result sets. I also fixed a unit test that was failing due to these changes.