perf(server): reuse position/rotation arrays in network entity sync#32
Open
RZDESIGN wants to merge 1 commit intohytopiagg:mainfrom
Open
perf(server): reuse position/rotation arrays in network entity sync#32RZDESIGN wants to merge 1 commit intohytopiagg:mainfrom
RZDESIGN wants to merge 1 commit intohytopiagg:mainfrom
Conversation
Physics runs at 60 Hz but network sync at 30 Hz (every 2 ticks). Each tick, _onEntityUpdatePosition and _onEntityUpdateRotation allocated a fresh [x,y,z] / [x,y,z,w] array for every moving entity, even though the previous tick's array was already sitting on the same sync object. The first tick's array became garbage on tick 2. With 100 moving entities this produced ~6 000 throwaway arrays per second. Reuse the existing array when one is already present on the sync object, falling back to allocation only on the first update in each sync cycle. Made-with: Cursor
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Physics runs at 60 Hz but network synchronization runs at 30 Hz (every 2 ticks, controlled by
TICKS_PER_NETWORK_SYNC). Each tick,Entity.checkAndEmitUpdates()firesUPDATE_POSITION/UPDATE_ROTATIONevents for every moving entity, which theNetworkSynchronizerhandles by allocating a new[x, y, z]or[x, y, z, w]array and assigning it to the entity sync object.Since the sync object persists across the 2-tick window, the first tick's array is silently discarded when the second tick overwrites it — creating a throwaway array that immediately becomes garbage.
Before
Every call allocates a new array, even when one already exists on the sync object.
After
Reuse the existing array when present (in-place index writes); only allocate on the first update in each sync cycle.
Impact
With 100 moving entities, this eliminates ~6,000 short-lived arrays per second (100 entities × 2 arrays × 30 wasted allocations/s). On busy worlds with 200+ entities the savings double. This directly reduces minor GC frequency and pause time on the server.
Changes
server/src/networking/NetworkSynchronizer.ts_onEntityUpdatePosition: reuse existing.parray via index writesserver/src/networking/NetworkSynchronizer.ts_onEntityUpdateRotation: reuse existing.rarray via index writesRisk Assessment
[0],[1],[2](and[3]for quaternions), which are identical whether the array is new or reused._createEntitySync→{ i: entity.id! }. On first position update in a cycle, theelsebranch allocates. On subsequent ticks in the same cycle, theifbranch reuses. After_clearSyncQueue, everything is discarded cleanly.Test Plan
--smolor V8--trace-gc) — expect fewer minor GC cycles