An adversarial performance review found our own instruments cannot measure the project's own performance gates: - FrameProfiler aggregated CPU/GPU/alloc/stage samples into ~5-second windows and reset the ring buffers after each report, so route-wide p50/p95/p99 distributions across a whole soak could not be reconstructed after the fact. ACDREAM_FRAME_HISTORY=<path> now opts into a separate per-frame history (one record per frame, ~72 bytes/record, accumulated in memory with zero frame-thread I/O) that a shutdown-only Dispose() writes as CSV. The aggregated [frame-prof] report format and its existing metrics are unchanged. - The canonical checkpoint JSON tracked cache residency (entry/byte counts) but never LOH size/fragmentation, process-wide allocated bytes, or cache hit/miss/eviction traffic — a committed audit JSON showed 65% LOH fragmentation that no tracked instrument recorded, and "does a revisit portal hit or miss the caches" was unanswerable from an artifact alone. WorldLifecycleResourceSnapshot now carries loh_size_bytes/loh_fragmentation_bytes (GCMemoryInfo.GenerationInfo index 3), process_total_allocated_bytes (GC.GetTotalAllocatedBytes), and Interlocked hit/miss/eviction counters for the CPU mesh cache, decoded-texture cache, and the four bounded DAT-object caches (portal/cell/highRes/language, aggregated). - run-connected-r6-soak.ps1 unconditionally forced ACDREAM_UNCAPPED_RENDER=1 with no capped mode, while its sibling lifecycle-gate script correctly gated it behind a switch. Added -Uncapped (default capped, matching the sibling script's pattern), fixed the stationary dwell (12s -> 26s, past the 25s LiveEntityLivenessController deadline the adjacent comment already cited), and now write an env-disclosure.json into the automation artifact directory before every launch listing every ACDREAM_* var the script sets plus -Uncapped, since the prior audit could only see ACDREAM_DUMP_MOVE_TRUTH and nothing else was ever recorded anywhere. Cache counters are wired via the existing composition path (ObjectMeshManager already owns the CPU mesh cache and the mesh extractor directly; content.Dats is threaded into WorldLifecycleResourceSnapshotSource the same way every other composition consumer receives it). The DAT-object cache lives behind IDatReaderWriter, a third-party interface from the DatReaderWriter package that cannot be extended; RuntimeDatCollection (the one production implementation) exposes the aggregate stats directly and a pattern match reads them, degrading to zero for any test double — no new static registry was introduced (GpuMemoryTracker remains the one precedented process-wide static). Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> (cherry picked from commit 1da2c33c875b41fa383dd79694ee2765f0e21896)
161 lines
5.4 KiB
C#
161 lines
5.4 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Concurrent;
|
|
using System.Threading;
|
|
|
|
namespace AcDream.Content;
|
|
|
|
/// <summary>
|
|
/// Thread-safe, byte-bounded residency for canonical decoded texture pixels.
|
|
/// Mesh extraction may run on several workers, so cached arrays are immutable
|
|
/// after admission and callers clone them before applying surface-local alpha.
|
|
/// </summary>
|
|
internal sealed class DecodedTextureCache {
|
|
private sealed record Entry(DecodedTextureKey Key, byte[] Pixels);
|
|
|
|
private readonly object _gate = new();
|
|
private readonly Dictionary<DecodedTextureKey, LinkedListNode<Entry>> _entries = new();
|
|
private readonly LinkedList<Entry> _lru = new();
|
|
private readonly ConcurrentDictionary<DecodedTextureKey, Lazy<byte[]>> _inflight = new();
|
|
private readonly long _maximumBytes;
|
|
private readonly int _maximumEntries;
|
|
private long _residentBytes;
|
|
|
|
// Hit/miss/eviction traffic counters (2026-07-24 measurement-tooling
|
|
// review). Read via Stats without the _gate lock, so plain fields with
|
|
// Interlocked increments rather than lock-protected longs.
|
|
private long _hits;
|
|
private long _misses;
|
|
private long _evictions;
|
|
|
|
/// <summary>Cumulative hit/miss/eviction counts since construction.</summary>
|
|
public CacheStats Stats => new(
|
|
Interlocked.Read(ref _hits),
|
|
Interlocked.Read(ref _misses),
|
|
Interlocked.Read(ref _evictions));
|
|
|
|
public DecodedTextureCache(long maximumBytes, int maximumEntries) {
|
|
ArgumentOutOfRangeException.ThrowIfNegative(maximumBytes);
|
|
ArgumentOutOfRangeException.ThrowIfNegative(maximumEntries);
|
|
_maximumBytes = maximumBytes;
|
|
_maximumEntries = maximumEntries;
|
|
}
|
|
|
|
public int Count {
|
|
get {
|
|
lock (_gate) {
|
|
return _entries.Count;
|
|
}
|
|
}
|
|
}
|
|
|
|
public long ResidentBytes {
|
|
get {
|
|
lock (_gate) {
|
|
return _residentBytes;
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool TryGet(DecodedTextureKey key, out byte[] pixels) {
|
|
lock (_gate) {
|
|
if (!_entries.TryGetValue(key, out var node)) {
|
|
Interlocked.Increment(ref _misses);
|
|
pixels = null!;
|
|
return false;
|
|
}
|
|
|
|
Interlocked.Increment(ref _hits);
|
|
Touch(node);
|
|
pixels = node.Value.Pixels;
|
|
return true;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Returns the canonical pixels for <paramref name="key"/>. If another
|
|
/// worker admitted the same key first, its array wins. <paramref name="isCached"/>
|
|
/// reports whether the returned array is retained and therefore immutable.
|
|
/// </summary>
|
|
public byte[] RetainOrUse(DecodedTextureKey key, byte[] pixels, out bool isCached) {
|
|
ArgumentNullException.ThrowIfNull(pixels);
|
|
|
|
lock (_gate) {
|
|
if (_entries.TryGetValue(key, out var existing)) {
|
|
Touch(existing);
|
|
isCached = true;
|
|
return existing.Value.Pixels;
|
|
}
|
|
|
|
if (_maximumEntries == 0 || pixels.LongLength > _maximumBytes) {
|
|
isCached = false;
|
|
return pixels;
|
|
}
|
|
|
|
while (_lru.First is { } oldest
|
|
&& (_entries.Count >= _maximumEntries
|
|
|| _residentBytes + pixels.LongLength > _maximumBytes)) {
|
|
Remove(oldest);
|
|
}
|
|
|
|
var node = _lru.AddLast(new Entry(key, pixels));
|
|
_entries.Add(key, node);
|
|
_residentBytes += pixels.LongLength;
|
|
isCached = true;
|
|
return pixels;
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Decode a missing key once across concurrent mesh workers. The expensive
|
|
/// factory runs outside the LRU lock and unrelated keys proceed in parallel.
|
|
/// A failed or oversized decode is not stranded as permanent in-flight state.
|
|
/// </summary>
|
|
public byte[] GetOrCreate(
|
|
DecodedTextureKey key,
|
|
Func<byte[]> factory,
|
|
out bool isCached) {
|
|
ArgumentNullException.ThrowIfNull(factory);
|
|
if (TryGet(key, out byte[] cached)) {
|
|
isCached = true;
|
|
return cached;
|
|
}
|
|
|
|
var candidate = new Lazy<byte[]>(
|
|
factory,
|
|
LazyThreadSafetyMode.ExecutionAndPublication);
|
|
Lazy<byte[]> shared = _inflight.GetOrAdd(key, candidate);
|
|
try {
|
|
byte[] decoded = shared.Value;
|
|
return RetainOrUse(key, decoded, out isCached);
|
|
}
|
|
finally {
|
|
_inflight.TryRemove(
|
|
new KeyValuePair<DecodedTextureKey, Lazy<byte[]>>(key, shared));
|
|
}
|
|
}
|
|
|
|
private void Touch(LinkedListNode<Entry> node) {
|
|
if (node != _lru.Last) {
|
|
_lru.Remove(node);
|
|
_lru.AddLast(node);
|
|
}
|
|
}
|
|
|
|
private void Remove(LinkedListNode<Entry> node) {
|
|
_lru.Remove(node);
|
|
_entries.Remove(node.Value.Key);
|
|
_residentBytes -= node.Value.Pixels.LongLength;
|
|
Interlocked.Increment(ref _evictions);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Every input that can change decoded RGBA belongs in the cache identity.
|
|
/// INDEX16/P8 clip-map conversion and A8 additive conversion are surface
|
|
/// dependent even when they reference the same RenderSurface DID.
|
|
/// </summary>
|
|
internal readonly record struct DecodedTextureKey(
|
|
uint RenderSurfaceId,
|
|
bool IsClipMap,
|
|
bool IsAdditive);
|