[PR #7730] [MERGED] perf(memory): build message lookups once per reply, not per stored memory #7902

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

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/7730
Author: @SpicyMarinara
Created: 10/16/2026
Status: ✅ Merged
Merged: 10/16/2026
Merged by: @SpicyMarinara

Base: staging ← Head: fix/7702-memory-prepare-maps


📝 Commits (2)

  • 2d2bd8d perf(memory): build message lookups once per reply, not per stored memory
  • 5235923 docs(memory): note when the per-context message lookups stay valid

📊 Changes

2 files changed (+192 additions, -14 deletions)

View changed files

📝 packages/server/src/services/advanced-memory.ts (+50 -14)
➕ scripts/regressions/advanced-memory-prepare-scale.regression.ts (+142 -0)

📄 Description

Important

Contributions target staging. Only SpicyMarinara may promote this repository's staging branch or a same-repository hotfix/* branch to main.
Outside and first-time contributors also require an approving review from SpicyMarinara.

Linked issue

Closes #7702

Why this change

  • In a long Roleplay chat with Advanced Memory, every reply waited several seconds before the first token: about 4.7 s at 800 closed scenes, against 0.3 s at 200. The wait came before generation even started, with or without the Decision model, and it grew faster than the chat did.
  • The cause: preparing a reply checks every stored memory (recordValid), and each check rebuilt an id map of the whole chat, searched it twice for indexes and ran a full visibility selection. Rendering each recalled scene ran another full selection just to find the persona's name. So the work was (stored memories × chat messages), about 4,000 × 9,600 at 800 scenes.

What changed

  • recordValid and renderEntry now share one lookup per context and message list: each message's position, and each view's selection (all messages, the scene archive's view, or what an audience may read), made once and reused for every record. Keyed by the context object and the message array, so a derived context (a historical cutoff, a fresh read after a write) gets its own.
  • sceneSource accepts a read-only message list, matching allowed.
  • New regression scripts/regressions/advanced-memory-prepare-scale.regression.ts: it counts how often one prepare reads each message's details, at 20 and at 80 summarized scenes. Before this change that went from 188 to 668 reads per message (the regression fails); now it stays the same (about 28 per message at both sizes).

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

Agent-run checks (offline harness, synthetic chats with 12 messages per scene, fake helper/Decision/embedding server, medians of 7 prepares):

Closed scenes Decision model Before (staging) After
200 on 865 ms 129 ms
200 off 666 ms 118 ms
800 on 11,390 ms 592 ms
800 off 8,627 ms 203 ms

The machine was shared with other jobs, so absolute times run higher than the 4.7 s in the issue; each before/after pair ran back to back. What is left grows in step with the archive (reading the stored records, ranking them), no longer with archive × chat length.

  • Same output: prepare() results (Decision on and off, at the newest message and 40 messages earlier) and status() are identical before and after at 200 and 800 scenes, apart from the creation times of the records each run saves.
  • Regressions: all 20 advanced-memory* files and the 3 memory-* files pass, including the new one; the new one fails on staging (187.9 at 20 scenes, 667.5 at 80).
  • tsc for shared, server, client and root; prettier on the changed files.

Manual verification notes

  • Manually verify in a long Roleplay chat with Advanced Memory (hundreds of scenes) that the time before the first token no longer grows with the archive.
  • Manually verify that recall still brings back the same scenes as before in that chat, with the Decision model on and off.
  • Manually verify a swipe on an older message still prepares memory normally.

Docs and release impact

  • No docs changes needed
  • Updated docs (README / CONTRIBUTING / android/README) as needed
  • Translated docs on the docs-i18n branch updated to match, or a [docs-i18n] follow-up issue opened (see CONTRIBUTING.md § Translated documentation)
  • Version/release files updated (only if this PR includes a version bump)

No docs, UI or copy changes.

UI evidence (if applicable)

Not applicable: server-only change.

🤖 Generated with Claude Code

Summary by CodeRabbit

  • Performance
    • Preparing memory with a growing scene history now scales more efficiently, reducing repeated work while keeping the latest scene available as live context. This helps maintain consistent memory preparation as conversations accumulate more past scenes.

🔄 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/7730 **Author:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Created:** 10/16/2026 **Status:** ✅ Merged **Merged:** 10/16/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `staging` ← **Head:** `fix/7702-memory-prepare-maps` --- ### 📝 Commits (2) - [`2d2bd8d`](https://github.com/Pasta-Devs/Marinara-Engine/commit/2d2bd8d41280eb38a499d74d769096212efdde4e) perf(memory): build message lookups once per reply, not per stored memory - [`5235923`](https://github.com/Pasta-Devs/Marinara-Engine/commit/52359231ecf89cffa7706f0160a25ca22f8f6b7e) docs(memory): note when the per-context message lookups stay valid ### 📊 Changes **2 files changed** (+192 additions, -14 deletions) <details> <summary>View changed files</summary> 📝 `packages/server/src/services/advanced-memory.ts` (+50 -14) ➕ `scripts/regressions/advanced-memory-prepare-scale.regression.ts` (+142 -0) </details> ### 📄 Description > [!IMPORTANT] > Contributions target `staging`. Only `SpicyMarinara` may promote this repository's `staging` branch or a same-repository `hotfix/*` branch to `main`. > Outside and first-time contributors also require an approving review from `SpicyMarinara`. <!-- See CONTRIBUTING.md § Branches. --> ## Linked issue Closes #7702 ## Why this change - In a long Roleplay chat with Advanced Memory, every reply waited several seconds before the first token: about 4.7 s at 800 closed scenes, against 0.3 s at 200. The wait came before generation even started, with or without the Decision model, and it grew faster than the chat did. - The cause: preparing a reply checks every stored memory (`recordValid`), and each check rebuilt an id map of the whole chat, searched it twice for indexes and ran a full visibility selection. Rendering each recalled scene ran another full selection just to find the persona's name. So the work was (stored memories × chat messages), about 4,000 × 9,600 at 800 scenes. ## What changed - `recordValid` and `renderEntry` now share one lookup per context and message list: each message's position, and each view's selection (all messages, the scene archive's view, or what an audience may read), made once and reused for every record. Keyed by the context object and the message array, so a derived context (a historical cutoff, a fresh read after a write) gets its own. - `sceneSource` accepts a read-only message list, matching `allowed`. - New regression `scripts/regressions/advanced-memory-prepare-scale.regression.ts`: it counts how often one prepare reads each message's details, at 20 and at 80 summarized scenes. Before this change that went from 188 to 668 reads per message (the regression fails); now it stays the same (about 28 per message at both sizes). ## 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` Agent-run checks (offline harness, synthetic chats with 12 messages per scene, fake helper/Decision/embedding server, medians of 7 prepares): | Closed scenes | Decision model | Before (staging) | After | | --- | --- | --- | --- | | 200 | on | 865 ms | 129 ms | | 200 | off | 666 ms | 118 ms | | 800 | on | 11,390 ms | 592 ms | | 800 | off | 8,627 ms | 203 ms | The machine was shared with other jobs, so absolute times run higher than the 4.7 s in the issue; each before/after pair ran back to back. What is left grows in step with the archive (reading the stored records, ranking them), no longer with archive × chat length. - Same output: `prepare()` results (Decision on and off, at the newest message and 40 messages earlier) and `status()` are identical before and after at 200 and 800 scenes, apart from the creation times of the records each run saves. - Regressions: all 20 `advanced-memory*` files and the 3 `memory-*` files pass, including the new one; the new one fails on staging (`187.9 at 20 scenes, 667.5 at 80`). - `tsc` for shared, server, client and root; prettier on the changed files. ### Manual verification notes - Manually verify in a long Roleplay chat with Advanced Memory (hundreds of scenes) that the time before the first token no longer grows with the archive. - Manually verify that recall still brings back the same scenes as before in that chat, with the Decision model on and off. - Manually verify a swipe on an older message still prepares memory normally. ## Docs and release impact - [ ] No docs changes needed - [ ] Updated docs (README / CONTRIBUTING / android/README) as needed - [ ] Translated docs on the `docs-i18n` branch updated to match, or a `[docs-i18n]` follow-up issue opened (see CONTRIBUTING.md § Translated documentation) - [ ] Version/release files updated (only if this PR includes a version bump) No docs, UI or copy changes. ## UI evidence (if applicable) Not applicable: server-only change. 🤖 Generated with [Claude Code](https://claude.com/claude-code) <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Performance** * Preparing memory with a growing scene history now scales more efficiently, reducing repeated work while keeping the latest scene available as live context. This helps maintain consistent memory preparation as conversations accumulate more past scenes. <!-- 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-24 04:08: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#7902
No description provided.