fix(opcua): #855 perf review 1 — groups evaluate their own filter clone; lazy fields; tighter equivalence tests

- A filter group owns a deep clone of its first filter, validated once and
  evaluated under the group's lock: no item-owned filter is mutated or raced
  (Validate mutates a select clause).
- The projection is computed in the SDK's order: where clause first, fields
  only for an item that will queue.
- Unambiguous locale key; counter for filters given a private group past the
  registry cap; DeliveryCache getter returns its own allocation.
- UseProjectedEventDelivery=false also asks the role and grant checks per
  item, so the equivalence test compares against the pre-optimisation path.
- Equivalence test: same select list under three where clauses, a filter
  modified mid-run, a session with other locales, an ungranted anonymous
  Server-object item, per-rig counter; plus a small overflowing queue with
  DiscardOldest true and false.

Claude-Session: https://claude.ai/code/session_01GT67AD99zQQqpLCVm3p7xk
This commit is contained in:
Joseph Doherty
2026-11-11 13:59:22 -05:00
parent 98d962b24b
commit ca0e73fbca
5 changed files with 317 additions and 120 deletions
@@ -1711,19 +1711,31 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2, INodeManager3
/// </summary>
private void QueueToItem(IEventMonitoredItem item, IFilterTarget e)
{
if (e is ConditionEventSnapshot snapshot) snapshot.DeliveryCache.Queue(Server, item, e, UseProjectedEventDelivery);
else item.QueueEvent(e);
if (e is ConditionEventSnapshot snapshot)
{
if (snapshot.DeliveryCache.Queue(Server, item, e, UseProjectedEventDelivery)) Interlocked.Increment(ref _projectedDeliveries);
}
else
{
item.QueueEvent(e);
}
}
private long _projectedDeliveries;
/// <summary>Items this node manager served through the projected path (test observation: it is really taken).</summary>
internal long ProjectedDeliveries => Interlocked.Read(ref _projectedDeliveries);
/// <summary>
/// Whether condition events are queued through the report's shared projection (#855 perf, default) or through the
/// SDK's own <c>QueueEvent(IFilterTarget)</c> per item. A test switch: the two must deliver identical events.
/// SDK's own <c>QueueEvent(IFilterTarget)</c> per item, with the role and grant checks asked per item (the
/// pre-optimisation path). A test switch: the two must deliver identical events.
/// </summary>
internal bool UseProjectedEventDelivery { get; set; } = true;
/// <summary>The SDK's role-permission verdict for one item, shared per (session, identity) within a report (#855 perf).</summary>
private bool RoleAllows(IEventMonitoredItem item, IFilterTarget e) =>
e is ConditionEventSnapshot snapshot
UseProjectedEventDelivery && e is ConditionEventSnapshot snapshot
? snapshot.DeliveryCache.RoleAllows(item, i => !ServiceResult.IsBad(ValidateEventRolePermissions(i, e)))
: !ServiceResult.IsBad(ValidateEventRolePermissions(item, e));
@@ -1734,7 +1746,7 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2, INodeManager3
private bool IsConditionVisibleTo(
IEventMonitoredItem item, List<NodeId> scopes, Dictionary<object, bool> perIdentity)
{
if (item.EffectiveIdentity is not { } identity) return IsConditionVisibleTo(item, scopes);
if (!UseProjectedEventDelivery || item.EffectiveIdentity is not { } identity) return IsConditionVisibleTo(item, scopes);
if (perIdentity.TryGetValue(identity, out var cached)) return cached;
var visible = IsConditionVisibleTo(item, scopes);
perIdentity[identity] = visible;
@@ -1938,10 +1950,15 @@ public sealed class OtOpcUaNodeManager : CustomNodeManager2, INodeManager3
/// the report's own walk uses it (sequentially, even when part of the walk runs off <c>Lock</c>); dropped when the
/// walk ends.
/// </summary>
public EventDeliveryCache DeliveryCache =>
Volatile.Read(ref _deliveryCache)
?? Interlocked.CompareExchange(ref _deliveryCache, new EventDeliveryCache(), null)
?? _deliveryCache!;
public EventDeliveryCache DeliveryCache
{
get
{
if (Volatile.Read(ref _deliveryCache) is { } existing) return existing;
var created = new EventDeliveryCache();
return Interlocked.CompareExchange(ref _deliveryCache, created, null) ?? created;
}
}
/// <summary>Drops the delivery cache once the walk is over (the snapshot stays referenced from the item queues).</summary>
public void ReleaseDeliveryCache() => Volatile.Write(ref _deliveryCache, null);