Analyze template memory duplication and refactor architecture
Investigate how PlayfieldEngine loads, copies, mutates, and retains NPC character and NPC-equipment template data across playfields containing thousands of live NPCs. Quantify per-NPC duplicated memory where repository data and runtime populations permit, clearly separating measured facts from estimates. Player inventory and MarketEngine memory are secondary and not optimization targets. Propose a lower-memory architecture centered on shared immutable template data and compact per-instance state, including compatibility boundaries, migration stages, risks, and validation. Acceptance criteria: - Current character and item template/data flows are traced to concrete code paths. - Major sources of duplicated stats, strings, collections, and template metadata are identified. - Memory impact is quantified or bounded with explicit assumptions, including an emulation of authored spawn populations for Pleasant Meadows and a deterministic sample of nine other playfields. - A practical target architecture and incremental migration plan are provided. - Recommendations account for serialization, persistence, mutation semantics, and engine boundaries. Verified findings: - A full RDB item/nano preload retains about 432.4 MiB managed per PlayfieldEngine; MarketEngine's endpoint subset retains about 367.5 MiB. Four production PFEs plus MarketEngine therefore duplicate about 2.05 GiB of managed template graphs. - The source dataset has 130,702 item/nano records, 1,602,212 authored stat rows, and 76.4 MiB of raw template payload. - StatPair is 80 bytes; measured retained StatBlock graphs are about 2.4–2.7 KiB for typical 12–14-stat items and 15.9 KiB for a 114-stat NPC baseline. - Persisted items average 13.5 eager runtime stat pairs because Ammo/StackCount loading materializes each full item stat block. - Spawn emulation against authored district data and current hash/template classification produced 2,349 NPCs for Pleasant Meadows and 15,058 NPCs across Pleasant Meadows plus a deterministic sample of nine other populated outdoor playfields. All authored district files resolve to about 84,237 initial NPCs before temporary, mission, pet, or encounter spawns. - A realistic instantiated NPC with shared dummy template stats, NPCSession, and BrainContext retains about 24.2 KiB before DI-created character systems and equipment ItemInstances: about 54 MiB for Pleasant Meadows, 348 MiB for the ten-playfield sample, and a 2.25 GiB lower bound at 100,000 NPCs. - Recommended target: canonical immutable NPC archetypes plus chunked per-playfield NPC state, shared compiled equipment/loadout behavior, compact stat overlays, and a dormant-spawn/active-NPC split. Cross-process sharing, MarketEngine, and player inventory are explicitly not optimization goals; success is measured by each PlayfieldEngine's per-NPC retained bytes and total heap at large authored populations.
Checklist
4 of 4 complete- Trace character and item template loading, construction, mutation, and persistence paths
- Quantify or bound duplicated template, stat, string, and collection memory
- Design a shared immutable-template and compact per-instance-state architecture
- Define incremental migration, compatibility risks, and validation approach
- Area
- Project Rubi-Ka
- Project
- engines
- Kind
- Research
- Priority
- Medium
- Task ID
tsk_hfx46ukw4a- Updated