perf(abcip): #1032 reuse the constant offset-0 read request per tag runtime; no boxed tuple per cancellable scheduler request
This commit is contained in:
@@ -51,6 +51,13 @@ public sealed class RequestScheduler : IAsyncDisposable
|
||||
/// <summary>This request's node in the scheduler queue, or null once dequeued.</summary>
|
||||
public LinkedListNode<Pending>? Node { get; set; }
|
||||
|
||||
/// <summary>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).</summary>
|
||||
public RequestScheduler? Owner { get; init; }
|
||||
|
||||
/// <summary>The caller's token, reported as the cancellation cause.</summary>
|
||||
public CancellationToken Token { get; init; }
|
||||
|
||||
/// <summary>Stores the waiter's registration; disposes it at once if the waiter already completed.</summary>
|
||||
/// <param name="registration">The cancellation registration to track.</param>
|
||||
/// <param name="owner">The scheduler whose live-registration count is maintained.</param>
|
||||
@@ -171,7 +178,11 @@ public sealed class RequestScheduler : IAsyncDisposable
|
||||
ArgumentNullException.ThrowIfNull(request);
|
||||
if (ct.IsCancellationRequested) return Task.FromCanceled<CipReply>(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);
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>One cached offset-0 read request: the constant bytes a steady poll resends every cycle (P-24).</summary>
|
||||
private sealed record FirstRead(byte[] Path, ushort Elements, byte[] Request);
|
||||
|
||||
private volatile FirstRead? _firstRead;
|
||||
|
||||
/// <summary>
|
||||
/// The offset-0 Read Tag Fragmented request for <paramref name="path"/>. 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.
|
||||
/// </summary>
|
||||
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;
|
||||
}
|
||||
|
||||
/// <summary>Appends <paramref name="chunk"/> to a pooled buffer, growing it (rent bigger, copy, return) as needed.</summary>
|
||||
private static void Append(ref byte[] buffer, ref int count, ReadOnlySpan<byte> chunk)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user