The cadence work ended with a question the log could not answer: the compositor
manages 6.8 composites/s at ~141ms while the 10:03 take did 29-34ms on the "same
scene shape" (full-render split=0 elements=6 dynamic=5). Nothing in the stats line
said WHICH of the six elements cost what, and nothing recorded the scene itself, so
the two takes could only be compared by eye. This closes that gap and nothing else:
no fix is attempted until a real take names the culprit.
- Services/Compositor/CompositeTimings.cs (new): per-window collector.
Add(stage, ticks) accumulates; Describe(renders, minMs = 0.1) prints the averaged
breakdown slowest-first, dropping sub-0.1ms noise.
- SceneCompositor.Render / CompositeLayers / BakeStaticBase / BlitBakedElement /
BlitCachedLayer + SceneGraph.GetBakedBase: trailing optional CompositeTimings?
Every stage is timed (backdrop, base, flash, social, ticker, zoom, scale, overlay,
bake backdrop, bake overlays) and EVERY element gets its own entry keyed by
z-index + name ("#2 Webcam"), with the nested "raster" sub-stage recording the
paste-cache rasterize miss - the split between "re-samples every tick" and "slow
paste" without a second session. Bake-pass elements are prefixed "bake #..." so a
one-off rebake cannot masquerade as a per-composite cost. Callers that pass
nothing (thumbnails, existing tests) are unaffected and pay nothing.
- FramePump: owns the collector, threads it through RenderScene/RenderFull/
GetBakedBase/CompositeLayers, and logs "FramePump parts (N renders/5s): ..." once
per 5s window beside the stats line (averaged per render, so it compares directly
with "avg render"), then resets it. Also dumps the scene - "FramePump scene dump:"
header plus one line per element (z-index, name, kind, source type, visibility,
rect, opacity, clip) - at pump start and on every scene change.
- One Good Dog integration test (ytLive.Tests/CompositeTimingTests.cs): a real
composite of a full-canvas element plus a 64x64 one through the real collector
must rank the full-canvas element first, carry the raster sub-stage and the fixed
stages, and the dump must name both elements with z-index/type/rect and echo the
split point.
Derivative work (AGENTS.md): OBS's answer to "which source is slow" is the Source
Profiler - average execution time of each source's tick and render functions
(https://docs.obsproject.com/reference-libobs-util-source-profiler, util/profiler.h).
Same shape here: one timer per element per composite, reported as an average, plus
the stage split this case needs.
Docs in the same commit: ai.md (new subsection "Per-element composite timing + scene
dump", the closed gap corrected in "Frame cadence"), Services/index.md (new file row
+ FramePump/SceneCompositor rows), TASKS.md (unit marked shipped, next = creator
take), HANDOFF.md (rewritten: unpushed commit, next step, verify.sh whitelist hole).
Also recorded this session (from the creator): the launch command is
`dotnet run -c Release` - Release is the DEFAULT, Debug only when explicitly named,
and perf takes must read Release. ai.md -> Run updated.
Full suite 428 tests, 0 warnings; the only failures are the known audio env flakes
(both pass in isolation). Scope check passed.
77 lines
3.6 KiB
C#
77 lines
3.6 KiB
C#
using System.Collections.Generic;
|
|
using System.Diagnostics;
|
|
using System.Linq;
|
|
|
|
namespace ytLive.Services.Compositor;
|
|
|
|
/// <summary>
|
|
/// Per-stage composite timing (webcam-cadence unit, 2026-10-16): wall time per render
|
|
/// pass, split into named stages — the backdrop, every element (keyed by z-index +
|
|
/// name), the global overlays, and the final output scale. The pump owns one instance
|
|
/// for the whole session, passes it into <see cref="SceneCompositor.Render"/> /
|
|
/// <c>CompositeLayers</c> / <c>BakeStaticBase</c>, and logs an averaged breakdown every
|
|
/// stats window — the "which of the 6 elements eats ~140ms" reading the old stats line
|
|
/// could not give (it reported render/acquire/resolve, never the culprit layer).
|
|
/// <para>Stage names are stable and greppable: <c>backdrop</c>, <c>base</c>,
|
|
/// <c>flash</c>, <c>social</c>, <c>ticker</c>, <c>zoom</c>, <c>scale</c>,
|
|
/// <c>bake backdrop</c>, <c>bake overlays</c>, and <c>#<index> <name></c> per
|
|
/// element (bake-pass elements are prefixed <c>bake</c> so a one-off rebake cannot
|
|
/// masquerade as a per-composite cost). A <c>#<i> <name> raster</c> sub-stage NESTS inside its parent's total
|
|
/// (it is the paste-cache rasterize miss — the prime suspect when one element costs a
|
|
/// full resample every tick). Stage time INCLUDES that stage's frame resolve; resolve
|
|
/// is separately totalled in the stats line.</para>
|
|
/// <para>Single writer in production (the pump's render thread — every blit joins its
|
|
/// <see cref="StripPool"/> workers before returning); the lock only makes a test able
|
|
/// to share one across threads.</para>
|
|
/// </summary>
|
|
public sealed class CompositeTimings
|
|
{
|
|
private readonly object _gate = new();
|
|
private readonly Dictionary<string, long> _stages = new(StringComparer.Ordinal);
|
|
|
|
/// <summary>Accumulate one stage sample. Non-positive ticks are dropped — a
|
|
/// skipped layer must not create a zero-cost entry that dilutes the reading.</summary>
|
|
public void Add(string stage, long ticks)
|
|
{
|
|
if (ticks <= 0 || stage.Length == 0) return;
|
|
lock (_gate)
|
|
{
|
|
_stages.TryGetValue(stage, out var cur);
|
|
_stages[stage] = cur + ticks;
|
|
}
|
|
}
|
|
|
|
/// <summary>Clear every stage — called once per stats window after the parts line
|
|
/// is logged, so each window reports its own breakdown.</summary>
|
|
public void Reset()
|
|
{
|
|
lock (_gate) _stages.Clear();
|
|
}
|
|
|
|
/// <summary>Average milliseconds per render for every stage at or above
|
|
/// <paramref name="minMs"/>, sorted slowest-first:
|
|
/// <c>"#2 Webcam 88.1ms, backdrop 61.2ms, #2 Webcam raster 84.0ms"</c>.
|
|
/// <paramref name="renders"/> is the divisor (the window's render count, so the
|
|
/// numbers compare directly with the stats line's <c>avg render</c>). The floor
|
|
/// keeps a healthy pipeline from burying the contender in zeroes; the empty string
|
|
/// means nothing was composited (the fully-static shortcut and C4 cache hits do no
|
|
/// per-element work — an empty parts line there is the correct reading).</summary>
|
|
public string Describe(int renders, double minMs = 0.1)
|
|
{
|
|
List<KeyValuePair<string, long>> snap;
|
|
lock (_gate)
|
|
{
|
|
if (_stages.Count == 0) return string.Empty;
|
|
snap = _stages.ToList();
|
|
}
|
|
|
|
var freq = (double)Stopwatch.Frequency;
|
|
var parts = snap
|
|
.Select(kv => (Stage: kv.Key, Ms: kv.Value / freq * 1000.0 / System.Math.Max(1, renders)))
|
|
.Where(p => p.Ms >= minMs)
|
|
.OrderByDescending(p => p.Ms)
|
|
.Select(p => $"{p.Stage} {p.Ms:F1}ms");
|
|
return string.Join(", ", parts);
|
|
}
|
|
}
|