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:
+65
@@ -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());
|
||||
}
|
||||
}
|
||||
}
|
||||
+80
@@ -0,0 +1,80 @@
|
||||
using Shouldly;
|
||||
using Xunit;
|
||||
using ZB.MOM.WW.OtOpcUa.Runtime.DataConnections;
|
||||
|
||||
namespace ZB.MOM.WW.OtOpcUa.Runtime.Tests.DataConnections;
|
||||
|
||||
/// <summary>
|
||||
/// #1057: the per-value / per-tick pieces of the origin publish path that used to allocate. Each runs once warm, then a loop is
|
||||
/// measured on this thread. (End-to-end numbers: <c>GrpcDataWave3PerfTests</c>, opt-in.)
|
||||
/// </summary>
|
||||
[Trait("Category", "Unit"), Trait("Feature", "GrpcData"), Trait("Issue", "1057")]
|
||||
public sealed class GrpcData1057AllocationTests
|
||||
{
|
||||
private static long Measure(Action action, int iterations = 1000)
|
||||
{
|
||||
action();
|
||||
var before = GC.GetAllocatedBytesForCurrentThread();
|
||||
for (var i = 0; i < iterations; i++) action();
|
||||
return GC.GetAllocatedBytesForCurrentThread() - before;
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_delivery_index_slice_reuses_the_callers_list_and_its_ready_nodes()
|
||||
{
|
||||
var scheduler = new DeliveryScheduler(16, 16);
|
||||
var source = HubTest.Ticket("driver", "a").Key;
|
||||
scheduler.TryRegister(source, 1).ShouldBeTrue();
|
||||
scheduler.TryRegister(source, 2).ShouldBeTrue();
|
||||
var slice = new List<ScheduledInterest>(8);
|
||||
long generation = 0;
|
||||
var allocated = Measure(() =>
|
||||
{
|
||||
scheduler.TryMarkDirty(source, ++generation).ShouldBeTrue();
|
||||
slice.Clear(); scheduler.TakeSlice(8, slice);
|
||||
slice.Clear(); scheduler.TakeSlice(8, slice);
|
||||
});
|
||||
allocated.ShouldBe(0);
|
||||
// Positive control: the slices still visit every interest of a dirty source.
|
||||
scheduler.TryMarkDirty(source, ++generation);
|
||||
var seen = new List<ScheduledInterest>();
|
||||
for (var i = 0; i < 4; i++) scheduler.TakeSlice(8, seen);
|
||||
seen.Select(x => x.InterestId).OrderBy(x => x).ShouldBe([1L, 2L]);
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void An_interest_lease_reads_into_the_callers_list_without_allocating()
|
||||
{
|
||||
using var hub = HubTest.Create(partitions: 1, bytes: 1_000_000, ingress: 100, dirty: 100);
|
||||
var mine = HubTest.Ticket("driver", "mine");
|
||||
HubTest.BindAndPublish(hub, mine, 1, 1);
|
||||
hub.ProcessPending();
|
||||
hub.TryCaptureInterestSnapshot([mine], HubTest.Budget, CancellationToken.None, out var snapshot, out var interest).ShouldBeTrue();
|
||||
using (snapshot!) using (interest!)
|
||||
{
|
||||
var changes = new List<SourceInterestChange>(8);
|
||||
Measure(() => { changes.Clear(); interest!.TryReadChanges(256, 4096, changes, out _).ShouldBeTrue(); }).ShouldBe(0);
|
||||
HubTest.Publish(hub, mine, 2, 2); hub.ProcessPending();
|
||||
changes.Clear();
|
||||
interest!.TryReadChanges(256, 4096, changes, out _).ShouldBeTrue();
|
||||
changes.ShouldHaveSingleItem().Ticket.ShouldBe(mine);
|
||||
}
|
||||
}
|
||||
|
||||
[Fact]
|
||||
public void The_catalog_resolve_and_the_latest_version_peek_do_not_allocate()
|
||||
{
|
||||
using var fixture = new LocalDataFixture();
|
||||
fixture.Publish(10);
|
||||
fixture.Catalog.TryResolve(fixture.Raw, out var node).ShouldBeTrue();
|
||||
node.Metadata.View.ShouldBe(fixture.Raw);
|
||||
Measure(() => fixture.Catalog.TryResolve(fixture.Raw, out _)).ShouldBe(0);
|
||||
fixture.Hub.TryPeekReadyVersion(fixture.Ticket, out var version).ShouldBeTrue();
|
||||
version.ShouldBe(1);
|
||||
Measure(() => fixture.Hub.TryPeekReadyVersion(fixture.Ticket, out _)).ShouldBe(0);
|
||||
// The resolve still refuses a view whose other fields disagree with the node's.
|
||||
fixture.Catalog.TryResolve(fixture.Raw with { Subject = "Plant/Other" }, out _).ShouldBeFalse();
|
||||
// ...and still normalises a differently-cased origin cluster id.
|
||||
fixture.Catalog.TryResolve(fixture.Raw with { Address = fixture.Raw.Address with { OriginClusterId = "CLUSTER" } }, out _).ShouldBeTrue();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user