From a17f24c817e2ce1f3fb836b0a8ff4a801358e709 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Thu, 3 Dec 2026 09:52:52 -0500 Subject: [PATCH] perf(worker): completion waits tick at the 50 ms fallback and the bulk wait probes only pending handles (MXA-009, #256) --- .../MxAccess/MxAccessValueCacheTests.cs | 60 +++++++++++++++++++ .../MxAccess/MxAccessValueCache.cs | 30 +++++++--- .../MxAccess/MxAccessWriteCompletionCache.cs | 4 +- 3 files changed, 83 insertions(+), 11 deletions(-) diff --git a/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/MxAccessValueCacheTests.cs b/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/MxAccessValueCacheTests.cs index 2af99724..3e883a0b 100644 --- a/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/MxAccessValueCacheTests.cs +++ b/src/ZB.MOM.WW.MxGateway.Worker.Tests/MxAccess/MxAccessValueCacheTests.cs @@ -264,6 +264,66 @@ public sealed class MxAccessValueCacheTests Assert.True(pumpCalls >= 3, $"Expected repeated pumping across the 400 ms wait, saw {pumpCalls} calls."); } + /// + /// 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. + /// + [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); + } + + /// + /// 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. + /// + [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, diff --git a/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/MxAccessValueCache.cs b/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/MxAccessValueCache.cs index fac45456..049abad6 100644 --- a/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/MxAccessValueCache.cs +++ b/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/MxAccessValueCache.cs @@ -225,7 +225,7 @@ public sealed class MxAccessValueCache /// Absolute UTC deadline. /// Action that pumps any pending Windows messages. /// The cached value if the update was received before the deadline. - /// Upper bound on one idle wait slice — the cadence at which runs when nothing wakes the wait. Default 5 ms, itself capped at . + /// Upper bound on one idle wait slice — the cadence at which 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 . /// if an update newer than arrived before the deadline; otherwise . 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; diff --git a/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/MxAccessWriteCompletionCache.cs b/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/MxAccessWriteCompletionCache.cs index 54ed9559..e137fef2 100644 --- a/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/MxAccessWriteCompletionCache.cs +++ b/src/ZB.MOM.WW.MxGateway.Worker/MxAccess/MxAccessWriteCompletionCache.cs @@ -133,7 +133,7 @@ public sealed class MxAccessWriteCompletionCache /// Absolute UTC deadline. /// Action that pumps any pending Windows messages. /// The recorded status rows if a completion arrived before the deadline; empty otherwise. - /// Upper bound on one idle wait slice — the cadence at which runs when nothing wakes the wait. Default 5 ms, itself capped at . + /// Upper bound on one idle wait slice — the cadence at which 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 . /// if a completion newer than arrived before the deadline; otherwise . public bool TryWaitForCompletion( int serverHandle, @@ -142,7 +142,7 @@ public sealed class MxAccessWriteCompletionCache DateTime deadlineUtc, Action pumpStep, out RepeatedField statuses, - int pollIntervalMs = 5) + int pollIntervalMs = StaWaitHelper.MaxFallbackTickMilliseconds) { if (pumpStep is null) {