[GH-ISSUE #2301] [Issue]: Every message create/edit/swipe deep-clones and deep-compares the entire global messages + message-swipes collections #1135

Closed
opened 2026-10-21 05:27:38 +07:00 by bsakaguchi · 1 comment
Owner

Originally created by @Romuromylus on GitHub (Jun 5, 2026).
Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2301

Originally assigned to: @Romuromylus on GitHub.

Summary

Severity: High — Performance. Every message mutation (create, edit, add/switch/delete swipe, branch copy) routes through write_message_and_swipes -> Storage::update_collections_atomically, which reads, deep-clones, and deep-compares the entire global messages and message-swipes collections. Cost scales with the total number of messages/swipes across every chat, not the active chat, and runs under the storage write lock on every turn.

Expected behavior

A single message create/edit/swipe should cost work proportional to the active chat (or to the changed rows), not to the entire message corpus. The conflict check in particular should not deep-compare the whole collection.

Actual behavior

update_collections_atomically (src-tauri/crates/storage/src/lib.rs:604-663), invoked for ["messages", "message-swipes"] by write_message_and_swipes (commands/storage/message_swipes.rs:359-390), performs, per single mutation, over the global collections (N = all messages, M = all swipes), all under the write lock:

  • read_collection_no_recovery returns cached.rows.clone() — full deep-clone of N (+M). A cache hit does not avoid the clone (cached_rows, lib.rs:730-740, always .clone()s the whole Vec).
  • entry.rows.clone() into original_rows — deep-clone #2.
  • Conflict check: read_collection_no_recovery(collection)? != *rows — deep-clone #3 plus a full serde_json::Value structural deep-equality over the entire N (+M):
for (collection, rows) in &original_rows {
    if self.read_collection_no_recovery(collection)? != *rows {
        return Err(AppError::new("storage_conflict", ...));
  • entry.rows.clone() into replacements — deep-clone #4.
  • replace_all_many_locked (lib.rs:1902) — serde_json::to_vec_pretty(rows) full re-serialize of both collections.

So roughly 4 full deep-clones + 1 full deep-equality + 1 full serialize of the entire global messages collection and the entire swipes collection, per mutation.

Hot path confirmed: create_message (message_swipes.rs:831/841/846), set_active_swipe, patch/edit (874), branch copy (911), and deletes (759/798) all route through write_message_and_swipes. messages / message-swipes are single global collections (one in-memory Vec<Value> across all chats; rows carry a chatId only for in-memory filtering) — there is no per-chat sharding, so cost scales with the total corpus. The append fast-path in create_locked (lib.rs:344-351) is dead for message creation: create_message never calls create_locked, and after the first chat load the collection is already cached.

Steps to reproduce

  1. Build up a corpus of several thousand messages across chats.
  2. Send a message, add/switch/delete a swipe, or edit a message.
  3. Observe each mutation pays multiple full-corpus clones + a full deep-equality pass (latency grows with total message count, not the active chat).

Impact area

  • Chat mode
  • Roleplay mode
  • Game mode
  • Tauri/Rust storage, assets, LLM, integrations, security, imports, or HTTP runtime

Environment

  • Branch or commit: refactor @ 47b16701
  • Run mode: Tauri desktop (same storage layer in server mode)
  • OS + version: not relevant (static source audit)
  • Node + pnpm versions: not relevant
  • Rust version, if relevant: not relevant
  • Remote Runtime URL configured? not relevant (applies to both)
  • Provider, model, or integration, if relevant: n/a

Logs, screenshots, or video

Static source audit; claims cited to file:line above.

Regression notes

Found via static audit of the refactor branch; not verified against main.

Additional context

Two fix directions:

  1. Cheap conflict guard — replace the O(n) read_collection_no_recovery != original_rows deep-equality with the already-computed CollectionFileStamp.content_signature (a version/stamp guard), eliminating the most wasteful step without changing the storage format.
  2. Per-chat sharding — store messages/swipes one collection (file) per chatId so reads/writes/compares scale with the active chat. Larger change, addresses the clone cost as well.

Template check

Please uncheck the box below before submitting so we know you read the template. It is intentionally pre-checked:

  • I DID NOT read this template and provide the requested details.
Originally created by @Romuromylus on GitHub (Jun 5, 2026). Original GitHub issue: https://github.com/Pasta-Devs/Marinara-Engine/issues/2301 Originally assigned to: @Romuromylus on GitHub. ## Summary **Severity: High — Performance.** Every message mutation (create, edit, add/switch/delete swipe, branch copy) routes through `write_message_and_swipes -> Storage::update_collections_atomically`, which reads, deep-clones, and deep-**compares** the entire **global** `messages` and `message-swipes` collections. Cost scales with the *total* number of messages/swipes across every chat, not the active chat, and runs under the storage write lock on every turn. ## Expected behavior A single message create/edit/swipe should cost work proportional to the active chat (or to the changed rows), not to the entire message corpus. The conflict check in particular should not deep-compare the whole collection. ## Actual behavior `update_collections_atomically` (`src-tauri/crates/storage/src/lib.rs:604-663`), invoked for `["messages", "message-swipes"]` by `write_message_and_swipes` (`commands/storage/message_swipes.rs:359-390`), performs, per single mutation, over the global collections (N = all messages, M = all swipes), all under the write lock: - `read_collection_no_recovery` returns `cached.rows.clone()` — full deep-clone of N (+M). A cache hit does **not** avoid the clone (`cached_rows`, `lib.rs:730-740`, always `.clone()`s the whole Vec). - `entry.rows.clone()` into `original_rows` — deep-clone #2. - Conflict check: `read_collection_no_recovery(collection)? != *rows` — deep-clone #3 **plus** a full `serde_json::Value` structural deep-equality over the entire N (+M): ```rust for (collection, rows) in &original_rows { if self.read_collection_no_recovery(collection)? != *rows { return Err(AppError::new("storage_conflict", ...)); ``` - `entry.rows.clone()` into `replacements` — deep-clone #4. - `replace_all_many_locked` (`lib.rs:1902`) — `serde_json::to_vec_pretty(rows)` full re-serialize of both collections. So roughly **4 full deep-clones + 1 full deep-equality + 1 full serialize** of the entire global messages collection and the entire swipes collection, per mutation. Hot path confirmed: `create_message` (`message_swipes.rs:831/841/846`), `set_active_swipe`, patch/edit (874), branch copy (911), and deletes (759/798) all route through `write_message_and_swipes`. `messages` / `message-swipes` are **single global collections** (one in-memory `Vec<Value>` across all chats; rows carry a `chatId` only for in-memory filtering) — there is no per-chat sharding, so cost scales with the total corpus. The append fast-path in `create_locked` (`lib.rs:344-351`) is dead for message creation: `create_message` never calls `create_locked`, and after the first chat load the collection is already cached. ## Steps to reproduce 1. Build up a corpus of several thousand messages across chats. 2. Send a message, add/switch/delete a swipe, or edit a message. 3. Observe each mutation pays multiple full-corpus clones + a full deep-equality pass (latency grows with total message count, not the active chat). ## Impact area - [x] Chat mode - [x] Roleplay mode - [x] Game mode - [x] Tauri/Rust storage, assets, LLM, integrations, security, imports, or HTTP runtime ## Environment - Branch or commit: refactor @ 47b16701 - Run mode: Tauri desktop (same storage layer in server mode) - OS + version: not relevant (static source audit) - Node + pnpm versions: not relevant - Rust version, if relevant: not relevant - Remote Runtime URL configured? not relevant (applies to both) - Provider, model, or integration, if relevant: n/a ## Logs, screenshots, or video Static source audit; claims cited to file:line above. ## Regression notes Found via static audit of the refactor branch; not verified against main. ## Additional context Two fix directions: 1. **Cheap conflict guard** — replace the O(n) `read_collection_no_recovery != original_rows` deep-equality with the already-computed `CollectionFileStamp.content_signature` (a version/stamp guard), eliminating the most wasteful step without changing the storage format. 2. **Per-chat sharding** — store messages/swipes one collection (file) per `chatId` so reads/writes/compares scale with the active chat. Larger change, addresses the clone cost as well. ## Template check Please uncheck the box below before submitting so we know you read the template. It is intentionally pre-checked: - [ ] I DID NOT read this template and provide the requested details.
bsakaguchi 2026-10-21 05:27:38 +07:00
Author
Owner

@Romuromylus commented on GitHub (Jun 6, 2026):

Fixed by #2399, merged into refactor.

<!-- gh-comment-id:4637947199 --> @Romuromylus commented on GitHub (Jun 6, 2026): Fixed by #2399, merged into `refactor`.
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#1135
No description provided.