## Summary Closes the Cold-cache regression flagged in PR #2450. `StepCache.TryGetMask` no longer eagerly runs `BuildChunk` on the first miss for a chunk that isn't in a `.swb` lazy reader. Instead it returns `Fallthrough_NotBuilt` and the caller (`BitmapAStarAlgorithm`) takes the per-cell slow path. The chunk is only promoted to the bitmap fast path after the **second** miss within a 30-second window, filtering single-touch pass-throughs. This makes BitmapAStar's worst-case (cold cache + short hops) collapse from **12–47× slower** than FastAStar to **roughly the same**, which is the floor the slow path can deliver. Steady-state warm performance (the actual deliverable) is unchanged from PR-5 — it was always the cache fast path. ## The pet-follow scenario this fixes A mounted player at ~4 tiles/sec with a pet/hireable following will trigger an NPC pathfind every 100–300 ms. Each pathfind is 1–6 tiles. As the player crosses chunk boundaries (~4 sec/chunk), the pet's first pathfind in the new chunk under the previous behavior triggered a full ~700 µs `BuildChunk` for a chunk the player would leave shortly after. At 50–100 mobiles per shard, this exceeded the 8 ms tick budget. PR-5 BDN data showed scenarios 6–9 (2–8 tile NPC perception) at 2,300–3,700 µs Cold vs FastAStar's 80–200 µs. Under the new gate: - First miss → `Fallthrough_NotBuilt` → caller uses slow path (~30–50 µs short path). No `BuildChunk`. No allocation. - Player keeps moving → chunk never gets a second touch within window → never promoted, no rot. - NPC patrolling a fixed territory → repeatedly hits the same chunks → second touch within window → promote → cache fast path on subsequent calls. ## What changed - **`CacheHitKind.Fallthrough_NotBuilt = 6`** + **`CacheStats.FallthroughNotBuilt`** counter. `IsHit=false`, so the caller routes to slow path. - **`StepCache._chunkMissTracker`** — `Dictionary<long, ChunkMissState>` capped at 4096 entries. State is `(byte missCount, uint lastMissTickStamp)` keyed by chunk key. Window-expired entries reset count to 1; capacity overflow prunes window-old entries first. - **`StepCache.MissPromotionThreshold`** (default `2`) and **`StepCache.MissPromotionWindowMs`** (default `30_000`) — tunable, can be wired through `ServerConfiguration` if shards want different policy. Setting threshold to `1` restores legacy eager-build behavior (used by tests that prime chunks via single `TryGetMask` call). - **`StepCache.TryGetMask` miss branch** — try lazy reader first (file-loaded chunks bypass the tracker entirely; an `.swb` represents an explicit prior decision to keep the chunk warm). Otherwise consult the tracker. - **`BitmapAStarAlgorithm.GetSuccessorsSlowPath`** now layers `IsBlockedByDynamic` on top of `CalcMoves.CheckMovement`. Previously the slow path only ran for `CanFly` creatures and rare cache fallthroughs — `CheckMovement` doesn't iterate same-cell mobiles, so the bitmap fast path's `IsBlockedByDynamic` was the only mobile-blocking check. Now first-touch pathfinds run through the slow path, so the gap had to close. ## Tests 50 pathfinding tests pass (was 47). New / updated: - **`TryGetMask_FirstTouchOnUnbuiltChunk_DefersBuildAndReturnsFallthrough`** — single TryGetMask call returns `Fallthrough_NotBuilt`, no chunk built, no allocation. - **`TryGetMask_SecondTouchWithinWindow_PromotesAndBuilds`** — second call inside the 30s window builds + serves. - **`TryGetMask_SecondTouchAfterWindow_RestartsCounterAndDefers`** — second call outside the window restarts the count, returns Fallthrough again. - **`TryGetMask_DistinctChunks_TrackedIndependently`** — counters are per-chunk; one touch on each of two adjacent chunks both stay in fallthrough. - **`LazyReaderHit_BypassesMissTrackerOnFirstTouch`** — open `.swb` + first touch hits without consulting the tracker. Production with `.swb` loaded skips the gate entirely. - **`MultisVersion_Bump_TriggersDirtyRebuild`** — updated to reflect the new 3-step flow (Fallthrough → Miss_NotBuilt → Miss_DirtyRebuild). - Tests that prime chunks via a single `TryGetMask` call (multi-Z, Tier4, lifecycle, parity, BitmapAStar uses-cache) set `MissPromotionThreshold = 1` to opt into eager behavior. ## Expected BDN impact The Cold column from PR-5's BDN should change as follows once the bench's submodule pointer is updated to this branch: | # | Scenario | Cold (PR-5) | Cold (PR-6 expected) | FastAStar Cold | |--:|-----------------|-------------:|---------------------:|---------------:| | 2 | sewer corridor | 1,627 µs | ~36 µs | 36 µs | | 4 | causeway | 1,533 µs | ~39 µs | 39 µs | | 6 | pet 2-tile | 2,364 µs | ~80 µs | 81 µs | | 8 | npc 5-tile | 3,708 µs | ~140 µs | 141 µs | | 9 | npc 8-tile | 2,386 µs | ~200 µs | 197 µs | WarmNoFile and LazyWarm rows should be unchanged — they were always cache-warm. The miss tracker only fires when neither resident chunks nor the lazy reader can satisfy the request. ## Future work (not in this PR) - **Background-thread bake**: builds outside the game thread so even promoted chunks don't pay the 700 µs build cost on the main thread. Rule 10 (no Task.Run) applies, so this needs careful design — the bake is a pure data transform but main-thread synchronization on chunk-state transitions has to be threaded through. Defer to a follow-up. - **Long-traverse BDN scenario**: a multi-Find benchmark simulating 50 pet repaths across chunk transitions. Requires restructuring the bench harness; the existing 10-scenario corpus + Cold provider already exercises the gate. - **Swim sourceZ bake**: scenario 5 (sea serpent) shows 56 B alloc on warm paths because the cache's SourceZ is computed under default-walker rules. Swim creatures fall through to slow path. Independent of this PR.
183 lines
7.9 KiB
C#
183 lines
7.9 KiB
C#
using System.Collections.Generic;
|
|
using Server.Engines.Pathing.Cache;
|
|
using Xunit;
|
|
using Xunit.Abstractions;
|
|
|
|
namespace Server.Tests.Pathfinding;
|
|
|
|
/// <summary>
|
|
/// Parity coverage for "walkable static surface above a land tile" terrain — sewers,
|
|
/// dungeon walkways, bridges, raised foundations, and stacked building floors.
|
|
///
|
|
/// The original parity tests only queried at the LAND-anchored standing Z and skipped
|
|
/// multi-Z fallthroughs, so they never noticed that a query at the REAL walk Z — the static
|
|
/// surface a creature actually stands on — returns
|
|
/// <see cref="CacheHitKind.Fallthrough_SourceZMismatch"/>, because the baker anchored
|
|
/// SourceZ at the land average instead of the walkway. In the Britain sewer that's a ~98%
|
|
/// cache miss on a known walk-path (confirmed via [PathDiag).
|
|
///
|
|
/// Method: flood-fill outward from a known-walkable start using
|
|
/// <see cref="Movement.Movement.CheckMovement"/> — the slow path the cache mirrors. Each
|
|
/// reached (x, y, z) is a genuine standing state at its TRUE Z (CheckMovement returns the
|
|
/// destination Z it lands on), exactly the set of states A* would query. For every reached
|
|
/// state the cache must serve a Hit and agree with the slow path. This naturally follows
|
|
/// ramped stairs (each tread at its own Z) and climbs to upper floors, so one start covers
|
|
/// the whole connected structure — no fragile fixed-Z assumption.
|
|
///
|
|
/// A bare test world has no spawned items/mobiles, so CheckMovement reduces to static
|
|
/// walkability (no door/dynamic interference). Parity restricted to cardinal directions:
|
|
/// the cache stores raw masks and applies the diagonal corner-cut at query time, so a raw
|
|
/// diagonal bit legitimately differs from CheckMovement's diagonal result.
|
|
///
|
|
/// EXPECTED: RED before the standable-surface bake (reached states fall through at their
|
|
/// true Z); GREEN after.
|
|
/// </summary>
|
|
[Collection("Sequential Pathfinding Tests")]
|
|
public class StepCacheStaticSurfaceParityTests
|
|
{
|
|
private readonly ITestOutputHelper _output;
|
|
|
|
public StepCacheStaticSurfaceParityTests(ITestOutputHelper output)
|
|
{
|
|
_output = output;
|
|
}
|
|
|
|
[Theory]
|
|
// label, start X, Y, Z (a real in-game walkable tile), max states to explore. Seeds are
|
|
// chosen to span the terrain classes the standable-surface bake must get right; the
|
|
// flood-fill spreads from each across a wide local area, so a handful of seeds exercises
|
|
// thousands of distinct (cell, Z) states without an exhaustive whole-map walk.
|
|
// sewer — static walkway @ z=5 over impassable land; covers dungeon walkways + bridges.
|
|
// inn — stair foot @ z=10; climbs the stairs onto the 1st & 2nd floors (multi-Z).
|
|
// plain — open Britain ground; guards against clearance false-positives on flat land.
|
|
// town — Britain cobblestones near the inn; mixed buildings, stairs, raised floors.
|
|
[InlineData("brit_sewer_walkway", 6034, 1476, 5, 2500)]
|
|
[InlineData("brit_inn_stairs_to_floors", 1495, 1628, 10, 2500)]
|
|
[InlineData("trammel_open_plain", 1500, 1600, 10, 2500)]
|
|
[InlineData("brit_town_cobblestones", 1494, 1626, 10, 2500)] // plain ground: guards against clearance false-positives
|
|
public void CacheServesReachableWalkStates(string label, int sx, int sy, int sz, int maxStates)
|
|
{
|
|
var cache = StepCache.Instance;
|
|
cache.Clear();
|
|
cache.MissPromotionThreshold = 1; // eager build — expect the cache to answer every state
|
|
|
|
var map = Map.Maps[1];
|
|
Assert.NotNull(map);
|
|
|
|
var stub = new ParityStubMobile();
|
|
stub.MoveToWorld(new Point3D(sx, sy, sz), map);
|
|
|
|
// Sanity: the start must itself be a walkable standing state via the slow path.
|
|
var startWalkable = false;
|
|
for (var d = 0; d < 8; d++)
|
|
{
|
|
if (Movement.Movement.CheckMovement(stub, map, new Point3D(sx, sy, sz), (Direction)d, out _))
|
|
{
|
|
startWalkable = true;
|
|
break;
|
|
}
|
|
}
|
|
Assert.True(startWalkable, $"[{label}] start ({sx},{sy},{sz}) is not walkable per the slow path — bad waypoint");
|
|
|
|
var visited = new HashSet<(int x, int y, int z)>();
|
|
var queue = new Queue<(int x, int y, int z)>();
|
|
visited.Add((sx, sy, sz));
|
|
queue.Enqueue((sx, sy, sz));
|
|
|
|
var states = 0;
|
|
var fellThrough = 0;
|
|
var disagreements = 0;
|
|
const int maxLog = 12;
|
|
|
|
while (queue.Count > 0)
|
|
{
|
|
var (x, y, z) = queue.Dequeue();
|
|
states++;
|
|
|
|
var loc = new Point3D(x, y, z);
|
|
var lookup = cache.TryGetMask(map, x, y, (sbyte)z);
|
|
|
|
if (!lookup.IsHit)
|
|
{
|
|
if (fellThrough < maxLog)
|
|
{
|
|
_output.WriteLine($"FELL THROUGH @ ({x},{y},{z}) hitKind={lookup.HitKind}");
|
|
}
|
|
fellThrough++;
|
|
}
|
|
|
|
for (var d = 0; d < 8; d++)
|
|
{
|
|
var dir = (Direction)d;
|
|
var slowOk = Movement.Movement.CheckMovement(stub, map, loc, dir, out var nz);
|
|
|
|
// Expand the frontier through every legal move (incl. diagonals).
|
|
if (slowOk)
|
|
{
|
|
var nx = x;
|
|
var ny = y;
|
|
Movement.Movement.Offset(dir, ref nx, ref ny);
|
|
var next = (nx, ny, (int)nz);
|
|
if (visited.Count < maxStates && visited.Add(next))
|
|
{
|
|
queue.Enqueue(next);
|
|
}
|
|
}
|
|
|
|
// Parity on cardinals only (diagonals carry the query-time corner-cut rule).
|
|
if ((d & 1) == 0 && lookup.IsHit)
|
|
{
|
|
var cacheOk = lookup.IsWalkable(dir);
|
|
if (cacheOk != slowOk)
|
|
{
|
|
if (disagreements < maxLog)
|
|
{
|
|
_output.WriteLine($"WALK DIFF @ ({x},{y},{z}) dir={dir} slow={slowOk} cache={cacheOk}");
|
|
}
|
|
disagreements++;
|
|
}
|
|
else if (slowOk && nz != lookup.GetWalkZ(dir))
|
|
{
|
|
if (disagreements < maxLog)
|
|
{
|
|
_output.WriteLine($"Z DIFF @ ({x},{y},{z}) dir={dir} slow={nz} cache={lookup.GetWalkZ(dir)}");
|
|
}
|
|
disagreements++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
stub.Delete();
|
|
|
|
var fallthroughPct = states == 0 ? 0 : 100.0 * fellThrough / states;
|
|
_output.WriteLine($"[{label}] states={states} fellThrough={fellThrough} ({fallthroughPct:F2}%) disagreements={disagreements}");
|
|
|
|
Assert.True(states > 50, $"[{label}] only explored {states} states — flood-fill stalled, bad waypoint");
|
|
|
|
// Correctness is strict: where the cache DOES answer, it must agree with the slow path.
|
|
Assert.Equal(0, disagreements);
|
|
|
|
// Coverage: nearly every reachable state should be cache-served. A small residual is
|
|
// expected and acceptable — a walkable surface sitting directly under a bridge/stair
|
|
// ramp falls through to the slow path (correct, just uncached) because the bake's
|
|
// clearance check is intentionally conservative there. A real anchor regression shows
|
|
// up as a large fraction (the pre-fix sewer was ~98%), which this still catches.
|
|
Assert.True(
|
|
fallthroughPct < 1.0,
|
|
$"[{label}] cache fell through on {fallthroughPct:F2}% ({fellThrough}/{states}) of reachable states — coverage regression"
|
|
);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Default static walker: inherits straight from Mobile so MovementImpl sees no
|
|
/// BaseCreature flags (CanSwim/CanFly false, bc==null). Mirrors the existing parity stub.
|
|
/// </summary>
|
|
private class ParityStubMobile : Mobile
|
|
{
|
|
public ParityStubMobile()
|
|
{
|
|
Body = 0xC9;
|
|
}
|
|
}
|
|
}
|