Skip to content

Share EnvCell geometry and shrink per-object collections - #4560

Open
Mag-nus wants to merge 1 commit into
ACEmulator:masterfrom
Mag-nus:memory-cellstruct-cache
Open

Mag-nus wants to merge 1 commit into
ACEmulator:masterfrom
Mag-nus:memory-cellstruct-cache

Conversation

@Mag-nus

@Mag-nus Mag-nus commented Nov 28, 2026 •

Copy link
Copy Markdown
Member

The problem

On a load of 25 dungeons (429 landblocks, about 12,000 creatures, no players), master keeps about 6 GB of live objects: a median of 5.94 and 5.98 GB after each gen2 collection in the two base runs of the A/B below. A heap dump of master on the same load (a diagnostic run on 2026-11-27, dumped at 20 and 60 minutes) shows where most of it goes:

What holds it MB
EnvCell geometry, one CellStruct copy per EnvCell ~2,700
EnvCell.VisibleCells dictionaries 391
EnchantmentManagerWithCaching and its 12 dictionaries 181
GUID recycle queue (dump at 20 min / 60 min) 57 / 154
  • Every EnvCell builds its own copy of its cell geometry. EnvCell's constructor runs new CellStruct(cellStruct) (EnvCell.cs:72) for the dat cell structure it uses, and the polygon, vertex and BSP caches are all disabled. The dump holds 134,117 physics CellStructs built from 958 dat cell structures, 5.6 million Polygons from 173,333 and 21.8 million Vertex objects from 122,109. That is about 140 copies of each structure. Nothing writes to a CellStruct after it is built, and the only other assignment to EnvCell.CellStructure is the empty one in EnvCell.Init.
  • Each EnvCell keeps a Dictionary<uint, EnvCell> of the cells it can see. There are 133,099 of them holding 10.3 million entries, about 77 per cell. The keys are already in VisibleCellIDs.
  • The GUID recycle queue holds one Tuple<DateTime, uint> object per destroyed dynamic object, for 6 hours. This load destroys about 1,050 objects a second (corpses, projectiles, loot), and the queue grew from 1.24 to 3.77 million entries between the two dumps. At 6 hours it would hold about 23 million entries, 0.9 GB of tuples plus the queue's array, all in gen2.
  • Every EnchantmentManagerWithCaching creates its 12 cache dictionaries up front. Every WorldObject has one, mostly items, corpses and projectiles that never carry an enchantment. The dump holds 1.77 million of these dictionaries (142 MB), and about 150,000 of them hold an entry.

The fix

  • CellStructCache (new, Physics/Entity/CellStructCache.cs): a ConcurrentDictionary keyed by (EnvironmentID, CellStructureID), following GfxObjCache. EnvCell asks it for the physics CellStruct instead of building one, so all EnvCells that use the same dat structure share one copy. If two landblock threads build the same structure at once, GetOrAdd keeps one and the other is dropped. /clearphysicscaches clears it, and the physics cache line of /serverstatus reports its count.
  • VisibleCellMap (new, Physics/Common/VisibleCellMap.cs) replaces Dictionary<uint, EnvCell> for EnvCell.VisibleCells. It is two arrays, the cell IDs and the cells, in the order they were added, and looks up an ID with Array.IndexOf. It has the members the callers use (Values, ContainsKey, TryGetValue, Add, Remove, Count), so no caller changes. Add throws ArgumentException on a repeated ID, as Dictionary.Add does. A dictionary that is only ever added to enumerates in insertion order, so the readers that iterate Values (EnvCell, ObjectMaint, BuildingObj, ObjCell) see the cells in the same order as before. Remove is only reached from code that is commented out.
  • GUID recycle queue: Queue<Tuple<DateTime, uint>> becomes Queue<(DateTime, uint)>. Each entry is 16 bytes in the queue's array instead of a 32-byte object and a reference. The order and timing are unchanged.
  • Enchantment caches: the 12 dictionaries in EnchantmentManagerWithCaching are created on their first write. Reads treat a missing dictionary as empty, and ClearCache uses ?.Clear(). The thread-safety is unchanged: these were plain Dictionarys before.

Measured

A/B on the dungeon load above, on one 8-core VM (16 GB, .NET 10, Server GC with DATAS) on the night of 2026-11-28. Each run had a 15 minute warm-up and a 45 minute measured window. The runs went in ABBA order so that drift over the night cancels: base, this PR, (the physics PR twice), this PR, base. Base is master (65aed57) plus the load-test harness, and the PR build is the same tree plus this diff. The harness touches none of this code. Nothing else ran on the VM during the six windows.

"Pair 1" compares the first PR run with the first base run, and "pair 2" the second with the second. The other columns are the means of two runs each.

Base This PR Change Pair 1 Pair 2
CPU (cores, of 8) 1.621 1.487 -8.2% -9.4% -7.1%
World update, mean (ms) 6.12 5.63 -8.0% -9.0% -6.9%
WorldObject physics (ms) 3.02 2.61 -13.5% -16.5% -10.4%
Landblock actions (ms) 3.05 2.62 -13.8% -15.9% -11.8%
Monster tick (ms) 15.37 14.21 -7.5% -7.0% -8.0%
Live heap after each gen2, median (GB) 5.96 2.71 -54.5% -54.4% -54.7%
Peak RSS in the window (GB) 11.39 5.39 -52.7% -52.9% -52.5%
Allocation (MB/s) 451 445 -1.3% -1.8% -0.8%
Collections in 45 min 2,290 2,269 -0.9% -1.5% -0.4%
Mean pause (ms) 25.9 24.4 -5.6% -5.5% -5.6%
Total pause in 45 min (s) 59.2 55.4 -6.4% -6.9% -6.0%
  • The times are the server's performance monitor means. The world update is UpdateGameWorld_Entire. The other three rows are WorldObject_Tick_UpdateObjectPhysics, Landblock_Tick_RunActions and Landblock_Tick_Monster_Tick, which add up the time on every landblock thread, so they can exceed the world update.
  • Live heap is the heap in use (size less fragmentation) at the first counter sample after each gen2 collection in the window. After the background gen2s, base left 5.86-5.99 GB and this PR 2.63-2.80 GB.
  • Both pairs agree on every row. Allocation, the number of collections and the GC heap count (8 in all four runs) barely change, so the CPU saving is not the GC doing less work. The largest falls are in WorldObject physics, which walks the cell geometry, and in the landblock actions.
  • Worst cases. The worst world update was 97 and 137 ms on base, and 488 and 80 ms with this PR. The 488 ms was a single update in the first PR run. That run's worst 5 seconds of GC pause was 172 ms, so a collection did not cause it, and I have not found what did. The worst 5 seconds of GC pause was 181 and 219 ms on base, and 172 and 175 ms with this PR.
  • Swap. The base runs swapped 16 and 38 MB out during their windows, and the PR runs nothing.

Earlier measurement (2026-11-28, before this A/B)

Earlier runs measured the same changes on top of the physics PR's changes, on the same load: two 45 minute runs of each build in ABCDDCBA order, so each build has one run in each half of the night. Cores are the mean of the two runs.

Cores Live heap after each gen2 Peak RSS
physics changes 1.452 5.68-5.82 GB 10.86-10.99 GB
+ CellStructCache 1.391 (-4.2%) 2.97-3.10 GB 6.74-6.86 GB
+ the other three 1.355 (-6.7%) 2.48-2.57 GB 5.22-5.23 GB, flat after 30 min

Both halves of the night agreed: -4.5% and -3.9% for the cache, -6.6% and -6.7% with the other three. Allocation (165-168 MB/s) and the number of collections did not change.

Testing

  • ACE.Server.Tests/Physics/VisibleCellMapTests.cs (new) checks VisibleCellMap against Dictionary<uint, EnvCell> with seeded random operations, comparing cells by reference:
    • add-only: 1,000 trials; the same Values in the same order, and the same ContainsKey, TryGetValue and duplicate-key results;
    • add and remove: 1,000 trials; the same set of cells and the same lookups;
    • a repeated ID throws ArgumentException and leaves the count unchanged.
  • ACE.Server.Tests: 19 passed, 4 failed. Master gives 16 passed and the same 4 failures, which come from the test paths to Config.js.example and starterGear.json.
  • In the A/B, the PR runs logged no kind of error or warning that the base runs did not, with one exception. 33 seconds after the first PR run started, before its measured window, five objects logged a [PERFORMANCE][PHYSICS] warning, all at the same instant (131 ms in UpdateObjectPhysics). The second PR run logged none.

Files

  • Source/ACE.Server/Physics/Entity/CellStructCache.cs (new)
  • Source/ACE.Server/Physics/Common/VisibleCellMap.cs (new)
  • Source/ACE.Server/Physics/Common/EnvCell.cs
  • Source/ACE.Server/Managers/GuidManager.cs
  • Source/ACE.Server/WorldObjects/Managers/EnchantmentManagerWithCaching.cs
  • Source/ACE.Server/Command/Handlers/AdminStatCommands.cs
  • Source/ACE.Server/Command/Handlers/DeveloperCommands.cs
  • Source/ACE.Server.Tests/Physics/VisibleCellMapTests.cs (new)

🤖 Generated with Claude Code

- Cache physics CellStructs by (EnvironmentID, CellStructureID), so all
  EnvCells built from the same dat cell structure share one copy
  (CellStructCache). Counted in /serverstatus, cleared by
  /clearphysicscaches.
- Replace EnvCell.VisibleCells' Dictionary<uint, EnvCell> with
  VisibleCellMap: cell IDs and cells in two arrays, in the order added.
- Queue recycled GUIDs as (DateTime, uint) value tuples instead of
  Tuple objects.
- Create EnchantmentManagerWithCaching's 12 cache dictionaries on their
  first write.
- Add VisibleCellMapTests: seeded random equivalence with Dictionary.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant