Skip to content

⚡ Bolt: Optimize Embedded Corpus Lookup and Allocations#187

Merged
theRebelliousNerd merged 1 commit into
mainfrom
bolt/optimize-prompt-corpus-lookup-776668771632516532
Feb 13, 2026
Merged

⚡ Bolt: Optimize Embedded Corpus Lookup and Allocations#187
theRebelliousNerd merged 1 commit into
mainfrom
bolt/optimize-prompt-corpus-lookup-776668771632516532

Conversation

@theRebelliousNerd
Copy link
Copy Markdown
Owner

@theRebelliousNerd theRebelliousNerd commented Feb 13, 2026

⚡ Bolt: Optimized Embedded Corpus Lookup and Allocations

💡 What:

  • Added a byCategory map index to EmbeddedCorpus to replace O(N) iteration in GetByCategory.
  • Added AppendAll method to append atoms directly to a destination slice, avoiding the allocation of an intermediate slice in All().
  • Updated collectAtomsWithStats to pre-allocate the allAtoms slice based on corpus count to prevent slice resizing.

🎯 Why:

  • GetByCategory and All (via collectAtomsWithStats) are called frequently during prompt compilation.
  • GetByCategory was iterating the entire map (O(N)), which is slow for large corpora.
  • All was allocating a full slice copy that was immediately appended to another slice, creating unnecessary garbage.

📊 Impact:

  • GetByCategory is ~11x faster (3.3µs vs 37.9µs).
  • AppendAll is ~34x faster than All (182ns vs 6.2µs) and eliminates 8KB allocation per call (for 1000 atoms).
  • Reduced GC pressure during high-frequency prompt compilation.

🔬 Measurement:

  • Verified with go test -bench (benchmarks run locally and results documented).
  • Existing tests passed.

PR created automatically by Jules for task 776668771632516532 started by @theRebelliousNerd

Summary by CodeRabbit

  • Chores
    • Optimized internal atom lookup and collection performance through enhanced data structures and memory management.

- Add `byCategory` index to `EmbeddedCorpus` for O(1) `GetByCategory` lookups (was O(N)).
- Add `AppendAll` method to `EmbeddedCorpus` to avoid intermediate slice allocation.
- Pre-allocate `allAtoms` slice in `JITPromptCompiler.collectAtomsWithStats` to reduce reallocations.

Benchmarks:
- `GetByCategory`: ~11x faster (3336ns vs 37953ns)
- `AppendAll`: ~34x faster than `All` (182ns vs 6233ns) with 0 allocations.

Co-authored-by: theRebelliousNerd <187437903+theRebelliousNerd@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
Copy link
Copy Markdown

coderabbitai Bot commented Feb 13, 2026

📝 Walkthrough

Walkthrough

This PR introduces performance optimizations to the prompt atom corpus handling: a pre-built category index for O(1) lookups in atoms.go and a preallocated buffer approach in compiler.go that leverages a new AppendAll method to reduce memory reallocations during corpus collection.

Changes

Cohort / File(s) Summary
Performance Index and Utility Methods
internal/prompt/atoms.go
Added byCategory index field (map[AtomCategory][]*PromptAtom) to EmbeddedCorpus for O(1) category lookups. Modified NewEmbeddedCorpus to initialize and populate this index. Optimized GetByCategory to use the pre-built index and return a copy. Added AppendAll method for efficient slice appending without intermediate allocations.
Compiler Integration
internal/prompt/compiler.go
Updated corpus collection logic to use preallocated buffers. Replaced All() calls with Count() for capacity estimation and preallocated slice make(). Now uses the new AppendAll() method for populating atoms with ~100-capacity buffer margin to reduce reallocations.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Poem

🐰 Swift hopping through categories,
No more searching left and right,
Pre-built maps and buffered arrays,
Make our prompt code fast and light!
Optimization done with care,
Performance blooms in the air! 🚀


Note

🎁 Summarized by CodeRabbit Free

Your organization is on the Free plan. CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please upgrade your subscription to CodeRabbit Pro by visiting https://app.coderabbit.ai/login.

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

@theRebelliousNerd theRebelliousNerd merged commit 5052cb5 into main Feb 13, 2026
1 of 2 checks passed
@theRebelliousNerd theRebelliousNerd deleted the bolt/optimize-prompt-corpus-lookup-776668771632516532 branch February 13, 2026 21:16
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