Skip to content

perf: avoid rescanning predecessors on removal - #229

Open
anaibol wants to merge 1 commit into
colyseus:masterfrom
anaibol:codex/optimize-schema-removal
Open

perf: avoid rescanning predecessors on removal#229
anaibol wants to merge 1 commit into
colyseus:masterfrom
anaibol:codex/optimize-schema-removal

Conversation

@anaibol

@anaibol anaibol commented Jul 28, 2026

Copy link
Copy Markdown

Summary

  • start cached-position updates at the removed node successor
  • decrement only successor positions instead of rescanning the linked list from its head
  • add a deterministic nested-schema regression test covering traversal, cached positions, and encode/decode correctness

Why

Root.remove recursively removes every ChangeTree in a detached subtree. The previous updatePositionsAfterRemoval implementation walked from the list head for every removed node even though predecessor positions cannot change. With many long-lived schemas before a removed nested actor, removal cost therefore scales with both the preceding list and removed subtree.

Starting at node.next makes the work proportional only to actual successors. Tail removals require no position traversal. This does not change the wire format or removal semantics.

Benchmark

Node 22.23.1, five runs, 2,000 preceding branches with 24 nested leaves each and one removed branch with 24 leaves:

  • master median: 6.205 ms
  • this change median: 0.047 ms

The regression test fails on master because predecessor links are revisited and passes with this change.

Verification

  • npm test
  • npm run build
  • npm run typecheck
  • focused Root test

@anaibol

anaibol commented Aug 24, 2026

Copy link
Copy Markdown
Author

@endel can you take a look into this please?

@endel

endel commented Aug 24, 2026

Copy link
Copy Markdown
Member

Hi @anaibol, sorry for lack of response here - thanks for your PR, I'm about to release version Colyseus 0.18 + schemas 5.0 and this issue has been addressed differently on that version!

If you can upgrade already, 0.18 is currently in preview, but it should be released this week!
See docs for 0.18: https://deploy-preview-222--colyseus-docs.netlify.app/

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