Cut 12.2 Soul fix batch 2, S3: measure allocations, correct an overclaiming comment

Measured (Release, 7x9 solid hull -- the LonginusX shipped-hull fixture the
brief named is not available to this test file):

- Lane: 0 B / 1000 calls (was: 64 B/call from Array.Sort's comparer wrapper;
  now confirmed 0 after the earlier IComparable<T> change in this batch).
- Silhouette (caller-supplied buffer): 0 B / 1000 calls.
- HitProbability, gated in (a real, non-gated-out call): ~120 B/call. This is
  NOT from Lane, Silhouette or the comparer -- it comes from Accuracy/
  Resolution's own GetBehavior<T>, which walks Entity.Equipment and each
  item's Behaviors (both ReactiveCollection<T>); that type's enumerator is
  heap-allocated per foreach. This predates Cut 12.2 and is out of this cut's
  scope to fix.
- Inspect, gated in: ~280 B/call (same cause, more GetBehavior<T> calls).

The Forecast comment claiming "the forecast path (HitProbability, Inspect) no
longer allocates on a gated-in call" was not true and is corrected to say what
was actually measured: Silhouette itself is allocation-free given a pooled
buffer, and the residual cost belongs to Entity's own collection choice.
LaneAndSilhouetteAllocateNothingGivenAPooledBuffer asserts the true zero-alloc
claims exactly and records (with a loose regression bound, not a false zero)
the unrelated Entity-level cost on the full HitProbability path.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-09-25 03:30:30 +02:00
commit ca1670a216
2 changed files with 57 additions and 1 deletions

View file

@ -188,9 +188,17 @@ public static class FireControl
// the same thing are two places for the same bug to hide and only be found once.
// Cost fix batch (Self, 2026-09-24): Forecast reads only sil.POnHull and sil.Span, never sil.Intervals, so
// it rents the Silhouette's own scratch array from ArrayPool<Interval>.Shared and returns it before
// returning -- the forecast path (HitProbability, Inspect) no longer allocates on a gated-in call.
// returning -- Silhouette itself (given this pooled buffer) allocates nothing, measured directly.
// Commit's own path (CommitProbability -> Silhouette, uncalled from here) still needs sil.Intervals alive
// for the lateral draw that follows it, so it keeps its own owned array; only the forecast pools.
// S3 fix batch (Hands, 2026-09-25) correction: "the forecast path no longer allocates on a gated-in call"
// overclaimed -- a gated-in HitProbability/Inspect call still allocates roughly 120/280 bytes respectively
// (measured, Release), from Accuracy/Resolution's own GetBehavior<T> walking Entity.Equipment and each
// item's Behaviors (both ReactiveCollection<T>, whose enumerator is heap-allocated per foreach), not from
// anything Silhouette or this pooling touches. That cost predates Cut 12.2 and belongs to Entity's own
// collection choice; it is out of this cut's scope. Cut 10's own guard is what actually matters
// operationally: HitProbability and CommitProbability both bail before Forecast is ever called for the
// common gated-out case (GatedOutHitProbabilityAllocatesNothing pins that at exactly 0).
private static (Silhouette Silhouette, float PSpread) Forecast(Weapon weapon, Entity source, Entity target, HullData targetHull, float precision, float range)
{
var bearing = Bearing(target, TravelDirection(weapon, source, target));

View file

@ -1214,4 +1214,52 @@ public sealed class FireControlCut12Tests : IDisposable
Assert.True(AbsorbsFrom(float2(0, -1))); // stern toward the shot
Assert.True(AbsorbsFrom(float2(1, 0))); // beam
}
// S3 fix batch (Hands, 2026-09-25): Lane's own comment ("never allocates on its own") and Silhouette's
// pooled path are pinned directly, instead of trusting the comment. Measured (Release, LonginusX-shaped
// fixture unavailable to this test file -- a 7x9 solid hull is used instead): Lane and Silhouette (given a
// caller-supplied buffer) allocate exactly 0 bytes over 1000 calls, confirming the S3 change itself
// (Array.Sort(T[], int, int) resolving Interval/LaneCell's own IComparable<T> at JIT time, not the
// (T[], int, int, IComparer<T>) overload that wrapped even a cached singleton comparer in a fresh delegate
// per call). A full gated-in HitProbability call is NOT zero-allocation (measured ~120 B/call, Release) --
// that cost is Accuracy/Resolution's GetBehavior<T> walking Entity's ReactiveCollection<T> fields, whose
// enumerator is heap-allocated per foreach; it predates this cut and is out of its scope (see the corrected
// comment on Forecast). This is recorded as a measurement, not asserted as zero, so the test does not lie
// about what the comment can honestly promise.
[Fact]
public void LaneAndSilhouetteAllocateNothingGivenAPooledBuffer()
{
var e = Build(TestSettings(), SolidShape(7, 9), precision: .5f);
var hull = e.HullData;
var travelDirection = FireControl.TravelDirection(e.Weapon, e.Shooter, e.Target);
var bearing = normalize(e.Target.ToSchematic(travelDirection));
var sil = FireControl.Silhouette(e.Target, hull, null, bearing, .5f);
var laneBuffer = new LaneCell[hull.Shape.Coordinates.Length];
var intervalBuffer = new Interval[hull.Shape.Coordinates.Length];
for (var i = 0; i < 100; i++) FireControl.Lane(hull, bearing, sil.A, laneBuffer); // JIT warm-up
for (var i = 0; i < 100; i++) FireControl.Silhouette(e.Target, hull, null, bearing, .5f, intervalBuffer);
var before = GC.GetAllocatedBytesForCurrentThread();
for (var i = 0; i < 1000; i++) FireControl.Lane(hull, bearing, sil.A, laneBuffer);
var laneAllocated = GC.GetAllocatedBytesForCurrentThread() - before;
Assert.True(laneAllocated == 0, $"1000 Lane calls allocated {laneAllocated} bytes; \"never allocates\" is not true");
before = GC.GetAllocatedBytesForCurrentThread();
for (var i = 0; i < 1000; i++) FireControl.Silhouette(e.Target, hull, null, bearing, .5f, intervalBuffer);
var silhouetteAllocated = GC.GetAllocatedBytesForCurrentThread() - before;
Assert.True(silhouetteAllocated == 0, $"1000 buffered Silhouette calls allocated {silhouetteAllocated} bytes");
// Recorded, not asserted zero: the full gated-in HitProbability call still allocates, from Entity's own
// collection enumeration (GetBehavior<T> over ReactiveCollection<T>), not from anything this fix batch
// touches. A loose upper bound catches a real regression (e.g. a reintroduced comparer wrapper) without
// pretending Entity's own cost is Lane's or Silhouette's to fix here.
for (var i = 0; i < 100; i++) FireControl.HitProbability(e.Weapon, e.Shooter, e.Target);
before = GC.GetAllocatedBytesForCurrentThread();
var sum = 0f;
for (var i = 0; i < 1000; i++) sum += FireControl.HitProbability(e.Weapon, e.Shooter, e.Target);
var hitProbabilityAllocated = GC.GetAllocatedBytesForCurrentThread() - before;
Assert.True(sum > 0f, "fixture: this must be a real, gated-in call, not a gated-out one");
Assert.True(hitProbabilityAllocated < 500_000, $"1000 gated-in HitProbability calls allocated {hitProbabilityAllocated} bytes -- well above Entity's own ~120 B/call baseline; likely a reintroduced Lane/Silhouette allocation");
}
}