perf(grpc-data): #1057 trim the projected publish path's per-value allocation; allocation-free catalog/subject resolve

Measured with GrpcDataWave3PerfTests (10k-item cohort, 100 source versions at 1k/s; numbers in the issue report).
- Core: MappedSubjectResolver.Resolve returns shared immutable resolutions (built once per subject) and compares
  scope/ancestry by index (no MappedSubjectResolution + 2 enumerators per call, several calls per value);
  DataOperationPolicy checks segments by index and builds its local scope key with one exact-length string.Create.
- Origin: LocalExportCatalog.TryResolve compares the view field by field instead of building 'view with { Address }'
  (and LocalAddress no longer copies an exactly-spelled address); the frame Eligible check peeks the latest READY
  version (PartitionedSampleHub.TryPeekReadyVersion) instead of retaining a second sample per value; TickAsync reuses
  worker-owned lists (interest groups, changes via SourceInterestLease.TryReadChanges(into), delivery slice via
  DeliveryScheduler.TakeSlice(into), due items) and the delivery index reuses one ready-list node per source.
- Consumer: PublishPacedAsync is a ValueTask; the cohort and RemoteSubscription walk frame items by index; renewal
  prunes expired witnesses in place.
This commit is contained in:
Joseph Doherty
2026-12-03 15:31:31 -05:00
parent 3d73f4d7d5
commit a6e5ae8aa2
12 changed files with 323 additions and 47 deletions
@@ -0,0 +1,65 @@
using System.Text;
using Shouldly;
using Xunit;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions;
using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections;
using ZB.MOM.WW.OtOpcUa.Core.Authorization;
namespace ZB.MOM.WW.OtOpcUa.Core.Tests.Authorization;
/// <summary>#1057: the mapped-subject resolve and the policy's scope key run per projected value; they must not allocate.</summary>
[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Issue", "1057")]
public sealed class GrpcDataPolicyAllocationTests
{
[Fact]
public void Resolve_is_allocation_free_and_still_answers_every_kind()
{
var fixture = new GrpcDataPolicyTests.Fixture(NodeHierarchyKind.Raw);
var subject = fixture.Subject(DataRealm.Raw, NodeHierarchyKind.Raw, "mapping-a", "Plant/Mount/Speed");
var resolver = fixture.Resolver([subject]);
var moved = subject with { LocalAncestry = ["different-parent", "mapping-a"] };
var local = subject.View with { MappingId = null, MappingRevision = 0 };
resolver.Resolve(subject.View, subject).Kind.ShouldBe(MappedSubjectResolutionKind.Mapped);
resolver.Resolve(subject.View, subject).Subject.ShouldNotBeSameAs(subject, "the authoritative copy, never the live claim");
resolver.Resolve(subject.View, moved).Kind.ShouldBe(MappedSubjectResolutionKind.Deny);
resolver.Resolve(local, null).Kind.ShouldBe(MappedSubjectResolutionKind.Local);
var before = GC.GetAllocatedBytesForCurrentThread();
for (var i = 0; i < 1000; i++)
{
resolver.Resolve(subject.View, subject);
resolver.Resolve(subject.View, moved);
resolver.Resolve(local, null);
}
(GC.GetAllocatedBytesForCurrentThread() - before).ShouldBe(0);
}
[Fact]
public void A_case_mismatched_ancestry_still_matches_and_a_segment_mismatch_still_denies()
{
var fixture = new GrpcDataPolicyTests.Fixture(NodeHierarchyKind.Raw);
var subject = fixture.Subject(DataRealm.Raw, NodeHierarchyKind.Raw, "mapping-a", "Plant/Mount/Speed");
var resolver = fixture.Resolver([subject]);
resolver.Resolve(subject.View, subject with { LocalAncestry = ["LOCAL-PARENT", "MAPPING-A"] }).Kind
.ShouldBe(MappedSubjectResolutionKind.Mapped, "ancestry compares OrdinalIgnoreCase, as before");
resolver.Resolve(subject.View, subject with { Scope = subject.Scope with { Segments = ["Plant", "Mount", "SPEED"] } }).Kind
.ShouldBe(MappedSubjectResolutionKind.Deny, "segments compare Ordinal, as before");
resolver.Resolve(subject.View, subject with { Scope = subject.Scope with { Segments = ["Plant", "Mount"] } }).Kind
.ShouldBe(MappedSubjectResolutionKind.Deny);
}
[Theory]
[InlineData("c1")]
[InlineData("c1", "Plant")]
[InlineData("a-much-longer-cluster-id", "Plant", "0123456789", "a-segment-well-past-ten-characters", "Ünïcødé")]
public void The_scope_key_is_unchanged(string cluster, params string[] segments)
{
foreach (var kind in new[] { NodeHierarchyKind.Raw, NodeHierarchyKind.Uns })
{
var scope = new NodeScope { ClusterId = cluster, Kind = kind, Segments = segments };
var expected = new StringBuilder().Append((int)scope.Kind).Append(':').Append(scope.ClusterId.Length).Append(':').Append(scope.ClusterId);
foreach (var segment in segments) expected.Append(':').Append(segment.Length).Append(':').Append(segment);
DataOperationPolicy.LocalScopeKey(scope).ShouldBe(expected.ToString());
}
}
}