Skip to content

⚡ Bolt: Fast array pre-allocation for RouteMatcher Params#295

Open
AhmmedSamier wants to merge 1 commit intomasterfrom
bolt-route-matcher-optimization-5162502706229762723
Open

⚡ Bolt: Fast array pre-allocation for RouteMatcher Params#295
AhmmedSamier wants to merge 1 commit intomasterfrom
bolt-route-matcher-optimization-5162502706229762723

Conversation

@AhmmedSamier
Copy link
Owner

@AhmmedSamier AhmmedSamier commented Mar 20, 2026

💡 What:
Replaced Array.prototype.map() with a pre-allocated array (new Array<boolean>(length)) and a manual for loop in RouteMatcher.getOrCompileCache when checking for parameters.

🎯 Why:
The functional .map() method incurs significant closure creation and iterator overhead in a hot path.

📊 Impact:
Expected performance impact: Route Match Search time decreased from ~51ms down to ~33ms over 100 iterations.

🔬 Measurement:
Run the Route Matcher benchmark using cd language-server && bun run benchmarks/route-matcher.bench.ts.


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

Summary by CodeRabbit

  • Performance

    • Improved route template matching performance through targeted optimization enhancements in the language server.
  • Chores

    • Updated benchmark measurements to reflect current performance metrics.
    • Expanded benchmark test coverage with additional concurrent processing scenarios for scalability testing.
    • Enhanced benchmark automation and testing infrastructure.

Replaced `Array.prototype.map()` with a pre-allocated array (`new Array<boolean>(length)`) and a manual `for` loop in `RouteMatcher.getOrCompileCache` when checking for parameters. This avoids closure creation and iterator overhead in a hot path.

Expected performance impact: Route Match Search time decreased from ~51ms down to ~33ms over 100 iterations.

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

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Mar 20, 2026

📝 Walkthrough

Walkthrough

Optimized RouteMatcher.getOrCompileCache by replacing .map()-based array transformation with a pre-allocated boolean array and manual loop iteration. Updated benchmark data, added a benchmark runner script, and documented the performance pattern in guidance notes.

Changes

Cohort / File(s) Summary
Performance Guidance
.jules/bolt.md
Added entry documenting RouteMatcher.getOrCompileCache optimization pattern: pre-allocated boolean arrays with manual for loops outperform .map()-based transformations, includes lint-rule bypass recommendation.
Benchmarking Infrastructure
language-server/benchmarks/benchmarks.json, language-server/scripts/run_benchmarks.ts
Updated benchmark results with new metric values; replaced 2000-file concurrency benchmarks with 5000-file variants; added script to execute route-matcher benchmarks.
Core Optimization
language-server/src/core/route-matcher.ts
Refactored isParameter flag array construction in getOrCompileCache from templateSegments.map(...) to new Array<boolean> with indexed for loop, maintaining identical semantic behavior.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

Poem

🐰 Hops with glee through optimized loops,
Pre-allocated arrays sing with joy,
No more .map() in the route-matcher's truth,
Benchmarks bloom with youth!

🚥 Pre-merge checks | ✅ 3
✅ Passed checks (3 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly reflects the main optimization: replacing Array.map() with pre-allocated boolean arrays in RouteMatcher for performance improvement.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ 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-route-matcher-optimization-5162502706229762723
📝 Coding Plan
  • Generate coding plan for human review comments

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 and usage tips.

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Inline comments:
In `@language-server/scripts/run_benchmarks.ts`:
- Around line 1-2: The script calls the async function runRouteMatcherBenchmarks
without awaiting it, causing Bun to exit early and swallow rejections; modify
the invocation so the benchmark is awaited and errors are handled (either use
top-level await: await runRouteMatcherBenchmarks(); or wrap the call in an async
IIFE with try/catch that logs any error via console.error or a logger), ensuring
you reference runRouteMatcherBenchmarks to locate the call and add proper error
handling to avoid silent failures.

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 6f836322-ea93-4710-baf1-97567491535f

📥 Commits

Reviewing files that changed from the base of the PR and between b2c343b and f3a71b0.

📒 Files selected for processing (4)
  • .jules/bolt.md
  • language-server/benchmarks/benchmarks.json
  • language-server/scripts/run_benchmarks.ts
  • language-server/src/core/route-matcher.ts

Comment on lines +1 to +2
import { runRouteMatcherBenchmarks } from '../benchmarks/route-matcher.bench';
runRouteMatcherBenchmarks();
Copy link
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue | 🔴 Critical

Async function called without await — benchmark won't complete and errors are silently swallowed.

runRouteMatcherBenchmarks is declared as async function and the existing codebase always calls it with await. Without awaiting, Bun will exit before the benchmark finishes, and any rejections are discarded.

🐛 Proposed fix
 import { runRouteMatcherBenchmarks } from '../benchmarks/route-matcher.bench';
-runRouteMatcherBenchmarks();
+await runRouteMatcherBenchmarks();

As per coding guidelines: "Prefer async/await over raw promises" and "Always handle potential rejections in background tasks".

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import { runRouteMatcherBenchmarks } from '../benchmarks/route-matcher.bench';
runRouteMatcherBenchmarks();
import { runRouteMatcherBenchmarks } from '../benchmarks/route-matcher.bench';
(async () => {
await runRouteMatcherBenchmarks();
})().catch(console.error);
Suggested change
import { runRouteMatcherBenchmarks } from '../benchmarks/route-matcher.bench';
runRouteMatcherBenchmarks();
import { runRouteMatcherBenchmarks } from '../benchmarks/route-matcher.bench';
runRouteMatcherBenchmarks().catch(console.error);
🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@language-server/scripts/run_benchmarks.ts` around lines 1 - 2, The script
calls the async function runRouteMatcherBenchmarks without awaiting it, causing
Bun to exit early and swallow rejections; modify the invocation so the benchmark
is awaited and errors are handled (either use top-level await: await
runRouteMatcherBenchmarks(); or wrap the call in an async IIFE with try/catch
that logs any error via console.error or a logger), ensuring you reference
runRouteMatcherBenchmarks to locate the call and add proper error handling to
avoid silent failures.

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