Skip to content

⚡ Bolt: O(1) Recency Set Lookup in Slash Commands#409

Merged
AhmmedSamier merged 1 commit intomasterfrom
bolt-sort-recency-set-2819443232202878319
May 10, 2026
Merged

⚡ Bolt: O(1) Recency Set Lookup in Slash Commands#409
AhmmedSamier merged 1 commit intomasterfrom
bolt-sort-recency-set-2819443232202878319

Conversation

@AhmmedSamier
Copy link
Copy Markdown
Owner

@AhmmedSamier AhmmedSamier commented May 3, 2026

💡 What: Replaced O(N) Array.includes() lookups with O(1) Set.has() lookups inside the sort comparator for slash commands.
🎯 Why: Array.includes() inside a sort callback causes O(M log M * N) complexity. Using a pre-computed Set brings this down to O(M log M + N).
📊 Impact: Faster UI responsiveness when displaying and sorting slash command suggestions, especially as the number of available commands and the history length grows.
🔬 Measurement: Verify by typing / in the extension search bar and observing suggestion speed, though the primary impact is complexity reduction.


PR created automatically by Jules for task 2819443232202878319 started by @AhmmedSamier

Summary by CodeRabbit

  • Refactor
    • Optimized slash command sorting performance for faster and more responsive command selection.

Extracted `recentlyUsed` array into a Set before iterating in `results.sort()` to provide O(1) recency lookups.

Co-authored-by: AhmmedSamier <17784876+AhmmedSamier@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
Contributor

coderabbitai Bot commented May 3, 2026

📝 Walkthrough

Walkthrough

The sortResults method in SlashCommandService is optimized to use O(1) set lookups instead of O(n) array inclusion checks. A recentSet is precomputed from this.recentlyUsed, and lookups are performed during sorting. Sort precedence remains unchanged: exact matches, recent commands, prefix matches, then alphabetical order.

Changes

Performance Optimization: Recent Commands Lookup

Layer / File(s) Summary
Core Sort Logic
vscode-extension/src/slash-command-service.ts
sortResults constructs recentSet from this.recentlyUsed and replaces includes() checks with has() for constant-time lookups during comparator execution.

Estimated code review effort

🎯 1 (Trivial) | ⏱️ ~5 minutes

Poem

🐰 A set hops faster than an array's slow chase,
Where recent commands find their rightful place,
O(1) magic makes the sorter race,
No behavior bent, just speed's embrace! ⚡

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately describes the main change: optimizing recency lookups in slash command sorting from O(N) to O(1) using a Set, which directly aligns with the changeset modifications.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
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.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch bolt-sort-recency-set-2819443232202878319

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
Review rate limit: 0/1 reviews remaining, refill in 60 minutes.

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

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
vscode-extension/src/slash-command-service.ts (1)

289-300: 💤 Low value

LGTM — optimization is correct and well-scoped.

new Set(this.recentlyUsed) is built once before the sort comparator runs. Because recentlyUsed is capped at 10 entries, Set construction is effectively O(1), and every subsequent recentSet.has() call inside the O(M log M) comparator is O(1) as advertised.

One optional nitpick: the .toLowerCase() calls on lines 299–300 are redundant. All SlashCommand.name values are defined as lowercase literals ('/all', '/t', …), and recentlyUsed is already normalised to lowercase by recordUsage(). Dropping them saves two string allocations per comparator invocation.

🔧 Optional: remove redundant `.toLowerCase()` calls
-            const aRecent = recentSet.has(a.name.toLowerCase());
-            const bRecent = recentSet.has(b.name.toLowerCase());
+            const aRecent = recentSet.has(a.name);
+            const bRecent = recentSet.has(b.name);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@vscode-extension/src/slash-command-service.ts` around lines 289 - 300, Remove
the unnecessary .toLowerCase() calls inside the comparator: recentSet is built
from this.recentlyUsed which is already normalized by recordUsage(), and
SlashCommand.name values are lowercase literals, so change
recentSet.has(a.name.toLowerCase()) and recentSet.has(b.name.toLowerCase()) to
recentSet.has(a.name) and recentSet.has(b.name) respectively in the results.sort
comparator to avoid two extra string allocations per comparison.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In `@vscode-extension/src/slash-command-service.ts`:
- Around line 289-300: Remove the unnecessary .toLowerCase() calls inside the
comparator: recentSet is built from this.recentlyUsed which is already
normalized by recordUsage(), and SlashCommand.name values are lowercase
literals, so change recentSet.has(a.name.toLowerCase()) and
recentSet.has(b.name.toLowerCase()) to recentSet.has(a.name) and
recentSet.has(b.name) respectively in the results.sort comparator to avoid two
extra string allocations per comparison.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 2a7b9b7f-9bef-4803-b510-87b746b5dd3c

📥 Commits

Reviewing files that changed from the base of the PR and between 32a2408 and 1faf86d.

📒 Files selected for processing (1)
  • vscode-extension/src/slash-command-service.ts

@AhmmedSamier AhmmedSamier merged commit 526086d into master May 10, 2026
2 checks passed
@AhmmedSamier AhmmedSamier deleted the bolt-sort-recency-set-2819443232202878319 branch May 10, 2026 19:17
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