Skip to content

Feature: Block Tick Smoothing

Tsu edited this page May 31, 2026 · 1 revision

This feature limits and smooths block tick work across main-thread queued ticks and off-thread random ticks.

Where It Lives

Main patch:

  • patches/VintagestoryLib/Vintagestory.Server/ServerSystemBlockSimulation.cs.patch

Main-Thread Queued Tick Cap

In UpdateEvery100ms, Stratum caps per-pass queued block ticks using Performance.BlockTicks:

int maxMainThreadBlockTicks = server.Config.MaxMainThreadBlockTicks;
if (stratumBlockTicks.Enabled)
{
    maxMainThreadBlockTicks = Math.Min(maxMainThreadBlockTicks, Math.Max(1, stratumBlockTicks.MaxMainThreadBlockTicksPerPass));
}

Then it processes at most that many queued ticks and records stats/timing.

Random Tick Scope and Rotation

In OnSeparateThreadTick, Stratum can limit random tick chunk range via simulation distance:

if (stratumSimulationDistance.Enabled && stratumSimulationDistance.LimitRandomTicks)
{
    int simulationChunkRange = (int)Math.Ceiling((double)stratumSimulationDistance.RandomTickDistanceBlocks / MagicNum.ServerChunkSize);
    blockTickChunkRange = Math.Min(blockTickChunkRange, simulationChunkRange);
}

It also caps chunks processed per pass and rotates start offset so the same chunks are not always first:

int maxChunksToTick = stratumBlockTicks.Enabled ? Math.Min(chunksSeen, Math.Max(1, stratumBlockTicks.MaxChunksPerPass)) : chunksSeen;
int rotationOffset = chunksSeen > maxChunksToTick && maxChunksToTick > 0 ? stratumBlockTickRotation % chunksSeen : 0;
...
stratumBlockTickRotation = (rotationOffset + Math.Max(1, maxChunksToTick)) % chunksSeen;

Adaptive Random-Tick Cap Under Overload

When average tick time is above OverloadTickMs, random ticks per chunk are scaled down:

if (avgMs > stratumBlockTicks.OverloadTickMs)
{
    int baseCap = Math.Max(0, stratumBlockTicks.MaxRandomTicksPerChunk);
    int scaled = (int)Math.Floor(baseCap * stratumBlockTicks.OverloadScale);
    stratumAdaptiveRandomTicksPerChunk = Math.Max(stratumBlockTicks.OverloadFloor, scaled);
}

tickChunk(...) then applies both normal and adaptive caps:

if (stratumBlockTicks.Enabled)
{
    randomBlockTicksPerChunk = Math.Min(randomBlockTicksPerChunk, Math.Max(0, stratumBlockTicks.MaxRandomTicksPerChunk));
}
if (stratumAdaptiveCap >= 0)
{
    randomBlockTicksPerChunk = Math.Min(randomBlockTicksPerChunk, stratumAdaptiveCap);
}

Instrumentation

The patch records:

  • StratumRuntime.PerformanceStats.RecordMainThreadBlockTicks(...)
  • StratumRuntime.PerformanceStats.RecordBlockTickPass(...)
  • StratumRuntime.Timings.RecordElapsed("block.queuedTicks", ...)
  • StratumRuntime.Timings.RecordElapsed("block.randomTicks", ...)

Related Files

  • patches/VintagestoryLib/Vintagestory.Server/ServerSystemBlockSimulation.cs.patch
  • VintagestoryLib/Vintagestory.Server/ServerSystemBlockSimulation.cs
  • sources/VintagestoryLib/Vintagestory/Server/StratumConfig.cs (Performance.BlockTicks, Performance.SimulationDistance)

Clone this wiki locally