Skip to content

⚡ Optimize FeedbackConnection.items performance and robustness#4

Open
Skywyze wants to merge 1 commit intomasterfrom
performance-optimization-feedback-loop-8078810807298983349
Open

⚡ Optimize FeedbackConnection.items performance and robustness#4
Skywyze wants to merge 1 commit intomasterfrom
performance-optimization-feedback-loop-8078810807298983349

Conversation

@Skywyze
Copy link
Owner

@Skywyze Skywyze commented Feb 24, 2026

This PR optimizes the FeedbackConnection.items generator in apns.py.

The original implementation used buff += chunk and buff = buff[bytes_to_read:], which led to quadratic time complexity due to repeated copying of the immutable bytes buffer. By switching to bytearray and using extend() and del, data accumulation and consumption are now significantly more efficient.

Additionally, a bug was fixed where the loop would break if a chunk smaller than 6 bytes was received. This now correctly continues to fetch more data until enough is available to parse the record header.

Performance impact:

  • 16,000 tokens (single chunk): 0.2447s -> 0.0416s (approx. 6x speedup)
  • 32,000 tokens (single chunk): ~1.0s (extrapolated) -> 0.0836s (approx. 12x speedup)

All existing tests pass, and the partial chunk bug was verified fixed with a new test case.


PR created automatically by Jules for task 8078810807298983349 started by @Skywyze

- Replaced immutable bytes concatenation and slicing with `bytearray` and `del` to avoid $O(N^2)$ performance degradation.
- Fixed a bug where processing would prematurely terminate if a received chunk was smaller than 6 bytes.
- Updated loop condition to correctly handle 6-byte record headers.
- Verified performance improvement: processing 16k tokens in a single chunk improved from 0.2447s to 0.0416s.

Co-authored-by: Skywyze <7504109+Skywyze@users.noreply.github.com>
@google-labs-jules
Copy link

👋 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.

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