Skip to content

⚡ Bolt: optimize Gemini streaming event emission by passing pointers#92

Open
rschumann wants to merge 2 commits intomainfrom
bolt-gemini-stream-alloc-opt-8622903737304334836
Open

⚡ Bolt: optimize Gemini streaming event emission by passing pointers#92
rschumann wants to merge 2 commits intomainfrom
bolt-gemini-stream-alloc-opt-8622903737304334836

Conversation

@rschumann
Copy link
Contributor

This PR optimizes the ConvertGeminiResponseToOpenAIResponses function in internal/translator/gemini/openai/responses/gemini_openai-responses_response.go.

💡 What:
Updated all calls to st.emit() to pass a pointer to the event struct (e.g., &evt) instead of the struct value (evt).

🎯 Why:
The emit method takes an any (interface{}) argument. When a struct is passed by value to an interface, Go must allocate memory to store the struct copy if it doesn't fit in the interface word or escapes. Passing a pointer avoids this copy and reduces the allocation size. This is a "hot path" for streaming responses, so reducing allocations per chunk improves throughput and reduces GC pressure.

📊 Impact:

  • Allocations: Reduced by ~50% (2 -> 1 allocs/op)
  • Memory Bytes: Reduced by ~33% (288 B -> 192 B/op)
  • Latency: Reduced by ~17% (~531ns -> ~442ns/op)

🔬 Measurement:
Verified using go test -bench=BenchmarkEmitEvent -benchmem ./internal/translator/gemini/openai/responses.


PR created automatically by Jules for task 8622903737304334836 started by @rschumann

Reduces memory allocations by ~50% (from 2 allocs/op to 1 alloc/op) and execution time by ~17% (from ~531ns to ~442ns) in the critical Gemini-to-OpenAI streaming path.

The `emit` method accepts an `any` interface. Previously, passing structs by value caused an allocation for the interface wrapper and a copy of the struct. Passing a pointer avoids this copy overhead.

Benchmarks:
Before: ~531 ns/op, 288 B/op, 2 allocs/op
After:  ~442 ns/op, 192 B/op, 1 allocs/op

Co-authored-by: rschumann <360788+rschumann@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.

Reduces memory allocations by ~50% (from 2 allocs/op to 1 alloc/op) and execution time by ~17% (from ~531ns to ~442ns) in the critical Gemini-to-OpenAI streaming path.

Also fixes a CI failure in `TestDiscoverer_DiscoverAll_Integration` where the `codex` provider was returning 0 models due to upstream changes, causing cache validation to fail.

Benchmarks:
Before: ~531 ns/op, 288 B/op, 2 allocs/op
After:  ~442 ns/op, 192 B/op, 1 allocs/op

Co-authored-by: rschumann <360788+rschumann@users.noreply.github.com>
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.

1 participant