[PR #5608] [MERGED] perf(storage): share unchanged rows across write paths (#5592 Phase 3) #6844

Closed
opened 2026-10-23 04:14:14 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/5608
Author: @kolacheee
Created: 8/28/2026
Status: ✅ Merged
Merged: 8/28/2026
Merged by: @kolacheee

Base: staging ← Head: feat/5592-phase3-write-cost


📝 Commits (2)

  • 04360aa perf(storage): share unchanged rows across write paths — rows are immutable once installed
  • 941bfc5 style: format lazy-chat-units regression

📊 Changes

3 files changed (+84 additions, -6 deletions)

View changed files

📝 CHANGELOG.md (+1 -0)
📝 packages/server/src/db/file-backed-store.ts (+26 -6)
📝 scripts/regressions/lazy-chat-units.regression.ts (+57 -0)

📄 Description

Why

Part of #5592 — the largest remaining per-write cost from the original #4730 diagnosis. Every insert cloned the ENTIRE resident message table (target.map(cloneRow) — O(rows) object clones per message), and each transaction's first mutation per table deep-copied the whole array again. On the large profiles that produced the GC-fatal ~430 MB allocation spikes, sending one message allocated a full copy of every resident row.

Stacked on #5607 (both touch the store; merge that first and this rebases clean).

What

The store's mutation paths already REPLACE rows rather than mutating them — with exactly one exception found by audit: the set-null cascade wrote null into live row objects in place. That last mutator is now copy-on-write, which promotes row-object immutability to a store-wide invariant, and sharing becomes safe everywhere:

  • inserts take a pointer-copy slice() of the table instead of per-row clones (~8 bytes/row transient instead of a full object clone per row),
  • transaction snapshots become shallow reference arrays instead of full row copies (insert-in-tx was previously 2× whole-table),
  • the merge mirror pushes references.

Update paths already shared unmatched rows, so read-side behavior is unchanged: selects clone at projection (verified in #5597), so no caller ever held a live row reference.

Regressions

New block pins the invariant behaviorally: a rolled-back set-null must leave the child's foreign key intact in memory and on disk. Verified to FAIL under the dangerous half-state (shallow snapshots while the in-place mutator still existed) and pass with both halves — the half-state is precisely what a future in-place mutation would recreate, so the test guards the invariant, not just this change. Full local node suite 155/161 (only the six known Windows-checkout lanes), message-sharding green in both lazy and eager modes.

Test plan

  • pnpm check on a non-Windows checkout
  • pnpm regression
  • Manually verify on a LARGE profile: send messages in a big chat — same behavior, and per-send memory spikes visibly smaller in a heap profile
  • Manually verify: transaction-heavy flows (imports, batch deletes) behave unchanged

References #5592 and #4730 (do not auto-close).

Summary by CodeRabbit

  • Performance

    • Improved transaction handling for large profiles by reducing memory allocation when creating snapshots.
  • Bug Fixes

    • Fixed transactional relationship updates so rolled-back changes correctly restore related data both in memory and after saving to disk.
  • Tests

    • Added regression coverage for rollback behavior involving related records.

🔄 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/5608 **Author:** [@kolacheee](https://github.com/kolacheee) **Created:** 8/28/2026 **Status:** ✅ Merged **Merged:** 8/28/2026 **Merged by:** [@kolacheee](https://github.com/kolacheee) **Base:** `staging` ← **Head:** `feat/5592-phase3-write-cost` --- ### 📝 Commits (2) - [`04360aa`](https://github.com/Pasta-Devs/Marinara-Engine/commit/04360aa399e68e4f020d26cf71fd03b23142f2d7) perf(storage): share unchanged rows across write paths — rows are immutable once installed - [`941bfc5`](https://github.com/Pasta-Devs/Marinara-Engine/commit/941bfc58997f3e1f8ffcbeaf5ae36062992b03eb) style: format lazy-chat-units regression ### 📊 Changes **3 files changed** (+84 additions, -6 deletions) <details> <summary>View changed files</summary> 📝 `CHANGELOG.md` (+1 -0) 📝 `packages/server/src/db/file-backed-store.ts` (+26 -6) 📝 `scripts/regressions/lazy-chat-units.regression.ts` (+57 -0) </details> ### 📄 Description ## Why Part of #5592 — the largest remaining per-write cost from the original #4730 diagnosis. Every insert cloned the ENTIRE resident message table (`target.map(cloneRow)` — O(rows) object clones per message), and each transaction's first mutation per table deep-copied the whole array again. On the large profiles that produced the GC-fatal ~430 MB allocation spikes, sending one message allocated a full copy of every resident row. Stacked on #5607 (both touch the store; merge that first and this rebases clean). ## What The store's mutation paths already REPLACE rows rather than mutating them — with exactly one exception found by audit: the set-null cascade wrote `null` into live row objects in place. That last mutator is now copy-on-write, which promotes **row-object immutability to a store-wide invariant**, and sharing becomes safe everywhere: - inserts take a pointer-copy `slice()` of the table instead of per-row clones (~8 bytes/row transient instead of a full object clone per row), - transaction snapshots become shallow reference arrays instead of full row copies (insert-in-tx was previously 2× whole-table), - the merge mirror pushes references. Update paths already shared unmatched rows, so read-side behavior is unchanged: selects clone at projection (verified in #5597), so no caller ever held a live row reference. ## Regressions New block pins the invariant behaviorally: a rolled-back set-null must leave the child's foreign key intact in memory and on disk. Verified to FAIL under the dangerous half-state (shallow snapshots while the in-place mutator still existed) and pass with both halves — the half-state is precisely what a future in-place mutation would recreate, so the test guards the invariant, not just this change. Full local node suite 155/161 (only the six known Windows-checkout lanes), message-sharding green in both lazy and eager modes. ## Test plan - [x] `pnpm check` on a non-Windows checkout - [x] `pnpm regression` - [x] Manually verify on a LARGE profile: send messages in a big chat — same behavior, and per-send memory spikes visibly smaller in a heap profile - [x] Manually verify: transaction-heavy flows (imports, batch deletes) behave unchanged References #5592 and #4730 (do not auto-close). <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit * **Performance** * Improved transaction handling for large profiles by reducing memory allocation when creating snapshots. * **Bug Fixes** * Fixed transactional relationship updates so rolled-back changes correctly restore related data both in memory and after saving to disk. * **Tests** * Added regression coverage for rollback behavior involving related records. <!-- 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-23 04:14:14 +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#6844
No description provided.