From 870f7b28dd6776b2f222c9a4b22a0f1adecee97e Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Thu, 3 Dec 2026 14:58:46 -0500 Subject: [PATCH] perf(abcip): #1032 reuse the constant offset-0 read request per tag runtime; no boxed tuple per cancellable scheduler request --- .../Cip/RequestScheduler.cs | 20 ++++++++++++---- .../Logix/LogixTagClient.cs | 23 ++++++++++++++++++- .../Logix/LogixTagClientTests.cs | 17 ++++++++++++++ 3 files changed, 55 insertions(+), 5 deletions(-) diff --git a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport/Cip/RequestScheduler.cs b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport/Cip/RequestScheduler.cs index 585963850..9ec23d2b9 100644 --- a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport/Cip/RequestScheduler.cs +++ b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport/Cip/RequestScheduler.cs @@ -51,6 +51,13 @@ public sealed class RequestScheduler : IAsyncDisposable /// This request's node in the scheduler queue, or null once dequeued. public LinkedListNode? Node { get; set; } + /// The scheduler this request was queued on; the cancellation callback's state (P-24: passing + /// this object as the state avoids boxing a tuple per cancellable request). + public RequestScheduler? Owner { get; init; } + + /// The caller's token, reported as the cancellation cause. + public CancellationToken Token { get; init; } + /// Stores the waiter's registration; disposes it at once if the waiter already completed. /// The cancellation registration to track. /// The scheduler whose live-registration count is maintained. @@ -171,7 +178,11 @@ public sealed class RequestScheduler : IAsyncDisposable ArgumentNullException.ThrowIfNull(request); if (ct.IsCancellationRequested) return Task.FromCanceled(ct); - var p = new Pending(request, exclusive || expectedReplyBytes < 0, Math.Max(expectedReplyBytes, UnknownReply)); + var p = new Pending(request, exclusive || expectedReplyBytes < 0, Math.Max(expectedReplyBytes, UnknownReply)) + { + Owner = this, + Token = ct, + }; lock (_lock) { ObjectDisposedException.ThrowIf(_disposed, this); @@ -183,14 +194,15 @@ public sealed class RequestScheduler : IAsyncDisposable Interlocked.Increment(ref _liveRegistrations); var registration = ct.Register(static state => { - var (self, pending, token) = ((RequestScheduler, Pending, CancellationToken))state!; + var pending = (Pending)state!; + var self = pending.Owner!; Interlocked.Increment(ref self._cancellationCallbacksRun); lock (self._lock) { if (pending.Node?.List is not null) self._queue.Remove(pending.Node); } - pending.Cancel(token, self); - }, (this, p, ct)); + pending.Cancel(pending.Token, self); + }, p); p.SetRegistration(registration, this); } diff --git a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport/Logix/LogixTagClient.cs b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport/Logix/LogixTagClient.cs index 85987cce0..86f2fc245 100644 --- a/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport/Logix/LogixTagClient.cs +++ b/src/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport/Logix/LogixTagClient.cs @@ -50,7 +50,7 @@ public sealed class LogixTagClient(RequestScheduler scheduler, ICipChannel chann byte[] path, ushort elements, CancellationToken ct, int elementBytes = 0) { ArgumentNullException.ThrowIfNull(path); - var reply = await _scheduler.SendAsync(LogixServices.ReadTagFragmented(path, elements, 0), ct, + var reply = await _scheduler.SendAsync(FirstReadRequest(path, elements), ct, expectedReplyBytes: ExpectedReadReplyBytes(elements, elementBytes, 0)).ConfigureAwait(false); var (type, handle, first) = LogixServices.ParseReadReply(reply); // throws on any status but 0x00/0x06 @@ -96,6 +96,27 @@ public sealed class LogixTagClient(RequestScheduler scheduler, ICipChannel chann } } + /// One cached offset-0 read request: the constant bytes a steady poll resends every cycle (P-24). + private sealed record FirstRead(byte[] Path, ushort Elements, byte[] Request); + + private volatile FirstRead? _firstRead; + + /// + /// The offset-0 Read Tag Fragmented request for . A tag's runtime reads the same encoded + /// path (the same array instance) with the same element count every poll, so the request is built once and + /// reused; a different path instance or count rebuilds it. The request bytes are never mutated after queueing + /// (the scheduler only reads them), so sharing one array across successive reads is safe. + /// + private byte[] FirstReadRequest(byte[] path, ushort elements) + { + var cached = _firstRead; + if (cached is not null && ReferenceEquals(cached.Path, path) && cached.Elements == elements) + return cached.Request; + var request = LogixServices.ReadTagFragmented(path, elements, 0); + _firstRead = new FirstRead(path, elements, request); + return request; + } + /// Appends to a pooled buffer, growing it (rent bigger, copy, return) as needed. private static void Append(ref byte[] buffer, ref int count, ReadOnlySpan chunk) { diff --git a/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport.Tests/Logix/LogixTagClientTests.cs b/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport.Tests/Logix/LogixTagClientTests.cs index d54f270c2..5c5d7c3ed 100644 --- a/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport.Tests/Logix/LogixTagClientTests.cs +++ b/tests/Drivers/ZB.MOM.WW.OtOpcUa.Driver.Ab.Transport.Tests/Logix/LogixTagClientTests.cs @@ -46,6 +46,23 @@ public sealed class LogixTagClientTests ch.Sent[0].ShouldBe(LogixServices.ReadTagFragmented(Path, 1, 0)); // libplctag's wire shape (golden captures) } + [Fact] + public async Task Repeated_reads_reuse_the_offset_zero_request_and_a_changed_count_rebuilds_it() + { + var (ch, client, s) = Create(); + await using var _ = s; + ch.RespondPerService = r => FakeCipChannel.Reply(r[0], 0x00, TypedData(LogixTypeCode.DInt, [1, 0, 0, 0])); + + await client.ReadAsync(Path, 1, Ct); + await client.ReadAsync(Path, 1, Ct); + await client.ReadAsync(Path, 2, Ct); + + ch.Sent.Count.ShouldBe(3); + ch.Sent[0].ShouldBe(LogixServices.ReadTagFragmented(Path, 1, 0)); + ch.Sent[1].ShouldBe(LogixServices.ReadTagFragmented(Path, 1, 0)); + ch.Sent[2].ShouldBe(LogixServices.ReadTagFragmented(Path, 2, 0)); // not the stale cached request + } + [Fact] public async Task Fragmented_read_keeps_the_first_partial_and_continues() {