mirror of
https://github.com/Pasta-Devs/Marinara-Engine.git
synced 2026-12-03 19:00:50 +07:00
[PR #3403] [MERGED] perf(chat): remove O(n²) work in the post-generation completion path #5920
Labels
No labels
android
bug
bugfix
ci
client
code freeze pause
container
dependencies
docs
documentation
duplicate
enhancement
feature
feature
feature
feature-creep
fixed-in-refactor
fixed-in-staging
good first issue
help wanted
in-pr
in-pr
needs-codeowner-decision
normal-scope
old-codebase
old-codebase
pull-request
qol
question
refactor
regression
revisit later
roadmap
scope-risk
scope-risk
server
server
shared
soup
upstream
windows
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
Pasta-Devs/Marinara-Engine#5920
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/3403
Author: @kolacheee
Created: 7/9/2026
Status: ✅ Merged
Merged: 7/9/2026
Merged by: @SpicyMarinara
Base:
staging← Head:fix/3402-completion-stall📝 Commits (2)
7d2b52dperf(chat): remove O(n^2) work in the post-generation completion path (#3402)9612781Merge branch 'staging' into fix/3402-completion-stall📊 Changes
3 files changed (+39 additions, -30 deletions)
View changed files
📝
CHANGELOG.md(+1 -0)📝
packages/server/src/routes/generate.routes.ts(+10 -9)📝
packages/server/src/services/storage/chats.storage.ts(+28 -21)📄 Description
Linked issue
Closes #3402
Why this change
In a long chat, after a response finished streaming there was a stall — up to ~50 s at 2000 messages (stopwatch-measured) — before it flagged complete and the input became usable again. The stall scaled with total chat size, not prompt size (the reporter capped the prompt at ~30k tokens via summaries + new-start), so it wasn't the model — it was server-side O(n²) work that runs after the last token and gates the
doneSSE the client waits on. The two quadratics both stem from the un-indexed file-native store (SQLite users, with indexed lookups, weren't affected):chats.getMessage(staleId)each, andgetMessageis a full-table scan in the file store →n/2 × O(n)= O(n²), on every response.listMessages/listMessagesPaginatedcounted swipes withinArray(messageSwipes.messageId, ids), which the file store evaluates asids.includes()per swipe row (re-materializing the ids array each row) → O(swipeRows × ids) ≈ O(n²), and it's called 2–3× per completion (eviction + RP auto-summary + the client's refetch).What changed
packages/server/src/routes/generate.routes.ts— the eviction loop now iterates the message objects already returned bychats.listMessages()(they carryextra) instead of re-getMessage-ing each stale id. Only messages that still hold acachedPromptdo the bounded update + swipe cleanup — in steady state that's ~1 per turn. Removes the dominantn/2 × O(n).packages/server/src/services/storage/chats.storage.ts— newcountSwipesByMessageId(db, ids)helper: one scan ofmessageSwipes+ aSetof the wanted ids → O(totalSwipes) instead of theinArrayO(swipeRows × ids). Used by bothlistMessagesandlistMessagesPaginated. (inArraystays imported — it's still used for bounded, chunked game-state deletes.)Both changes are behavior-preserving — identical swipe counts and identical eviction result. Net effect: the per-response completion cost drops from O(n²) to O(n).
Not addressed here (bigger blast radius, worth follow-ups): the file store's whole-table re-serialize on each save and per-insert
findDuplicateIndexare O(total) but not quadratic; the real structural fix is a primary-key index in the file store so point lookups stop scanning the whole table. Filed context is in #3402.Validation
pnpm checkpasses locallyCONTRIBUTING.mdManual verification notes
pnpm --filter @marinara-engine/server lint.cachedPrompt).Docs and release impact
CHANGELOG updated (Fixed). No version bump.
UI evidence (if applicable)
N/A — server-side performance fix; no visual change (removes a stall).
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.