Table of Contents

Tier-Filtered & Amortized Dispatch

Scope a system to clusters in matching tier cells, and optionally process just 1/N of them per tick.

Status: βœ… Implemented Β· Visibility: Public Β· Level: 🟣 Advanced Β· Category: Runtime

🎯 What it solves

Not every system should touch every entity every tick. A combat AI needs full fidelity near the camera but is wasted work on the far side of the map; a slow-decay or idle-drift update is correct even if it only runs once every few seconds for distant entities. Without engine support, a system author would have to thread a distance/tier check through every entity loop by hand β€” exactly the per-entity overhead tiering exists to avoid. Tier-filtered dispatch lets a system declare its tier scope once at registration; cell amortization lets it additionally spread coarse-tier work across ticks instead of doing all of it at once.

βš™οΈ How it works (in brief)

A system declares a SimTier filter (default SimTier.All, meaning unfiltered β€” the pre-tiering fast path). At dispatch-prepare time the scheduler reads the system's archetype's per-tier cluster list (rebuilt once per tick, before any system runs) and restricts the parallel chunk partition to just that list β€” never touching clusters outside the filter. Adding CellAmortize(N) further strides that list into N rotating buckets keyed by tickNumber % N, so the system sees 1/N of its tier's clusters on any given tick; TickContext.AmortizedDeltaTime is scaled to DeltaTime Γ— N so integration math (decay, drift, movement) stays correct across the longer effective step. A View's own WithTier(...) filter applies the same cluster scoping independently; a system's filter and its input view's filter combine by bit-AND.

πŸ’» Usage

var game = schedule.PublicTrack.DeclareDag("Game");

// Full fidelity, every tick β€” Tier 0 cells only.
game.QuerySystem("CombatAi", ctx =>
{
    foreach (var id in ctx.Entities) { /* full-fidelity AI */ }
}, input: () => antsView, parallel: true, tier: SimTier.Tier0);

// Coarse tier: process 1/60th of Tier 2's clusters per tick, amortizing the integration step.
game.QuerySystem("IdleDrift", ctx =>
{
    foreach (var id in ctx.Entities)
    {
        var entity = ctx.Accessor.OpenMut(id);
        var pos = entity.Read(Ant.Position);
        pos.X += DriftSpeed * ctx.AmortizedDeltaTime;
        entity.Set(Ant.Position, pos);   // ~1s of drift, once every 60th tick
    }
}, input: () => antsView, parallel: true, tier: SimTier.Tier2, cellAmortize: 60);

// Equivalent class-based form (SystemBuilder):
// protected override void Configure(SystemBuilder b) => b.Name("IdleDrift").Tier(SimTier.Tier2).CellAmortize(60).Parallel();
Option Default Effect
tier: (QuerySystem) / b.Tier(...) (SystemBuilder) SimTier.All Restrict dispatch to clusters in matching cells; flags combine (SimTier.Near = Tier0 \| Tier1)
cellAmortize: / b.CellAmortize(N) 0 (off) Process 1/N of the tier's clusters per tick, rotating buckets by tick number; requires a non-All tier
realms: / b.InRealm(r), b.InRealms(...) every runnable realm Restrict dispatch to the clusters of those realms (Realms); intersects with the tier

⚠️ Guarantees & limits

  • Filtering operates on cluster lists, never per-entity β€” a tier-filtered system's chunk partition is built only over its tier's clusters, with zero scanning of clusters outside the filter.
  • Build()-time validation rejects SimTier.None (would dispatch zero clusters), CellAmortize > 0 without a non-All tier filter, CellAmortize > 0 combined with a change filter (it would drop the changes of every cluster outside the current bucket), and tier filters on system types other than QuerySystem.
  • CellAmortize skips whole untouched clusters between buckets β€” it is bucket rotation, not per-entity modulo, so the 59/60 you skip in a tick are never iterated, not iterated-and-ignored.
  • Multi-tier flag combinations (e.g. SimTier.Near, SimTier.Active) are merged once per rebuild and cached by flag byte β€” no per-system merge allocation for repeated combinations.
  • TickContext.TierBudgetMetrics (per-tier cost and entity count from the previous tick) is all-zero on the first tick β€” guard BudgetMs == 0 before computing a utilization ratio.
  • A system's Tier(...) and its input View.WithTier(...) combine by bit-AND; a non-overlapping combination throws at dispatch-prepare time rather than silently materializing zero entities.

πŸ§ͺ Tests

  • TierDispatchTests β€” Build_TierNone_Throws, Build_CellAmortizeWithoutTier_Throws, TierDispatch_Tier0System_SeesOnlyTier0Entities, TierDispatch_CellAmortize_ProcessesEachCellExactlyOncePerCycle, TierDispatch_AmortizedDeltaTime_EqualsDeltaTimeTimesAmortize