Skip to content

perf(sessions): sub-second /chat loading for large session histories#27028

Open
N1kky-wed wants to merge 23 commits into
google-gemini:mainfrom
N1kky-wed:main
Open

perf(sessions): sub-second /chat loading for large session histories#27028
N1kky-wed wants to merge 23 commits into
google-gemini:mainfrom
N1kky-wed:main

Conversation

@N1kky-wed
Copy link
Copy Markdown

@N1kky-wed N1kky-wed commented May 14, 2026

Summary

The /chat command took 25+ seconds to load with large session histories.
This PR reduces load time to 634ms on a real-world benchmark of 59 sessions / 2.3GB of JSONL.

Details

Three compounding bottlenecks eliminated:

  1. chatRecordingService.ts: New ultra-fast preview path uses raw fd.read()
    calls (64KB head + 128KB tail). Core metadata is extracted via regex directly
    on buffers, while the first user message is extracted via line-based JSON parsing
    to robustly handle nested structures — without streaming the full file.

  2. sessionUtils.ts: Bulk wc -l across all files in a single subprocess call
    (batched at 100 files) replaces per-file subprocess calls. Windows fallback uses
    batch-limited (20 concurrent) Node.js buffer scanning to avoid FD exhaustion.

  3. utils.ts: Stable secondary sort by startTime when lastUpdated ties, fixing
    inconsistent session ordering. Filename timestamp used as reliable startTime
    fallback instead of mtime.

Note: The 25.62s benchmark was recorded on a fresh environment. In practice,
load times frequently exceed 60 seconds on warmed systems with large histories.

Related Issues

Fixes #27027
Related to #26478

How to Validate

  1. Generate a large session history (30+ sessions with substantial conversation history recommended — the more messages per session, the larger the files and the more pronounced the improvement)
  2. Run /chat and compare load time against main branch
  3. Verify session list ordering is consistent across multiple opens
  4. On Linux: load time should be under 1 second for typical histories

Note: Windows fallback path (batch-limited buffer scanning) is implemented
but not benchmarked on hardware — community validation welcome.

Pre-Merge Checklist

  • Updated relevant documentation and README (if needed)
  • Added/updated tests (if needed)
  • Noted breaking changes (if any)
  • Validated on required platforms/methods:
    • MacOS
      • npm run
      • npx
      • Docker
      • Podman
      • Seatbelt
    • Windows
      • npm run
      • npx
      • Docker
    • Linux
      • npm run
      • npx
      • Docker

@gemini-cli gemini-cli Bot added the area/core Issues related to User Interface, OS Support, Core Functionality label May 14, 2026
@N1kky-wed
Copy link
Copy Markdown
Author

N1kky-wed commented May 14, 2026

InShot_20260514_115500194.mp4

Real-world comparison: 1 minute 7 seconds → 1.24 seconds (98% reduction)
Benchmark (fresh environment): 25.62s → 634ms

@N1kky-wed
Copy link
Copy Markdown
Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces significant performance optimizations for session loading and sorting. Key improvements include a secondary sort by startTime for sessions with identical lastUpdated timestamps, and a new high-performance path for retrieving message counts using wc -l on POSIX systems or optimized buffer scanning on Windows. Furthermore, loadConversationRecord now includes an ultra-fast preview mode that extracts metadata using regex from raw file buffers, significantly reducing the overhead of JSON parsing for large files. A review comment identifies a potential issue where the current logic for parsing command-line output would fail if file paths contain spaces, and it suggests a more robust regex-based extraction and path resolution strategy.

Comment thread packages/cli/src/utils/sessionUtils.ts Outdated
@TrueAlpha-spiral
Copy link
Copy Markdown

TrueAlpha-spiral commented May 14, 2026 via email

@N1kky-wed
Copy link
Copy Markdown
Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request implements performance optimizations for session loading, including a regex-based fast path for large files, bulk line counting using native system tools on POSIX, and parallelized metadata processing. It also adds secondary sorting by session start time. Reviewer feedback highlights the need for a helper to extract the latest metadata occurrences in JSONL files, anchoring regex patterns to prevent false positives during parsing, and sanitizing AI-generated summaries to prevent prompt injection.

Comment thread packages/core/src/services/chatRecordingService.ts
Comment thread packages/core/src/services/chatRecordingService.ts Outdated
Comment thread packages/core/src/services/chatRecordingService.ts Outdated
Comment thread packages/core/src/services/chatRecordingService.ts Outdated
@N1kky-wed
Copy link
Copy Markdown
Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces significant performance optimizations for session browsing by implementing a fast-path for loading conversation metadata. Key changes include a regex-based 'ultra-fast' preview for large session files, bulk line counting using native wc -l on POSIX systems, and optimized metadata-only parsing that avoids full JSON deserialization of message content. Additionally, session sorting now includes a stable secondary sort by startTime. Feedback was provided regarding a fragile regex pattern used for summary extraction in the fast-path, which could fail if nested objects appear before the summary key.

Comment thread packages/core/src/services/chatRecordingService.ts Outdated
@N1kky-wed
Copy link
Copy Markdown
Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request significantly optimizes session loading performance by introducing a fast preview path that uses regex-based extraction from file buffers and a bulk line-counting mechanism (using wc -l on POSIX). It also refactors the metadata-only loading path to reduce unnecessary JSON parsing. Feedback highlights several critical areas for improvement: the sanitizeSummary function incorrectly uses HTML encoding which breaks terminal rendering and violates prompt injection rules; the regex for metadata extraction is fragile regarding escaped quotes; the optimized line reader relies on a brittle startsWith check; and the Windows fallback for line counting could be more efficient by using Buffer.indexOf instead of byte-by-byte iteration.

Comment thread packages/core/src/services/chatRecordingService.ts Outdated
Comment thread packages/core/src/services/chatRecordingService.ts Outdated
Comment thread packages/core/src/services/chatRecordingService.ts Outdated
Comment thread packages/cli/src/utils/sessionUtils.ts Outdated
@N1kky-wed
Copy link
Copy Markdown
Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces significant performance optimizations for session management and browsing. Key improvements include a secondary sort by startTime for stable session ordering, a platform-optimized bulk line-counting mechanism using wc -l or stream buffers, and an 'ultra-fast' metadata extraction path in loadConversationRecord that utilizes regex on raw file buffers to avoid expensive JSON parsing. Review feedback identified a brittle regex used for message ID extraction in the optimized metadata path, which could fail if property ordering in the JSON lines changes, potentially leading to incorrect message counts.

Comment thread packages/core/src/services/chatRecordingService.ts Outdated
@N1kky-wed
Copy link
Copy Markdown
Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces significant performance optimizations for loading and displaying session history. Key changes include a secondary sort by startTime for stable session ordering, a bulk line-counting mechanism (utilizing wc -l on POSIX systems), and an 'ultra-fast' preview mode in loadConversationRecord that uses regex-based extraction from raw file buffers to bypass full JSON parsing. Feedback from the review focuses on preventing potential crashes due to file descriptor limits during concurrent processing, ensuring robust fallbacks for the bulk line-counting logic, and removing redundant file system calls to further improve efficiency.

Comment thread packages/cli/src/utils/sessionUtils.ts Outdated
Comment thread packages/cli/src/utils/sessionUtils.ts
Comment thread packages/cli/src/utils/sessionUtils.ts Outdated
@N1kky-wed
Copy link
Copy Markdown
Author

/gemini review

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces significant performance optimizations for session management, including a regex-based "fast preview" mode for large session files and bulk line counting using native system tools. It also refines session sorting and adds metadata sanitization. Feedback focuses on improving the robustness of the fast preview logic, specifically by ensuring the most recent metadata is captured from buffers, handling regex flags safely to avoid runtime errors, and lowering concurrency limits in fallback paths to prevent file descriptor exhaustion. Additionally, there is a note regarding the potential for data visibility issues in extremely large files where messages might fall outside the optimized read buffers.

Comment thread packages/cli/src/utils/sessionUtils.ts Outdated
Comment thread packages/core/src/services/chatRecordingService.ts Outdated
Comment thread packages/core/src/services/chatRecordingService.ts Outdated
Comment thread packages/core/src/services/chatRecordingService.ts
Comment thread packages/core/src/services/chatRecordingService.ts
@N1kky-wed N1kky-wed marked this pull request as ready for review May 14, 2026 20:58
@N1kky-wed N1kky-wed requested a review from a team as a code owner May 14, 2026 20:58
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request introduces significant performance optimizations to the /chat command, reducing load times from over 25 seconds to under 1 second for large session histories. By implementing a fast-preview path for metadata and batching file system operations, the changes drastically reduce I/O overhead and processing time while maintaining robust data handling.

Highlights

  • Ultra-fast Preview Path: Implemented a new preview mechanism in chatRecordingService.ts that uses raw buffer reads (64KB head/128KB tail) and regex-based metadata extraction, bypassing full file streaming for significant performance gains.
  • Bulk Line Counting: Optimized session history loading by replacing per-file subprocess calls with batched wc -l commands on POSIX systems and a concurrent buffer-scanning fallback for Windows.
  • Stable Session Sorting: Updated utils.ts to include a secondary sort by startTime when lastUpdated timestamps tie, ensuring consistent session ordering in the UI.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize the Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counterproductive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request introduces performance optimizations for session history loading and browsing. Key improvements include a secondary sort by startTime for sessions with identical update times, a fast line-counting utility using wc -l or streams, and an 'ultra-fast' metadata extraction path that uses regex on raw file buffers to avoid full JSON parsing. Feedback was provided to improve the robustness of regex patterns used during metadata extraction to prevent false matches within message content.

Comment thread packages/core/src/services/chatRecordingService.ts Outdated
…tches in message content

Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/core Issues related to User Interface, OS Support, Core Functionality

Projects

None yet

Development

Successfully merging this pull request may close these issues.

/chat command loading extremely slow with large histories (60s+)

2 participants