Skip to content

Feature: Entity Partition Reuse

Tsu edited this page May 31, 2026 · 1 revision

This feature keeps entity partitioning cheap under high entity counts by reusing partition structures across ticks.

What It Changes

Vanilla-style partition rebuild clears the dictionary each tick and recreates per-chunk/per-grid lists as entities are repartitioned.

Stratum keeps the partition dictionary and list instances, clears active chunks in-place, and only trims cold chunks occasionally.

Key Patch Snippet

From patches/VSEssentials/Systems/EntityPartitioning.cs.patch:

var activeChunks = stratumActiveChunks;
for (int i = 0; i < activeChunks.Count; i++)
{
    var c = activeChunks[i];
    var ents = c.Entities;
    for (int g = 0; g < ents.Length; g++)
    {
        var list = ents[g];
        if (list != null && list.Count > 0) list.Clear();
    }
    ...
}
activeChunks.Clear();
int generation = ++stratumGeneration;

The patch tracks whether a chunk was used this generation:

if (partition.StratumGeneration != generation)
{
    partition.StratumGeneration = generation;
    activeChunks.Add(partition);
}

Cold Chunk Trim

Instead of rebuilding every tick forever, Stratum trims unused chunks periodically:

private const int StratumTrimIntervalTicks = 300;
private const int StratumTrimChunkCountThreshold = 512;
private const int StratumTrimUnusedGenerations = 16;
if (currentGeneration - kv.Value.StratumGeneration > StratumTrimUnusedGenerations)
{
    (toRemove ??= new List<long>()).Add(kv.Key);
}

Largest Touch Distance Clamp

The patch also clamps partition search expansion caused by oversized mod entities:

public static double StratumLargestTouchDistanceCap = 6.0;

Why It Helps

  • less allocation churn per tick
  • less GC pressure in crowded worlds
  • stable partition behavior after large crowds disperse

Related Files

  • patches/VSEssentials/Systems/EntityPartitioning.cs.patch
  • VSEssentials/Systems/EntityPartitioning.cs

Clone this wiki locally