Skip to content

⚡ Bolt: [Performance] Replace .map with pre-allocated array in RouteMatcher#293

Open
AhmmedSamier wants to merge 1 commit intomasterfrom
bolt-perf-array-allocation-9323353101146577012
Open

⚡ Bolt: [Performance] Replace .map with pre-allocated array in RouteMatcher#293
AhmmedSamier wants to merge 1 commit intomasterfrom
bolt-perf-array-allocation-9323353101146577012

Conversation

@AhmmedSamier
Copy link
Owner

@AhmmedSamier AhmmedSamier commented Mar 19, 2026

⚡ Bolt: [Performance] Replace .map with pre-allocated array in RouteMatcher

💡 What: Replaced templateSegments.map(...) with a pre-allocated array and manual for loop in RouteMatcher.precompute.
🎯 Why: RouteMatcher.precompute is a hot path where Array.prototype.map() introduces measurable callback and iterator allocation overhead.
📊 Impact: Expected to speed up route pattern precomputation and URL segment matching by avoiding callback overhead on segment iteration.
🔬 Measurement: Verify via bun run benchmark output under the Route Matcher Benchmarks section.


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

Summary by CodeRabbit

  • Chores

    • Updated benchmark metrics with new timing statistics
  • Refactor

    • Optimized array handling in route matching for improved performance
  • Documentation

    • Added best practices guidance for array pre-allocation techniques

…atcher

💡 What: Replaced `templateSegments.map(...)` with a pre-allocated array and manual `for` loop in `RouteMatcher.precompute`.
🎯 Why: `RouteMatcher.precompute` is a hot path where `Array.prototype.map()` introduces measurable callback and iterator allocation overhead.
📊 Impact: Expected to speed up route pattern precomputation and URL segment matching by avoiding callback overhead on segment iteration.
🔬 Measurement: Verify via `bun run benchmark` output under the `Route Matcher Benchmarks` section.

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 19, 2026

📝 Walkthrough

Walkthrough

This PR optimizes array iteration in RouteMatcher.getOrCompileCache by replacing .map() with a pre-allocated array and manual for loop. It adds documentation guidance for this optimization pattern and updates benchmark results reflecting the performance changes from this and related optimizations.

Changes

Cohort / File(s) Summary
Documentation & Guidance
.jules/bolt.md
Added new note (2024-05-26) documenting the fast array pre-allocation optimization pattern, explicitly contrasting .map() with pre-allocated new Array(length) plus manual iteration, and instructing to disable sonarjs/array-constructor lint rule in performance-sensitive paths.
Benchmark Results
language-server/benchmarks/benchmarks.json
Updated benchmark timing statistics (avgMs, totalMs, minMs, maxMs, p95Ms, stdDevMs) across all benchmark entries; JSON structure unchanged.
Performance Optimization
language-server/src/core/route-matcher.ts
Replaced Array.prototype.map() with pre-allocated new Array<boolean>(segmentsLength) and index-based for loop in RouteMatcher.getOrCompileCache for isParameter computation, maintaining identical parameter detection logic while improving iteration performance in the hot path.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

Poem

🐰 A bunny hops through arrays swift,
No map() calls to make it drift—
Pre-allocated, tight, and lean,
The fastest loops we've ever seen!

🚥 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 accurately reflects the main change: replacing .map() with pre-allocated array in RouteMatcher for performance optimization.
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-perf-array-allocation-9323353101146577012
📝 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.

🧹 Nitpick comments (2)
.jules/bolt.md (1)

1-4: Consider consolidating duplicate “array pre-allocation vs map” learnings.

This new note substantially overlaps with Line 6-Line 9 and Line 32-Line 35; keeping a single canonical entry will reduce doc drift.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In @.jules/bolt.md around lines 1 - 4, Consolidate the duplicate "array
pre-allocation vs map" notes in .jules/bolt.md by keeping a single canonical
entry (merge the content from the new note referencing RouteMatcher.precompute
and the existing entries at lines ~6-9 and ~32-35), remove the redundant
paragraphs, and ensure the retained entry mentions using pre-allocated arrays in
hot paths (e.g., RouteMatcher.precompute) and the need to disable the
sonarjs/array-constructor lint rule when appropriate; update the date or
aggregation header if needed to reflect the consolidation.
language-server/benchmarks/benchmarks.json (1)

4-343: Consider normalizing metric precision to reduce benchmark diff noise.

Rounding persisted benchmark values (for example, fixed 3–4 decimals) would make performance deltas easier to review across PRs.

🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed.

In `@language-server/benchmarks/benchmarks.json` around lines 4 - 343, Normalize
benchmark numeric precision by rounding all metric fields ("avgMs", "totalMs",
"minMs", "maxMs", "p95Ms", "stdDevMs") to a consistent fixed precision (e.g., 3
or 4 decimal places) before persisting benchmarks.json so diffs are smaller and
stable; update the code path that serializes/writes these metric objects (the
place that constructs the per-benchmark objects with "name" and the above metric
keys) to apply Number.toFixed or a rounding helper to each metric value,
ensuring totals remain numeric (parse back from strings if needed) and apply the
same rounding for every benchmark entry (e.g., "Endpoint Search (Unified)",
"Fuzzy Search 'File100'", etc.).
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.

Nitpick comments:
In @.jules/bolt.md:
- Around line 1-4: Consolidate the duplicate "array pre-allocation vs map" notes
in .jules/bolt.md by keeping a single canonical entry (merge the content from
the new note referencing RouteMatcher.precompute and the existing entries at
lines ~6-9 and ~32-35), remove the redundant paragraphs, and ensure the retained
entry mentions using pre-allocated arrays in hot paths (e.g.,
RouteMatcher.precompute) and the need to disable the sonarjs/array-constructor
lint rule when appropriate; update the date or aggregation header if needed to
reflect the consolidation.

In `@language-server/benchmarks/benchmarks.json`:
- Around line 4-343: Normalize benchmark numeric precision by rounding all
metric fields ("avgMs", "totalMs", "minMs", "maxMs", "p95Ms", "stdDevMs") to a
consistent fixed precision (e.g., 3 or 4 decimal places) before persisting
benchmarks.json so diffs are smaller and stable; update the code path that
serializes/writes these metric objects (the place that constructs the
per-benchmark objects with "name" and the above metric keys) to apply
Number.toFixed or a rounding helper to each metric value, ensuring totals remain
numeric (parse back from strings if needed) and apply the same rounding for
every benchmark entry (e.g., "Endpoint Search (Unified)", "Fuzzy Search
'File100'", etc.).

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: ab0cd6be-59f9-468f-b1e2-f2486efe54b5

📥 Commits

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

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

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