Skip to content

perf(bindx): index dirty relation lookups by owner - #110

Merged
matej21 merged 1 commit into
mainfrom
fix/dirty-relation-scan
Sep 10, 2026
Merged

perf(bindx): index dirty relation lookups by owner#110
matej21 merged 1 commit into
mainfrom
fix/dirty-relation-scan

Conversation

@matej21

@matej21 matej21 commented Sep 10, 2026

Copy link
Copy Markdown
Member

Summary

Fix a quadratic dirty-relation scan observed during datagrid filtering. For every entity, collectDirtyRelations previously walked all has-one and has-many relations in the store. The captured profile attributed about 82% of total time to isEntityDirty, including its relation checks.

  • Add a relation-owner index to both relation stores.
  • Maintain it through existing write/delete/clear paths, including import and owner rekeying.
  • Look up only the current entity's relations during dirty detection, reducing the relation traversal from O(entities × relations) to O(entities + relations) for a full scan.
  • Cover disconnected relations, placeholder-only changes, owner isolation, reset, commit, import, rekey, removal, and clear.

Verification

  • Full non-browser suite: 2,066 passed, 0 failed.
  • Typecheck and ESLint on changed files passed.
  • git diff --check passed.
  • Both complexity regression tests fail with the original loops: 200 owners produce 40,000 prefix checks per relation store, exceeding the linear bound.

Synthetic benchmark

Measured the complete getAllDirtyEntities() call on clean stores with one has-one and one has-many relation per entity. Compared the base implementation against this change on identical data using Bun, reserved CPU cores with idle SMT siblings, three warmups, and the median of nine alternating samples. Setup is excluded.

Entities Relations Before After Speedup
1,000 2,000 39.1 ms 2.4 ms 16×
5,000 10,000 830.5 ms 11.6 ms 71×
10,000 20,000 6,615.9 ms 33.0 ms 200×

These measure the dirty scan, not end-to-end filtering. The affected application still needs a browser profile with the fix.

@matej21
matej21 merged commit 770aad7 into main Sep 10, 2026
4 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.

1 participant