Skip to content

⚡ perf: pre-allocate slice capacity in parseSystem#37

Merged
matdev83 merged 1 commit into
mainfrom
optimize-anthropic-parse-system-slice-alloc-12487070078391722433
Jun 29, 2026
Merged

⚡ perf: pre-allocate slice capacity in parseSystem#37
matdev83 merged 1 commit into
mainfrom
optimize-anthropic-parse-system-slice-alloc-12487070078391722433

Conversation

@matdev83

Copy link
Copy Markdown
Owner

💡 What: Modified internal/plugins/frontends/anthropic/decode.go to preallocate the parts slice in parseSystem with make([]lipapi.Part, 0, len(blocks)) instead of declaring a nil slice var parts []lipapi.Part.
🎯 Why: Pre-allocating slice capacity when the number of elements is known (e.g. from the length of the slice being iterated over) prevents multiple reallocations during append.
📊 Measured Improvement:
Measured with a benchmark parsing a mocked request with 10 system blocks:

Baseline:
180 allocs/op, 14971 B/op, 57572 ns/op

Improvement:
176 allocs/op, 11050 B/op, 56716 ns/op

This results in a small reduction in memory allocations (fewer append re-allocations) and a slight speed improvement.


PR created automatically by Jules for task 12487070078391722433 started by @matdev83

Pre-allocating slice capacity when the number of elements is known
from the length of the slice being iterated over prevents multiple
reallocations during append.

Co-authored-by: matdev83 <211248003+matdev83@users.noreply.github.com>
@google-labs-jules

Copy link
Copy Markdown
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.

@coderabbitai

coderabbitai Bot commented Jun 29, 2026

Copy link
Copy Markdown

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Path: .coderabbit.yaml

Review profile: ASSERTIVE

Plan: Pro

Run ID: 523dca4e-a235-4302-8267-52743d5c1330

📥 Commits

Reviewing files that changed from the base of the PR and between 02325fe and 8860bbb.

📒 Files selected for processing (1)
  • internal/plugins/frontends/anthropic/decode.go
📜 Recent review details
⏰ Context from checks skipped due to timeout. (1)
  • GitHub Check: qa
🧰 Additional context used
📓 Path-based instructions (3)
**/*.go

📄 CodeRabbit inference engine (Custom checks)

For server, CLI, worker, or network code, check that context.Context is propagated correctly, cancellation is respected, and new goroutines cannot leak indefinitely

Files:

  • internal/plugins/frontends/anthropic/decode.go

⚙️ CodeRabbit configuration file

**/*.go: Review as production Go code. Prioritize correctness, race conditions, goroutine leaks, context cancellation, timeout handling, error wrapping, nil-pointer risks, resource cleanup, defer placement, API compatibility, interface design, dependency boundaries, and testability. Avoid generic style comments when gofmt/golangci-lint already covers the issue.

Files:

  • internal/plugins/frontends/anthropic/decode.go
internal/plugins/frontends/**/*.go

📄 CodeRabbit inference engine (AGENTS.md)

In internal/plugins/frontends/, implement OpenAI Responses, OpenAI legacy, Anthropic, and Gemini frontends.

Files:

  • internal/plugins/frontends/anthropic/decode.go
internal/**

⚙️ CodeRabbit configuration file

internal/**: Focus on package boundaries, hidden coupling, unexported API design, concurrency safety, deterministic behavior, and whether logic belongs in this internal package.

Files:

  • internal/plugins/frontends/anthropic/decode.go
🔇 Additional comments (1)
internal/plugins/frontends/anthropic/decode.go (1)

152-152: LGTM!


📝 Walkthrough

Summary by CodeRabbit

  • Refactor
    • Improved an internal message-processing step for slightly better efficiency, with no change to user-facing behavior.

Walkthrough

In parseSystem, the parts slice initialization changes from a nil declaration to make(..., 0, len(blocks)), preallocating capacity equal to the number of blocks. No other logic changes.

Anthropic decode fix

Layer / File(s) Summary
parseSystem slice preallocation
internal/plugins/frontends/anthropic/decode.go
parts slice is initialized with make([]T, 0, len(blocks)) instead of a nil slice to preallocate capacity.

🎯 1 (Trivial) | ⏱️ ~2 minutes

🚥 Pre-merge checks | ✅ 7 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (7 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly matches the technical change and uses an appropriate perf prefix.
Description check ✅ Passed The description directly explains the same slice preallocation change and its performance impact.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
No Secrets ✅ Passed Only a slice preallocation change in parseSystem; no credentials, tokens, passwords, private keys, or sensitive URLs are introduced.
Context Propagation ✅ Passed decode.go only preallocates a slice in parseSystem; no context, goroutine, or cancellation paths were added or changed.
No Accidental Public Api Break ✅ Passed Only parseSystem’s slice preallocation changed in an internal package; no exported types, signatures, JSON fields, flags, or config keys changed.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@matdev83 matdev83 merged commit 6841133 into main Jun 29, 2026
2 checks passed
@matdev83 matdev83 deleted the optimize-anthropic-parse-system-slice-alloc-12487070078391722433 branch June 29, 2026 12:47
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