perf(grpc-data): #1105 a queued cohort frame rides the channel as a struct, not a FrameEvent record

Release, 10k cohort: ~1.69 KB -> 1.65-1.67 KB per delivered value process-wide; queue section 40 B -> 0 B per value.

- ProjectedDataAccess's cohort channel carries a CohortMessage struct (control Event, or frame + charge + received
  timestamp); control events convert implicitly, so every writer is unchanged. Slot release, charge and dispose obligations
  on the read, drain and finally paths are unchanged.
- GrpcDataWave3PerfTests asserts queue 0 B/value, build < 192 B/value and (Release) < 1.8 KB/value process-wide.

Declined (documented in docs/DataConnections.md): ProjectedValueUpdate+DataValueSnapshot (public publish API, retained by the
publisher), the one-element publish list (reuse needs an ownership promise IProjectedValuePublisher does not make), origin
frame records (public Core.Abstractions contract; the frame is owned by the reader), CachedSourceRead (public, escapes,
identity-based once-only guards).
This commit is contained in:
Joseph Doherty
2026-12-04 20:25:00 -05:00
parent ce0133b03c
commit b03e01dae1
3 changed files with 54 additions and 9 deletions
@@ -1274,7 +1274,21 @@ public sealed class ProjectedDataAccess : IProjectedDataAccess, IDisposable
private sealed record AddEvent(Demand Demand) : Event;
private sealed record RemoveEvent(Demand Demand) : Event;
private sealed record RefreshEvent(Demand Demand) : Event;
private sealed record FrameEvent(DataSubscriptionFrame Frame, long Bytes, long ReceivedTimestamp) : Event;
/// <summary>
/// #1105: what the cohort's channel carries — a control <see cref="Event"/> or a stream frame. A frame used to be wrapped
/// in a FrameEvent record (40 B per frame, one frame per value under a live publish); a value in the channel's own
/// (reused, circular) segment carries it with no allocation. The frame's slot, charge and dispose obligations are unchanged.
/// </summary>
private readonly struct CohortMessage
{
internal readonly Event? Control;
internal readonly DataSubscriptionFrame? Frame;
internal readonly long Bytes, ReceivedTimestamp;
private CohortMessage(Event control) => Control = control;
internal CohortMessage(DataSubscriptionFrame frame, long bytes, long receivedTimestamp)
{ Frame = frame; Bytes = bytes; ReceivedTimestamp = receivedTimestamp; }
public static implicit operator CohortMessage(Event control) => new(control);
}
private sealed record FreshnessEvent : Event;
private sealed record RenewalEvent : Event;
private sealed record RenewedEvent(Demand Demand, Demand.Binding Expected, bool Success) : Event;
@@ -1300,7 +1314,7 @@ public sealed class ProjectedDataAccess : IProjectedDataAccess, IDisposable
// Value frames keep their backpressure through _frameSlots (and the _frameBytes budget), so a slow consumer still
// stalls the pump rather than growing memory. #1098: a FrameSlotWindow (the semaphore's semantics, without its per-wait
// allocations: a fast origin keeps the window full, so the pump waits for nearly every frame).
private readonly Channel<Event> _events;
private readonly Channel<CohortMessage> _events;
private readonly FrameSlotWindow _frameSlots;
private readonly CancellationTokenSource _stop = new();
private readonly Dictionary<Demand, Registration> _registrations = new();
@@ -1342,7 +1356,7 @@ public sealed class ProjectedDataAccess : IProjectedDataAccess, IDisposable
internal Cohort(ProjectedDataAccess owner, CohortKey key, string connectionId)
{
_owner = owner; _key = key; _connectionId = connectionId;
_events = Channel.CreateUnbounded<Event>(new UnboundedChannelOptions { SingleReader = true });
_events = Channel.CreateUnbounded<CohortMessage>(new UnboundedChannelOptions { SingleReader = true });
var frames = Math.Clamp(owner._limits.MaxDemands * 2, 4, 128);
_frameSlots = new FrameSlotWindow(frames);
_commandTokens = CommandBurst; _commandTokenStamp = owner.Clock.GetTimestamp();
@@ -1387,7 +1401,7 @@ public sealed class ProjectedDataAccess : IProjectedDataAccess, IDisposable
catch { probe?.Exit(ProjectedPublishSection.Pump); Interlocked.Add(ref _frameBytes, -bytes); (frame as IDisposable)?.Dispose(); throw; }
probe = _owner.PublishProbeForTests;
probe?.Enter(ProjectedPublishSection.Queue);
var queued = _events.Writer.TryWrite(new FrameEvent(frame, bytes, receivedTimestamp));
var queued = _events.Writer.TryWrite(new CohortMessage(frame, bytes, receivedTimestamp));
probe?.Exit(ProjectedPublishSection.Queue);
if (!queued)
{ _frameSlots.Release(); Interlocked.Add(ref _frameBytes, -bytes); (frame as IDisposable)?.Dispose(); break; }
@@ -1876,7 +1890,7 @@ public sealed class ProjectedDataAccess : IProjectedDataAccess, IDisposable
// #1098: the probe sees only the synchronous part of each call (no await inside a section).
var probe = _owner.PublishProbeForTests;
probe?.Enter(ProjectedPublishSection.Dispatch);
if (!_events.Reader.TryRead(out var message))
if (!_events.Reader.TryRead(out var entry))
{
sinceFlush = 0;
var flush = FlushAsync(route.Token);
@@ -1885,7 +1899,7 @@ public sealed class ProjectedDataAccess : IProjectedDataAccess, IDisposable
probe?.Enter(ProjectedPublishSection.Dispatch);
var read = _events.Reader.ReadAsync(route.Token);
probe?.Exit(ProjectedPublishSection.Dispatch);
message = await read.ConfigureAwait(false);
entry = await read.ConfigureAwait(false);
}
else if (++sinceFlush >= 64)
{
@@ -1895,6 +1909,7 @@ public sealed class ProjectedDataAccess : IProjectedDataAccess, IDisposable
await flush.ConfigureAwait(false);
}
else probe?.Exit(ProjectedPublishSection.Dispatch);
var message = entry.Control;
if (message is FlushEvent) { Interlocked.Exchange(ref _flushQueued, 0); continue; }
if (message is AddEvent add)
{
@@ -1996,8 +2011,9 @@ public sealed class ProjectedDataAccess : IProjectedDataAccess, IDisposable
if (registration.Handle is not null) removeQueue.Add(registration);
}
else if (message is EndEvent) break;
else if (message is FrameEvent delivered)
else if (entry.Frame is not null)
{
var delivered = entry;
var frame = delivered.Frame;
ProjectedStreamDiagnostics.Count(ProjectedStreamDiagnostics.Event.Frame);
try
@@ -2204,8 +2220,8 @@ public sealed class ProjectedDataAccess : IProjectedDataAccess, IDisposable
route?.Dispose();
while (_events.Reader.TryRead(out var remaining))
{
if (remaining is RenewalEvent) Interlocked.Increment(ref _owner._renewalTicksHandled);
else if (remaining is FrameEvent f) { _frameSlots.Release(); Interlocked.Add(ref _frameBytes, -f.Bytes); (f.Frame as IDisposable)?.Dispose(); }
if (remaining.Control is RenewalEvent) Interlocked.Increment(ref _owner._renewalTicksHandled);
else if (remaining.Frame is { } f) { _frameSlots.Release(); Interlocked.Add(ref _frameBytes, -remaining.Bytes); (f as IDisposable)?.Dispose(); }
}
lock (_owner._sync) _refreshRequests.Clear();
}