perf(worker): completion waits tick at the 50 ms fallback and the bulk wait probes only pending handles (MXA-009, #256)

This commit is contained in:
Joseph Doherty
2026-12-03 09:52:52 -05:00
parent ad30daf6e2
commit a17f24c817
3 changed files with 83 additions and 11 deletions
@@ -264,6 +264,66 @@ public sealed class MxAccessValueCacheTests
Assert.True(pumpCalls >= 3, $"Expected repeated pumping across the 400 ms wait, saw {pumpCalls} calls.");
}
/// <summary>
/// MXA-009: with the default fallback tick a wait that nothing wakes pumps about once per 50 ms
/// (not once per 5 ms), so a 200 ms timeout costs a handful of pump steps.
/// </summary>
[Fact]
public void TryWaitForUpdate_WithDefaultInterval_PumpsAtTheFiftyMillisecondFallbackTick()
{
MxAccessValueCache cache = new();
int pumpCalls = 0;
bool found = cache.TryWaitForUpdate(
serverHandle: 7,
itemHandle: 21,
sinceVersion: 0,
deadlineUtc: DateTime.UtcNow.AddMilliseconds(200),
pumpStep: () => Interlocked.Increment(ref pumpCalls),
out _);
Assert.False(found);
Assert.InRange(pumpCalls, 2, 12);
}
/// <summary>
/// MXA-009: the bulk wait keeps only still-pending handles in its working set, and reports each
/// handle that updates (including ones that arrive in different wakes) exactly once.
/// </summary>
[Fact]
public void TryWaitForUpdates_ReportsEachHandleAsItArrivesAcrossWakes()
{
MxAccessValueCache cache = new();
int[] handles = { 21, 22, 23 };
ulong[] baselines = { 0UL, 0UL, 0UL };
bool[] received = new bool[3];
MxAccessValueCache.CachedValue[] values = new MxAccessValueCache.CachedValue[3];
int pumpCalls = 0;
int count = cache.TryWaitForUpdates(
serverHandle: 7,
handles,
baselines,
deadlineUtc: DateTime.UtcNow.AddMilliseconds(300),
pumpStep: () =>
{
pumpCalls++;
if (pumpCalls == 2)
{
cache.Set(7, 22, BuildEvent(7, 22, 5, 192, Timestamp.FromDateTime(DateTime.UtcNow)));
}
else if (pumpCalls == 3)
{
cache.Set(7, 21, BuildEvent(7, 21, 6, 192, Timestamp.FromDateTime(DateTime.UtcNow)));
}
},
received,
values);
Assert.Equal(2, count);
Assert.Equal(new[] { true, true, false }, received);
}
private static MxEvent BuildEvent(
int serverHandle,
int itemHandle,
@@ -225,7 +225,7 @@ public sealed class MxAccessValueCache
/// <param name="deadlineUtc">Absolute UTC deadline.</param>
/// <param name="pumpStep">Action that pumps any pending Windows messages.</param>
/// <param name="value">The cached value if the update was received before the deadline.</param>
/// <param name="pollIntervalMs">Upper bound on one idle wait slice — the cadence at which <paramref name="pumpStep"/> runs when nothing wakes the wait. Default 5 ms, itself capped at <see cref="StaWaitHelper.MaxFallbackTickMilliseconds"/>.</param>
/// <param name="pollIntervalMs">Upper bound on one idle wait slice — the cadence at which <paramref name="pumpStep"/> runs when nothing wakes the wait. Default 50 ms (the fallback ceiling): a message or the update signal already wakes the wait immediately, so the tick is only the safety net; capped at <see cref="StaWaitHelper.MaxFallbackTickMilliseconds"/>.</param>
/// <returns><see langword="true"/> if an update newer than <paramref name="sinceVersion"/> arrived before the deadline; otherwise <see langword="false"/>.</returns>
public bool TryWaitForUpdate(
int serverHandle,
@@ -234,7 +234,7 @@ public sealed class MxAccessValueCache
DateTime deadlineUtc,
Action pumpStep,
out CachedValue value,
int pollIntervalMs = 5)
int pollIntervalMs = StaWaitHelper.MaxFallbackTickMilliseconds)
{
if (pumpStep is null)
{
@@ -304,7 +304,7 @@ public sealed class MxAccessValueCache
Action pumpStep,
bool[] received,
CachedValue[] values,
int pollIntervalMs = 5)
int pollIntervalMs = StaWaitHelper.MaxFallbackTickMilliseconds)
{
if (itemHandles is null)
{
@@ -337,6 +337,11 @@ public sealed class MxAccessValueCache
throw new ArgumentException("Handle, baseline, and output arrays must have matching lengths.");
}
// Indexes still waiting for an update. Each wake probes only these and drops the ones that
// arrived, so the per-wake cost shrinks as handles complete instead of rescanning all of
// them (a lock and a dictionary lookup each) for the whole timeout.
int[] pending = new int[count];
int pendingCount = 0;
int receivedCount = 0;
for (int index = 0; index < count; index++)
{
@@ -344,27 +349,34 @@ public sealed class MxAccessValueCache
{
receivedCount++;
}
else
{
pending[pendingCount++] = index;
}
}
while (true)
{
pumpStep();
for (int index = 0; index < count; index++)
int stillPending = 0;
for (int slot = 0; slot < pendingCount; slot++)
{
if (received[index])
{
continue;
}
int index = pending[slot];
if (TryGet(serverHandle, itemHandles[index], out CachedValue value) && value.Version > sinceVersions[index])
{
received[index] = true;
values[index] = value;
receivedCount++;
}
else
{
pending[stillPending++] = index;
}
}
pendingCount = stillPending;
if (receivedCount >= count)
{
return receivedCount;
@@ -133,7 +133,7 @@ public sealed class MxAccessWriteCompletionCache
/// <param name="deadlineUtc">Absolute UTC deadline.</param>
/// <param name="pumpStep">Action that pumps any pending Windows messages.</param>
/// <param name="statuses">The recorded status rows if a completion arrived before the deadline; empty otherwise.</param>
/// <param name="pollIntervalMs">Upper bound on one idle wait slice — the cadence at which <paramref name="pumpStep"/> runs when nothing wakes the wait. Default 5 ms, itself capped at <see cref="StaWaitHelper.MaxFallbackTickMilliseconds"/>.</param>
/// <param name="pollIntervalMs">Upper bound on one idle wait slice — the cadence at which <paramref name="pumpStep"/> runs when nothing wakes the wait. Default 50 ms (the fallback ceiling): a message or the completion signal already wakes the wait immediately, so the tick is only the safety net; capped at <see cref="StaWaitHelper.MaxFallbackTickMilliseconds"/>.</param>
/// <returns><see langword="true"/> if a completion newer than <paramref name="sinceVersion"/> arrived before the deadline; otherwise <see langword="false"/>.</returns>
public bool TryWaitForCompletion(
int serverHandle,
@@ -142,7 +142,7 @@ public sealed class MxAccessWriteCompletionCache
DateTime deadlineUtc,
Action pumpStep,
out RepeatedField<MxStatusProxy> statuses,
int pollIntervalMs = 5)
int pollIntervalMs = StaWaitHelper.MaxFallbackTickMilliseconds)
{
if (pumpStep is null)
{