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 rejectsSimTier.None(would dispatch zero clusters),CellAmortize > 0without a non-Alltier filter,CellAmortize > 0combined with a change filter (it would drop the changes of every cluster outside the current bucket), and tier filters on system types other thanQuerySystem.CellAmortizeskips 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 β guardBudgetMs == 0before computing a utilization ratio.- A system's
Tier(...)and its inputView.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
π Related
- Source: src/Typhon.Engine/Runtime/public/SimTier.cs, SystemBuilder.cs (
Tier,CellAmortize), Dag.cs (tier:/cellAmortize:parameters) - Source: src/Typhon.Engine/Ecs/internals/TierClusterIndex.cs (per-archetype tier-grouped index, dual invalidation, merge cache)
- Source: src/Typhon.Engine/Runtime/public/TyphonRuntime.cs (
OnParallelQueryPreparetier-scoped partition,BuildTierIndexesAtTickStart) - Parent feature: Spatial Tiers & Adaptive Dispatch
- Same mechanism, cell/grid-configuration angle: Spatial category β Tiered Simulation Dispatch