Skip to content

Lazily allocate ChunkEntitySlices state - #199

Open
wphillipp wants to merge 1 commit into
Tuinity:mc/26.2from
wphillipp:perf/lazy-chunk-entity-slices-state
Open

Lazily allocate ChunkEntitySlices state#199
wphillipp wants to merge 1 commit into
Tuinity:mc/26.2from
wphillipp:perf/lazy-chunk-entity-slices-state

Conversation

@wphillipp

Copy link
Copy Markdown

Summary

Lazily allocate ChunkEntitySlices state that is unused for most chunks.

This avoids retaining entity lists, section collections, and lookup maps for empty chunks.

Changes

  • Lazily allocate entity lists and section collections.
  • Only allocate hard-collision state when needed.
  • Allocate type/class maps after the first entity is added.
  • Keep individual class lookup collections lazy.
  • Keep allocated state after first use to avoid allocation churn.

Memory impact

In a 625 full chunk test, this saved about 0.7 MiB of directly attributable retained memory, or roughly 1.2 KiB per full chunk.

Object Baseline Optimized
EntityCollectionBySection 1,440 27
EntityList 628 10
Reference2ObjectOpenHashMap 7,213 5,977
Int2IntOpenHashMap 628 10

Direct heap graph analysis puts the retained saving at about 0.714 MiB per 625 full chunks.

With view-distance=10 and simulation-distance=10, an isolated player keeps about 625 full chunks loaded in this workload. For players far enough apart that their chunk areas do not overlap, this scales roughly to:

Players Full chunks Estimated saving
10 6,250 ~7.1 MiB
20 12,500 ~14.3 MiB
50 31,250 ~35.7 MiB
100 62,500 ~71.4 MiB

Actual savings will be lower when players are close enough for their loaded chunk areas to overlap.

Validation

Tested entity add/remove, section movement, cross-chunk movement, hard-colliding entities, save/unload, reuse after becoming empty, and unload/reload.

Mixin audit and full build pass.

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