perf(client): cache parsed chunk/batch origin coordinates#31
Open
RZDESIGN wants to merge 1 commit intohytopiagg:mainfrom
Open
perf(client): cache parsed chunk/batch origin coordinates#31RZDESIGN wants to merge 1 commit intohytopiagg:mainfrom
RZDESIGN wants to merge 1 commit intohytopiagg:mainfrom
Conversation
chunkIdToOriginCoordinate() and batchIdToBatchOrigin() are called in
hot per-frame loops (view distance culling, batch sorting, light
volumes). Each call does split(',') + .map(Number) which allocates
an intermediate array, a closure, a mapped array, and an {x,y,z}
object — 5 allocations per call.
With ~200 active batches, applyBatchViewDistance() alone produces
~1 000 throwaway objects per frame. Add a Map<string, Vector3Like>
cache for each method so the parsing and allocation happen only once
per unique ID. All existing callers are read-only so caching the
returned objects is safe.
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
Chunk.chunkIdToOriginCoordinate()andChunk.batchIdToBatchOrigin()convert string IDs like"32,0,-64"into{ x, y, z }objects. They're called in hot per-frame loops:ChunkMeshManager.applyBatchViewDistance()ChunkManagerbatch sortingLightLevelManager/SkyDistanceVolumeManagerChunkWorkerChunk.getChunkIdsInBatch()/chunkIdToBatchId()Each uncached call performs:
split(',')→ allocates anArray<string>.map(Number)→ allocates a closure + a newArray<number>{ x, y, z }→ allocates a result objectThat's 5 allocations per call. With ~200 active batches,
applyBatchViewDistance()alone creates ~1,000 throwaway objects per frame — pure GC pressure for a deterministic mapping that never changes.Fix
Add a module-level
Map<string, Vector3Like>cache for each method. The first call for a given ID parses and caches; all subsequent calls return the cached object. Since the mapping from string ID to coordinates is deterministic and all callers are read-only, this is completely safe.Changes
client/src/chunks/Chunk.tschunkOriginCacheandbatchOriginCachemaps; wrapped both methods with cache-first lookupRisk Assessment
"x,y,z" → { x, y, z }is pure and deterministic — caching it cannot produce incorrect results..x,.y,.zwithout mutation. If a future caller were to mutate the returned object, it would corrupt the cache — but this would also be a bug in the caller, not in the cache.Test Plan