Skip to content

Avoid redundant work when applying entity state updates - #823

Merged
bramkragten merged 3 commits into
masterfrom
optimize-apply-state-updates
Dec 4, 2026
Merged

bramkragten merged 3 commits into
masterfrom
optimize-apply-state-updates

Conversation

@MindFreeze

@MindFreeze MindFreeze commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Two small optimizations in processEvent (lib/states.ts) with no API changes:

  1. Rebuild attributes without delete on attribute removal — delete attributes[key] permanently transitions the object into V8 dictionary mode, slowing down subsequent spreads and property reads on that entity.
  2. Skip { ...store.state } copy and notification on no-op incremental updates — lazily copy store.state only when an incremental message actually adds, removes, or updates a known entity (empty replace snapshots still publish {}).

Benchmarks (Chromium 158, 1× and 6× CPU throttling, Node v24)

Benchmark Before (Node / Chr 1× / Chr 6×) After (Node / Chr 1× / Chr 6×) Speedup (Chr 6×)
1,000 alternating attribute add/remove mutations 0.65 / 0.41 / 2.74 ms 0.07 / 0.06 / 0.39 ms 7.0×
1,000 subsequent { ...attributes } spreads after removal 0.13 / 0.09 / 0.59 ms 0.02 / 0.01 / 0.09 ms 7.0×
100,000 subsequent attribute property reads after removal 1.42 / 0.28 / 2.10 ms 0.07 / 0.07 / 0.48 ms 4.4× (19× Node)
Skipped no-op { ...store.state } copy at 5,000 entities 0.80 / 0.48 / 3.25 ms / msg 0 ms Avoids full copy + subscriber notify

@MindFreeze
MindFreeze marked this pull request as ready for review December 3, 2026 11:12
@bramkragten

Copy link
Copy Markdown
Member

@copilot resolve the merge conflicts in this pull request

Copilot AI commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

@copilot resolve the merge conflicts in this pull request

Resolved in 986ced5 by merging master and preserving both the optimizations and upstream batching support. All 81 tests and the build pass; CodeQL found no alerts.

Comment thread lib/states.ts Resolved
@bramkragten
bramkragten merged commit 7dcfa07 into master Dec 4, 2026
2 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.

3 participants