Repository navigation
Conversation
- 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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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:
CellStructcopy per EnvCellEnvCell.VisibleCellsdictionariesEnchantmentManagerWithCachingand its 12 dictionariesEnvCell's constructor runsnew 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 physicsCellStructs built from 958 dat cell structures, 5.6 millionPolygons from 173,333 and 21.8 millionVertexobjects from 122,109. That is about 140 copies of each structure. Nothing writes to aCellStructafter it is built, and the only other assignment toEnvCell.CellStructureis the empty one inEnvCell.Init.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 inVisibleCellIDs.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.EnchantmentManagerWithCachingcreates 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): aConcurrentDictionarykeyed by(EnvironmentID, CellStructureID), followingGfxObjCache.EnvCellasks it for the physicsCellStructinstead 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,GetOrAddkeeps one and the other is dropped./clearphysicscachesclears it, and the physics cache line of/serverstatusreports its count.VisibleCellMap(new,Physics/Common/VisibleCellMap.cs) replacesDictionary<uint, EnvCell>forEnvCell.VisibleCells. It is two arrays, the cell IDs and the cells, in the order they were added, and looks up an ID withArray.IndexOf. It has the members the callers use (Values,ContainsKey,TryGetValue,Add,Remove,Count), so no caller changes.AddthrowsArgumentExceptionon a repeated ID, asDictionary.Adddoes. A dictionary that is only ever added to enumerates in insertion order, so the readers that iterateValues(EnvCell,ObjectMaint,BuildingObj,ObjCell) see the cells in the same order as before.Removeis only reached from code that is commented out.Queue<Tuple<DateTime, uint>>becomesQueue<(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.EnchantmentManagerWithCachingare created on their first write. Reads treat a missing dictionary as empty, andClearCacheuses?.Clear(). The thread-safety is unchanged: these were plainDictionarys 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.
UpdateGameWorld_Entire. The other three rows areWorldObject_Tick_UpdateObjectPhysics,Landblock_Tick_RunActionsandLandblock_Tick_Monster_Tick, which add up the time on every landblock thread, so they can exceed the world update.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.
CellStructCacheBoth 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) checksVisibleCellMapagainstDictionary<uint, EnvCell>with seeded random operations, comparing cells by reference:Valuesin the same order, and the sameContainsKey,TryGetValueand duplicate-key results;ArgumentExceptionand 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 toConfig.js.exampleandstarterGear.json.[PERFORMANCE][PHYSICS]warning, all at the same instant (131 ms inUpdateObjectPhysics). 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.csSource/ACE.Server/Managers/GuidManager.csSource/ACE.Server/WorldObjects/Managers/EnchantmentManagerWithCaching.csSource/ACE.Server/Command/Handlers/AdminStatCommands.csSource/ACE.Server/Command/Handlers/DeveloperCommands.csSource/ACE.Server.Tests/Physics/VisibleCellMapTests.cs(new)🤖 Generated with Claude Code