Skip to content

⚡ Bolt: Replace O(N²) array search with O(N) Set lookup in session listing#93

Merged
iotserver24 merged 1 commit into
mainfrom
bolt-optimize-session-list-o-n-squared-15004076471281300550
May 17, 2026
Merged

⚡ Bolt: Replace O(N²) array search with O(N) Set lookup in session listing#93
iotserver24 merged 1 commit into
mainfrom
bolt-optimize-session-list-o-n-squared-15004076471281300550

Conversation

@iotserver24
Copy link
Copy Markdown
Owner

@iotserver24 iotserver24 commented May 17, 2026

💡 What: Replaced a nested array search (jsonlFiles.some(...)) containing string replacements with an O(1) Set lookup (jsonlBaseNames.has(...)) inside the collectSessionsFromDir loop.
🎯 Why: When reading the session history directory, finding legacy JSON files that don't have a corresponding JSONL file was causing an O(N²) performance bottleneck. This was because Array.prototype.some was called on the jsonlFiles array for every .json file, executing string replacements on every iteration.
📊 Impact: Changes the time complexity of the legacy file filtering from O(N²) to O(N). For users with hundreds of past sessions, this significantly reduces the CPU overhead and lag when opening the application or loading the history panel.
🔬 Measurement: Verified by ensuring the session history continues to correctly load and deduplicate legacy sessions without regressions using pnpm test. You can verify the performance difference by profiling a session directory with ~1000+ .json and .jsonl files.


PR created automatically by Jules for task 15004076471281300550 started by @iotserver24

Summary by CodeRabbit

  • Refactor

    • Optimized session listing performance with improved file lookup operations, delivering faster load times and enhanced responsiveness when managing sessions
  • Documentation

    • Added documentation entry describing session listing performance optimization improvements

Review Change Stack

@google-labs-jules
Copy link
Copy Markdown

👋 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.

@xibe-review
Copy link
Copy Markdown

xibe-review Bot commented May 17, 2026

Hey @iotserver24! 👋

I'll go through the changes and help you out with an automated review! 🔍

Starting the review now...

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 17, 2026

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro Plus

Run ID: 6c6daf43-5d3b-4685-be5e-b59caea301f4

📥 Commits

Reviewing files that changed from the base of the PR and between 950b462 and 6b35d15.

📒 Files selected for processing (2)
  • .jules/bolt.md
  • packages/core/src/session-manager.ts

📝 Walkthrough

Walkthrough

This PR optimizes session directory listing by replacing repeated linear array searches with a precomputed Set lookup. Legacy .json file filtering now consults a Set of JSONL base names for O(1) membership checks instead of O(N²) array iteration, and this change is documented in the learning log.

Changes

Session Listing Performance

Layer / File(s) Summary
Set-based duplicate filtering
packages/core/src/session-manager.ts, .jules/bolt.md
In collectSessionsFromDir, legacy .json files are now filtered using a precomputed Set of existing .jsonl base names instead of repeated some() checks. The optimization shifts file enumeration from O(N²) to O(N) complexity and is documented as a performance learning.

🎯 1 (Trivial) | ⏱️ ~5 minutes

A rabbit hops through files with glee,
No more searching endlessly—
Sets precomputed, lookups quick,
O(N) now, not N² tricks!
Performance blooms, efficiency springs,
Such optimization joy it brings! 🐰✨

✨ 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-optimize-session-list-o-n-squared-15004076471281300550

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint skipped: no ESLint configuration detected in root package.json. To enable, add eslint to devDependencies.


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.

@xibe-review
Copy link
Copy Markdown

xibe-review Bot commented May 17, 2026

🔍 Code Review for @iotserver24

Great work on this performance improvement! The change is clean, focused, and well-documented. Here’s a detailed review covering security, recommended improvements, and overall analysis.


🔐 Security Concerns

No security issues found.

  • No hardcoded credentials, API keys, secrets, or sensitive configurations were introduced.
  • All file operations remain within controlled directories, and no user input manipulates file paths.
  • The change does not introduce injection, traversal, or authentication vulnerabilities.

The PR is safe from a security standpoint.


💡 User‑Specific Analysis (Performance Optimization)

You replaced an O(N²) Array.some() + replace() call inside a loop with an O(1) Set.has() lookup. This is exactly the right fix for the described bottleneck.

Before:
For each .json file, jsonlFiles.some(...) scanned the entire jsonlFiles array, executing replace() on every element.
After:
jsonlBaseNames is built once (O(M) where M = number of .jsonl files), and then each .json file checks existence in O(1).

The logic remains identical:

// New code (lines 559‑562)
const jsonlBaseNames = new Set(jsonlFiles.map((f) => f.slice(0, -6)));
// Later (line 571)
if (jsonlBaseNames.has(file.slice(0, -5))) continue;

This is a textbook micro‑optimization from array to Set, and it’s precisely what’s needed for directories with hundreds of session files. No behavioral regression is introduced.


🛠 Recommended Changes

While the PR is complete and correct, consider these minor improvements for long‑term robustness:

  1. Make the extension slicing more explicit
    The current slice(0, -6) / slice(0, -5) works because the filters guarantee extensions .jsonl and .json, but it’s fragile if the extension length ever changes (e.g., .jsonl.backup is accidentally included).
    Recommendation: Use a utility like basename.replace(/\.[^.]+$/, '') or explicitly check the extension length only after stripping. However, since the endsWith filter is immediately above, this is a low‑risk choice. No urgent action needed.

  2. Add a comment explaining the constant‑time lookup
    Future readers might not immediately see why a Set is used. A brief comment on line 559 would help:

    // Pre‑compute base names for O(1) lookup later
    const jsonlBaseNames = new Set(jsonlFiles.map((f) => f.slice(0, -6)));
  3. Test coverage
    The PR description mentions pnpm test passed. Ensure there is at least one unit test that exercises this deduplication logic with multiple .json and .jsonl files. If not, adding one would prevent regressions.


📊 Comprehensive Analysis

Functionality:
✅ The deduplication behaves identically to the original code – legacy .json files are skipped if a corresponding .jsonl exists. No edge‑case issues introduced.

Performance:
Measurable improvement – O(N²) → O(N) in the number of mixed‑type session files. This directly reduces UI lag when loading large history panels. The precomputed Set adds O(M) memory, which is trivial.

Code Quality:
✅ Minimal diff (+3/‑1 lines), clean variable names (jsonlBaseNames), and no side‑effects.
✅ The rest of the function (error handling, concurrency batching, directory reading) remains untouched.

Maintainability:
✅ The change is easy to understand and revert if needed.
⚠️ (Minor) The hardcoded slice lengths rely on exact extension lengths; while safe now, a comment or a more explicit stripping would improve future‑proofing.

Verification:
✅ The PR author confirms pnpm test passes. Manual verification with a mixed‑file session directory would also be straightforward.


📋 Review Summary

Files reviewed: 2
Issues found:

  • 🔴 Critical: 0
  • 🟡 Warnings: 0
  • 🔵 Suggestions: 1 (add comment for Set purpose)

Recommendation:Approve – The change is secure, correct, and delivers a clear performance win with zero regression risk. Optional suggestions can be addressed in a follow‑up, but the PR is ready to merge as is.


🤖 Powered by Xibe AI • Auto-generated
📊 Analysis: 2283 characters analyzed across 2 files
💙 Real-time Analytics📚 Documentation

@iotserver24 iotserver24 marked this pull request as ready for review May 17, 2026 18:23
Copilot AI review requested due to automatic review settings May 17, 2026 18:23
@iotserver24 iotserver24 merged commit 92c2ff6 into main May 17, 2026
4 of 5 checks passed
@iotserver24 iotserver24 deleted the bolt-optimize-session-list-o-n-squared-15004076471281300550 branch May 17, 2026 18:23
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR improves session history directory scanning performance by replacing a nested O(N²) filename check with an O(N) precomputed Set lookup, reducing CPU work when many .json and .jsonl session files exist.

Changes:

  • Precomputes a Set of .jsonl base names and uses Set.has(...) to skip legacy .json sessions that already have a JSONL equivalent.
  • Adds a Bolt learning note documenting the optimization pattern for future reference.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
packages/core/src/session-manager.ts Replaces per-file array.some(...) + string ops with a precomputed Set lookup for legacy session deduping.
.jules/bolt.md Documents the O(N²) → O(N) optimization pattern as a Bolt learning/action item.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

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.

2 participants