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,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();
}
}