[PR #3115] [MERGED] fix(client): O(1) segment index lookup in game narration (#3104) #5805

Closed
opened 2026-10-22 04:19:40 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/3115
Author: @kolacheee
Created: 6/30/2026
Status: ✅ Merged
Merged: 7/2/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/3104-game-narration-quadratic


📝 Commits (2)

  • 665451f fix(client): O(1) segment index lookup in game narration (#3104)
  • 372be04 Merge branch 'staging' into fix/3104-game-narration-quadratic

📊 Changes

1 file changed (+12 additions, -2 deletions)

View changed files

📝 packages/client/src/components/game/GameNarration.tsx (+12 -2)

📄 Description

Linked issue

Partially addresses #3104 (the game-mode half). This does not close #3104 — the roleplay/conversation lag is a separate, still-open thread (different render path; this change does not touch it).

Why this change

A contributor confirmed the lag does not exist in v2.0.6, so #3104 is a regression. Bisecting the render-path diff (v2.0.6 → staging) surfaced one change that fits the instrumented signature (8–22s render+commit scaling with chat length, game mode):

In GameNarration.tsx, the stacked-log narration renderer computes, at the top of each per-segment render:

const liveSegmentIndex = segments.findIndex((candidate) => candidate.id === seg.id);

segments is the entire chat's flattened, uncapped segment array, and renderStackedLogSegment runs once per visible segment. So the cost is rendered × total = O(n²) in segment count, per render. On long agent-heavy chats (hundreds of messages → thousands of segments) that's thousands of full-array scans per render — matching the measured multi-second freeze, and scaling with chat length exactly as users report.

In v2.0.6 this renderer was display-only and had no findIndex. The eager lookup was added in v2.0.7, commit e1e378b7e ("Prepare v2.0.7 follow-up fixes"), when the segment renderer gained an inline action-button suite. GameNarration is rendered solely by GameSurface, so this is game-mode only.

What changed

Build an id → index Map once per segments change (a useMemo keyed on segments) and look it up in O(1) at both call sites (the delete-navigation handler and the jump-to-segment row). This collapses the per-render cost from O(n²) to O(n) (one Map build) + O(1) lookups.

Behavior-identical: findIndex returned -1 when absent; Map.get(...) ?? -1 returns the same. No visual or behavioral change — purely the cost of the index lookup.

Validation

  • pnpm check passes locally
  • Container (Docker / Podman) built and ran without issue
  • Ran the app, clicked through the changes manually
  • Checked edge cases (light + dark mode, mobile viewport, empty states, error paths)
  • Above manual verification completed (describe below)
  • Read and followed CONTRIBUTING.md

Manual verification notes

Done by the agent (not a substitute for human verification):

  • pnpm check → exit 0 (TypeScript + ESLint + client build all clean).

Still needs manual verification:

  • Recommended before merge: a DevTools Performance profile on a long, agent-heavy game-mode chat, before vs. after, confirming renderStackedLogSegment / findIndex self-time drops out (per #3104's "confirm perf fixes with a profile, don't trust code-reading alone").
  • Open the Logs panel on a long game chat and confirm: stacked-log entries still render correctly, the delete button still deletes the right message/segment, and the jump-to-segment (click a past log row) still jumps to the correct segment.
  • Confirm short/empty game chats and non-game chats are unaffected.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed
  • Version/release files updated (only if this PR includes a version bump)

UI evidence (if applicable)

No UI change — this only changes how the per-segment index is computed (eager O(n²) scan → O(1) Map lookup).

Summary by CodeRabbit

  • Performance
    • Improved game narration log rendering by using faster segment lookup, making log updates and navigation more responsive.
  • Bug Fixes
    • Fixed segment selection behavior in stacked and modal log views so “jump back” actions reliably target the correct entry.

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/3115 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 6/30/2026 **Status:** ✅ Merged **Merged:** 7/2/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/3104-game-narration-quadratic` --- ### 📝 Commits (2) - [`665451f`](https://github.com/Pasta-Devs/Marinara-Engine/commit/665451f777681e3a91894533c0b9c2173daf5888) fix(client): O(1) segment index lookup in game narration (#3104) - [`372be04`](https://github.com/Pasta-Devs/Marinara-Engine/commit/372be04595bccb9ff23508e0551a3fdfdc63187d) Merge branch 'staging' into fix/3104-game-narration-quadratic ### 📊 Changes **1 file changed** (+12 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `packages/client/src/components/game/GameNarration.tsx` (+12 -2) </details> ### 📄 Description ## Linked issue Partially addresses #3104 (the **game-mode** half). This does **not** close #3104 — the roleplay/conversation lag is a separate, still-open thread (different render path; this change does not touch it). ## Why this change A contributor confirmed the lag does **not** exist in v2.0.6, so #3104 is a regression. Bisecting the render-path diff (v2.0.6 → staging) surfaced one change that fits the instrumented signature (8–22s `render+commit` scaling with chat length, game mode): In `GameNarration.tsx`, the stacked-log narration renderer computes, at the **top of each per-segment render**: ```js const liveSegmentIndex = segments.findIndex((candidate) => candidate.id === seg.id); ``` `segments` is the **entire chat's** flattened, uncapped segment array, and `renderStackedLogSegment` runs **once per visible segment**. So the cost is `rendered × total` = **O(n²) in segment count, per render**. On long agent-heavy chats (hundreds of messages → thousands of segments) that's thousands of full-array scans per render — matching the measured multi-second freeze, and scaling with chat length exactly as users report. In v2.0.6 this renderer was display-only and had no `findIndex`. The eager lookup was added in **v2.0.7, commit `e1e378b7e`** ("Prepare v2.0.7 follow-up fixes"), when the segment renderer gained an inline action-button suite. `GameNarration` is rendered solely by `GameSurface`, so this is game-mode only. ## What changed Build an `id → index` Map once per `segments` change (a `useMemo` keyed on `segments`) and look it up in O(1) at both call sites (the delete-navigation handler and the jump-to-segment row). This collapses the per-render cost from O(n²) to O(n) (one Map build) + O(1) lookups. Behavior-identical: `findIndex` returned `-1` when absent; `Map.get(...) ?? -1` returns the same. No visual or behavioral change — purely the cost of the index lookup. ## Validation - [ ] `pnpm check` passes locally - [ ] Container (Docker / Podman) built and ran without issue - [ ] Ran the app, clicked through the changes manually - [ ] Checked edge cases (light + dark mode, mobile viewport, empty states, error paths) - [ ] Above manual verification completed (describe below) - [ ] Read and followed `CONTRIBUTING.md` ### Manual verification notes Done by the agent (not a substitute for human verification): - `pnpm check` → exit 0 (TypeScript + ESLint + client build all clean). Still needs manual verification: - **Recommended before merge:** a DevTools Performance profile on a long, agent-heavy **game-mode** chat, before vs. after, confirming `renderStackedLogSegment` / `findIndex` self-time drops out (per #3104's "confirm perf fixes with a profile, don't trust code-reading alone"). - Open the **Logs** panel on a long game chat and confirm: stacked-log entries still render correctly, the **delete** button still deletes the right message/segment, and the **jump-to-segment** (click a past log row) still jumps to the correct segment. - Confirm short/empty game chats and non-game chats are unaffected. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README / CHANGELOG) as needed - [ ] Version/release files updated (only if this PR includes a version bump) ## UI evidence (if applicable) No UI change — this only changes how the per-segment index is computed (eager O(n²) scan → O(1) Map lookup). <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Performance** * Improved game narration log rendering by using faster segment lookup, making log updates and navigation more responsive. * **Bug Fixes** * Fixed segment selection behavior in stacked and modal log views so “jump back” actions reliably target the correct entry. <!-- end of auto-generated comment: release notes by coderabbit.ai --> --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:19:40 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#5805
No description provided.