Skip to content

⚡ optimize slice growth in evalRetrieve#159

Merged
copybara-service[bot] merged 2 commits intomainfrom
optimize-slice-growth-evalretrieve-4496038871358788997
Feb 28, 2026
Merged

⚡ optimize slice growth in evalRetrieve#159
copybara-service[bot] merged 2 commits intomainfrom
optimize-slice-growth-evalretrieve-4496038871358788997

Conversation

@suyashkumar
Copy link
Collaborator

💡 What: This change optimizes the evalRetrieve function in interpreter/expressions.go by pre-allocating the slice used to collect retrieved values.

🎯 Why: Previously, the slice was initialized with zero capacity, causing it to undergo multiple reallocations and memory copies as it grew during the loop. By setting the initial capacity to the number of retrieved resources (len(got)), we ensure that at most one allocation occurs.

📊 Measured Improvement: While environmental limitations (slow dependency downloads in the sandbox) prevented me from obtaining exact benchmark numbers, this change follows Go performance best practices. It eliminates $O(\log n)$ reallocations and reduces overall memory pressure, which is particularly beneficial when retrieving a large number of resources.


PR created automatically by Jules for task 4496038871358788997 started by @suyashkumar

Pre-allocate the slice capacity in `evalRetrieve` using `make([]result.Value, 0, len(got))` to avoid multiple reallocations as the slice grows. This is a standard optimization in Go when the maximum size of the resulting slice is known beforehand.

Co-authored-by: suyashkumar <6299853+suyashkumar@users.noreply.github.com>
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

}

l := []result.Value{}
l := make([]result.Value, 0, len(got))
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

if we wanted to be optimal should we only set this if expr.Codes == nil? might not be worth the added complexity though

Copy link
Contributor

Choose a reason for hiding this comment

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

That's a fair point. Pre-allocating the full length avoids all reallocations regardless of the filter's hit rate, which is generally faster. While it might over-allocate memory when filtering is active, len(got) is a safe upper bound. I agree that adding conditional logic might not be worth the complexity here unless we expect very large result sets with high filtering rates.

Pre-allocate the slice capacity in `evalRetrieve` using `make([]result.Value, 0, len(got))` to avoid multiple reallocations as the slice grows. This is a standard optimization in Go when the maximum size of the resulting slice is known beforehand.

Co-authored-by: suyashkumar <6299853+suyashkumar@users.noreply.github.com>
Copy link
Collaborator

@evan-gordon evan-gordon left a comment

Choose a reason for hiding this comment

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

LGTM

@copybara-service copybara-service bot merged commit ed660cd into main Feb 28, 2026
10 checks passed
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.

2 participants