Skip to content

Latest commit

 

History

History
177 lines (148 loc) · 11.6 KB

memory-reduction-audit.md

File metadata and controls

177 lines (148 loc) · 11.6 KB

Memory reduction audit

Baseline: b6eee61 (SDK 318). This audit covers current main's v1 packed-world loader, streaming adapter, catalog and rendering helpers. It does not assume unmerged progressive-runtime branches are present. The target has 24 MiB MEM1; managed objects, native assets and framebuffers all compete for that physical memory. ARAM is a separate DMA-backed tier, not directly GX-addressable memory.

Implemented reductions

The deterministic 48-resolution validation pack has 649 records and a largest supported streamed record of 73,984 bytes. The new accounting tool checks pack CRCs/ranges and reports its source SHA256; engine load-time admission still validates descriptor semantics. The checked-in JSON is payload accounting, not a physical-console trace.

Allocation Before After Reduction
Native streaming source buffer 131,072 B 73,984 B 57,088 B
Catalog ID indexes/links, retained managed payload 5,724 B 2,862 B 2,862 B
Scheduler ID-to-slot lookup, retained managed payload 2,600 B 1,300 B 1,300 B
ARAM metadata while unused, managed payload on 32-bit target 5,200 B 0 B 5,200 B
ARAM metadata once active, including pending transfer references 5,200 B 7,800 B -2,600 B
Selected idle/fence/read-state arrays, replacing store-task references 32 B 904 B -872 B
Optional cold-write native buffer, allocated lazily 0 B 16,384 B -16,384 B
Temporary catalog mesh-tail payload 384 B 192 B 192 B

The read-source reduction remains 57,088 bytes. Once the optional cold-write buffer is allocated, the two native buffers together save 40,704 bytes over the baseline source buffer. Additional native savings depend on actually retiring unused resident geometry and texture pairs; retaining a cold copy alone does not free their MEM1 allocations. See cold residency.

After the new idle/fence bookkeeping and pending-transfer references, the selected retained arrays save 8,490 B of element payload while the cold cache is unused, or 690 B once it is active. The catalog/scheduler index arrays themselves still save 4,162 B. These figures exclude object field changes, object/array headers and allocator rounding; they are not a whole managed-heap net total. The SDK's reserved managed heap is unchanged: lower live occupancy reduces pressure but does not automatically return that reservation to the native allocator. No heap-size default or texture quality is reduced.

Bounded native staging

StreamingBufferPolicy reserves the aligned maximum of a configured coalescing ceiling and every supported streamed record. The default ceiling is 65,536 bytes; existing validation targets of 32/64 KiB fit. A single larger supported record still fits even when the requested coalescing target is smaller. Unsupported records above 131,040 bytes retain their existing demand-time rejection, so an unused unsupported fine level does not newly reject an otherwise usable pack.

ReadBatchBytes remains a soft target and is capped by MaximumReadBatchBytes. For the former maximum coalescing range, explicitly construct:

new WorldStreamer(world, path, budgetBytes, maximumReadBatchBytes: 131040);

The original three-parameter constructor signature is retained for compiled clients. Capacity is reserved before streaming begins; Pump never resizes or disposes the source buffer. This avoids introducing a GPU wait through the SDK's conservative native-buffer disposal. Source holds, cancellation drain, transfer padding, CRC and texture publication are unchanged. StagingBytes reports this separate allocation; PhysicalBytes retains its existing dynamic-residency meaning. Scenario teardown checks now use the actual staging size.

Compact IDs and lazy ARAM metadata

Catalog record IDs are bounded by 4,096, so private indexes/links use ushort. Scheduler capacity is bounded by 256; its lookup stores slot+1 in ushort, with zero as the missing sentinel. Public return types, generation checks, record ordering and the cached AssetRecord[] representation are unchanged. Maximum-ID and slot-255 tests exercise relocation, stale mappings, cancellation and drain.

Combined retained catalog/scheduler payload savings are 4*recordCount+1566 bytes: 4,162 B at 649 records and 17,950 B at 4,096. The catalog constructor also uses smaller temporary mesh-tail IDs without retaining or mutating its caller's metadata buffer.

An unused AramCache no longer reserves per-record entry/age arrays. Metadata is created only after the first native cold allocation succeeds; empty probes, over-budget stores and failed native allocation do not allocate it. Once active, the cache uses the same bounds and LRU/protected-ID behavior. On the 32-bit target this avoids the baseline's 8*(recordCount+1) payload bytes while unused, up to 32,776 B at 4,096 records. The active cache now also tracks pending store tasks, charging 12*(recordCount+1) bytes instead of the baseline's eight bytes per entry. This one-time first-use allocation may occur during streaming; normal stores already allocate transfer/ARAM ownership. Callers still drain transfers before disposal. Zero-ID entries are now evicted and freed correctly.

Bootstrap and failed construction

The synchronous bootstrap file is scoped to world construction. Its handle and SDK/stdio resources are no longer retained for the world's lifetime; the independent asynchronous AssetStream remains owned by the world. Failed world construction also releases already-created coarse meshes, textures and asset stream ownership. This prevents malformed input or allocation failure from leaving native resources behind. Cleanup is on the bootstrap/error path, not the steady-state promotion path. SDK-free allocation-failure/corruption tests verify partial ownership is released.

DolphinDotNet audit

The original SDK allocation reductions were delivered in DolphinDotNet PR #171. The later cold-residency work uses the shared broker from DolphinDotNet PR #173. The source pin in .sdk-revision and the published dependency in Directory.Build.props must identify the same reviewed SDK revision. The original compact indexes and bounded source buffer remain compatible with SDK 318 APIs; prioritized DMA is provided by the newer broker.

The SDK renderer previously reserved two 4,096-entry model submission tables, although it admits at most 4,096 submissions in total. A shared table preserves opaque insertion order, transparent depth sorting and stable ties while removing 65,536 B of native static storage. This saving applies when the executable links the model submission path. A raw-mesh-only Hypercube executable may discard that section, so this is not added to the world staging savings above.

SDK transforms now reserve a local matrix only when first parented, and a child list only when a child is first attached. A never-parented leaf keeps its existing world-matrix cache and computes TRS directly into it. MeshBatch's 128 root poses therefore avoid 8,192 B of local-matrix payload plus child-list objects, empty backing arrays and headers. Hierarchy caches are reserved during attachment and retained for reuse; matrix updates and draws remain allocation-free after setup. These are managed occupancy savings, not a change to the reserved heap size. The SDK's host CLR probe measures 47,128 B before versus 28,696 B after for 128 root transforms, including the array that holds them: an 18,432 B reduction.

Native skinning workspace now holds the largest validated skin in a model instead of always reserving room for 256 joints. A two-joint instance uses 288 B rather than 32,800 B including the native allocation header and alignment. A cached stride avoids scanning skins per update; its extra 4 B per PPC instance leaves a net 32,508 B saving in that example. A 256-joint model keeps its former workspace size and pays the 4 B stride field. Budget checks, inverse-transpose normal matrices, unload/recreation and cleanup remain covered by native tests.

Other opportunities and why they are deferred

Opportunity Potential Required follow-up
Dense permanent-patch metadata The 649-record fixture reserves 7,800 B of 32-bit array payload for 89 permanent patches; dense storage would use about 1,068 B plus a mapping Patches, TriangleCounts and VertexCounts are public arrays indexed by record ID. Introduce/version an accessor API before changing that contract.
Compact or streamed catalog metadata Parsed records alone occupy 56 B per record; input metadata adds another 64 B per record during construction Reconstructing records in Get adds work to a hot path; a streaming parser needs bounded-read/CRC and v2-family tests. Keep cached records for this pass. The 4,096-record format limit is not a promise that a full world fits the default 256 KiB managed heap.
Further flatten MeshBatch transforms After SDK transform compaction, 128 root poses still retain 13,312 B of float-array payload plus headers A flat-matrix/native batching path needs transform, ordering, allocation and throughput parity. Keep reusable preallocation.
Drop decoded collision source copies Up to 24 extra 44-byte native payloads, plus allocator overhead, duplicate decoded heights/masks Residency and cell-readiness currently use their ownership during eviction. Change the accounting and lifecycle together, with movement/hole tests.
Shrink decode scratch A valid maximum mesh needs 13,068 B versus the 16,384 B scratch reservation Small gain relative to source staging; preserve public scratch/decode behavior until a separate bounded workspace API is agreed.
Reduce texture-pool copies or idle retention Double-buffered textures and reusable unbound allocations dominate some working sets Active GPU readers and async staging require their storage. Use measured residency/quality policy or the independent progressive-level runtime; do not free a front/back allocation or reduce quality implicitly.
Lower heap/framebuffer/ARAM reservations Potentially larger physical MEM1 savings than live-object compaction Requires representative console high-water, video mode, stacks, audio and pressure measurements. No physical-console performance or reserve-sizing claim is made here.

Reproduction and acceptance

python3 tools/build_assets.py --resolution 48
python3 tools/memory_audit.py out/assets/world.hcp --output out/memory-audit/world.json
python3 tools/measure_core_allocations.py --revision b6eee61
python3 tools/measure_core_allocations.py
python3 tools/test_host.py
python3 tests/progressive_fixtures.py
dotnet run --project tests/TextureFamilies.Tests -c Release

The CLR probe exports baseline or working-tree Core sources to an isolated project, warms constructors, excludes input-buffer creation and counts allocated bytes for catalog and a capacity-256 scheduler (the streaming adapter uses 128). It is a host measurement, not a GameCube heap measurement. On this executor, totals were:

Record count Baseline allocated Updated allocated Reduction
649 55,008 B 50,664 B 4,344 B
4,096 275,624 B 257,488 B 18,136 B

Validation includes maximum record/slot boundaries, coalescing/alignment and unsupported levels, unchanged catalog ownership, ARAM LRU/protection/copy failure, empty-cache allocation bounds, constructor cleanup, both ordered-rendering paths and full console-scene CI. Console checks must continue to cover stale/canceled work, pixel consistency, audio, movement readiness and zero-resource teardown.