Skip to content

perf: optimize rx-storage-helper.ts write path#8162

Merged
pubkey merged 2 commits intomasterfrom
copilot/optimize-performance-rx-storage-helper
Mar 24, 2026
Merged

perf: optimize rx-storage-helper.ts write path#8162
pubkey merged 2 commits intomasterfrom
copilot/optimize-performance-rx-storage-helper

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 23, 2026

Targeted optimizations to categorizeBulkWriteRows() and related helpers on the hot write path.

Results (node:memory, 40 runs)

Metric Before After Δ
insert-documents-500 0.24 ms 0.21 ms -12.5%
serial-inserts-50 1.35 ms 1.24 ms -8.1%
find-by-query-parallel-4 2.79 ms 2.55 ms -8.6%
serial-find-by-id-50 0.41 ms 0.38 ms -7.3%
time-to-first-insert 0.93 ms 0.88 ms -5.4%

Read-path metrics unchanged (within noise).

Changes

  • Eliminate redundant stripAttachmentsDataFromRowupdatedRow was already stripped on creation, then stripped again when pushed to bulkUpdateDocs
  • Reuse stripped documents for eventscategorizeBulkWriteRows called stripAttachmentsDataFromDocument() separately for event creation despite having the stripped doc from insertedRow/updatedRow already available
  • Replace Object.entries().forEach() / .find() with indexed Object.keys() loops in all attachment iteration paths — eliminates intermediate array + callback overhead
  • flatCloneDocWithMeta: { ...doc, _meta: { ...doc._meta } } instead of Object.assign({}, doc, { _meta: flatClone(doc._meta) }) — one fewer intermediate object
  • stripAttachmentsDataFromDocument: same Object.keys() loop treatment
  • Simplify insert path: remove insertedIsDeleted ternary indirection, merge duplicate hasAttachments branches

Before (update path stripped twice):

const updatedRow = hasAttachments ? stripAttachmentsDataFromRow(writeRow) : writeRow;
// ... later ...
bulkUpdateDocs.push(stripAttachmentsDataFromRow(updatedRow)); // redundant
eventDocumentData = stripAttachmentsDataFromDocument(document); // also redundant

After:

const updatedRow = hasAttachments ? stripAttachmentsDataFromRow(writeRow) : writeRow;
bulkUpdateDocs.push(updatedRow);
eventDocumentData = hasAttachments ? updatedRow.document : document;

Copilot AI and others added 2 commits March 23, 2026 14:00
- Eliminate redundant stripAttachmentsDataFromRow() call in update path
  (updatedRow was already stripped, then stripped again when pushed)
- Cache and reuse stripped documents for event creation instead of
  calling stripAttachmentsDataFromDocument() again
- Replace Object.entries().forEach() and Object.entries().find() with
  indexed for loops using Object.keys() to avoid callback overhead
- Optimize flatCloneDocWithMeta() to use spread syntax instead of
  Object.assign + flatClone (fewer intermediate objects)
- Simplify insert path by removing redundant branches and using
  documentDeleted directly instead of ternary conversion
- Merge duplicate hasAttachments branches in insert/update paths

Performance results (node:memory, 40 runs):
- insert-documents-500: 0.24 → 0.21 ms (-12.5%)
- serial-inserts-50: 1.35 → 1.24 ms (-8.1%)
- find-by-query-parallel-4: 2.79 → 2.55 ms (-8.6%)
- serial-find-by-id-50: 0.41 → 0.38 ms (-7.3%)
- time-to-first-insert: 0.93 → 0.88 ms (-5.4%)

Co-authored-by: pubkey <8926560+pubkey@users.noreply.github.com>
Agent-Logs-Url: https://github.com/pubkey/rxdb/sessions/9212f597-676d-4a25-91da-c55b55fedd77
@pubkey pubkey marked this pull request as ready for review March 23, 2026 14:42
@pubkey pubkey merged commit 0ce6ecc into master Mar 24, 2026
45 of 46 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants