Repository navigation
Conversation
- AFrame becomes a struct. Its mutators move to AFrameExtensions as `this ref` extension methods, so a call on a copy does not compile; callers that change a frame pass it by ref. - Pool transitions per thread (at most 4), released by the update and line-of-sight callers; Reset drops every object and cell reference. - Copy check, backup and current positions in place (Position.CopyFrom), falling back to a new Position where the target may be aliased. - add_all_outside_cells sets CellArray.AddedOutside, so find_cell_list adds the outside cells once. - CellArray.GetCellAt replaces Cells.Values.ElementAt. - Reuse a scratch Position in UpdateObjectInternal, return early from report_collision_end on an empty table, copy only LocalSpacePos in CylSphere.IntersectsSphere, test spheres by center and radius in find_transit_cells, and skip GRotate's multiply for a zero rotation. 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 allocates about 450 MB a second (451 MB/s, the mean of the two base runs in the A/B below). An allocation trace of master on the same load (2026-11-27) attributes 82% of it to
PhysicsObj.update_object. By type,AFrameis 28%,Sphere17% andPosition12%. Most of it is short-lived scaffolding built again for every object update:AFrameis a class, so every frame combined, copied or offset during an update is a heap object, and so is everyPosition's frame. Animation (Sequence) and the managers that adjust an offset (PositionManager,StickyManager,ConstraintManager) create more.Transitionand itsObjectInfo,CollisionInfo,CellArrayandSpherePath, with their lists.MakeTransitionisnew Transition(), andCleanupTransitionis empty.SpherePathcopies positions withnew Position(...)each time it saves, restores or sets the check position, andValidateTransitiondoes the same for the current position.find_cell_listrunsadd_all_outside_cellsagain for every land cell already in the array.add_all_outside_cellsnever setsCellArray.AddedOutside, so the guard that should skip the repeats never fires. Each repeat callsget_landcellagain, along with the landblock lookup and lock behind it, 1.7-4.6 times more often than needed.Cells.Values.ElementAt(i)boxes an enumerator on every call (Transition.CheckOtherCells,ObjCell.find_cell_list,PhysicsObj.find_bbox_cell_list).Dictionary.ValueCollectionis not anIList.UpdateObjectInternalallocates a newPositionevery update.report_collision_endallocates aListon every successful move, even when the collision table is empty.CylSphere.IntersectsSpherebuilds the full local-space sphere cache for every cylinder it tests, but only reads its position.EnvCell.find_transit_cellsbuilds a newSpherefor each portal and each sphere.GRotateevaluatesCreateFromYawPitchRolleven when the rotation is zero.The fix
AFramebecomes a struct. Its methods that change the frame (Combinewith a scale,InterpolateOrigin,InterpolateRotation,GRotate,Rotate,Subtract,set_heading,set_position,set_rotate,set_vector_heading,rotate_around_axis_to_vector) move intoAFrameExtensionsasthis refextension methods. A call on a copy, such as a property or a method's return value, then fails to compile instead of silently changing the copy. The methods that only read the frame stay on the struct. Code that passed a frame in to be changed now passes it byref:Sequence.apply_physics,PositionManager.AdjustOffset,StickyManager.adjust_offset,ConstraintManager.adjust_offsetandChessMatch.CalculateWeeniePosition. Theframe != nullchecks inSequencego, since a struct cannot be null.AFrame frame = nullbecomes= defaultin the two places that had it.MakeTransitiontakes a transition from a per-thread stack ([ThreadStatic], at most 4 kept per thread), andTransition.Releasereturns it. The callers on the update and visibility paths call it after their last read of the transition:SetPosition,check_collision,transitionwhen the move fails, the three object-update paths that take the transitiontransitionreturns, and the four line-of-sight checks inWorldObject. Three callers that run rarely are left as they are and their transitions are collected as before:TryDropItem,/fumbleand/nudge. Transitions can nest on one thread, because collision handlers run game code, so the pool is a stack.Resetreturns every field of the transition and its parts to the state ofnew Transition()and drops every reference to objects and cells, so a pooled transition keeps nothing alive.CurPos,CheckPosandBackupCheckPoscan be the same object, soResetgives them newPositions rather than reusing them.Position.CopyFromcopies the cell ID and frame into an existing position.SpherePathandTransitionuse it for the check position, its backup and the current position. Where the target can be the same object as another live position (ValidatePlacementsetsCurPos = CheckPos), aReferenceEqualscheck falls back tonew Position(...), as before.add_all_outside_cellssetsCellArray.AddedOutside = true, sofind_cell_listadds the outside cells once.CellArray.GetCellAt(index)walks the values with the dictionary's struct enumerator and replaces the threeElementAtcalls.UpdateObjectInternalreuses one scratchPositionperPhysicsObj.report_collision_endreturns at once when the collision table is empty.CylSphere.IntersectsSpherecopies the position intoLocalSpacePos, the one field it reads, instead of building the whole cache.CellStruct,BSPTreeandBSPNodegainsphere_intersects_cell(center, radius)overloads, whichfind_transit_cellsuses instead of building aSphere.GRotateskips the multiply when the rotation is zero. Multiplying by the exact identity is exact, so the result is the same bit for bit.Behaviour
An independent read-only review compared master and this branch path by path and found no difference reachable from a live path. Two differences exist:
set_framewith an invalid orientation but a valid origin: master also wrote the zero quaternion into the caller's frame object (an object'sRequestPos, for example), and a later normalise turned it into NaN. Now only the object's own position gets the zero quaternion, whichset_framesets in both versions.InterpolationManagerstays by value. Passing its frame byrefwould have letadjust_offset'sframe = offsetreplace the caller's offset frame. The only difference left is in code with no callers (InterpolateTo, reached only fromMoveOrTeleport): master's heading and scaling of the offset also rewrote the queued node's target frame, and now they change a copy.AFrame.Equalscomparesframe.Orientationwith itself, so any two orientations compare equal. That is on master too, and it moves unchanged here.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, (the memory PR), this PR, this PR, (the memory 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 two rows areWorldObject_Tick_UpdateObjectPhysicsandLandblock_Tick_Monster_Tick, which add up the time on every landblock thread, so they can exceed the world update.Earlier measurement (2026-11-27, before this A/B)
The same changes were measured earlier in three steps, on the same load in ABBA order with 30 minute windows (the mean of two runs each):
AddedOutside,GetCellAt, position copiesAFramestructEach step beat the run-to-run noise (about 1%) in both halves of the ABBA. Those builds differ from this branch only by cleanups (the
AFrameExtensionssplit among them) and by passingInterpolationManager's frame byref.Testing
ACE.Server.Tests: 16 passed, 4 failed, the same as master. The 4 failures come from the test paths toConfig.js.exampleandstarterGear.json.PhysicsObj.update_objecton single objects over real terrain and setups. Over 1,000,000 updates (35,607 cell changes, about 50,000 updates in contact), compared on cell, position, orientation, state and shadow cells after every update, there were no mismatches, and a crowd placement of 1,500 creatures came out the same. A negative control, the unguarded in-placeSetCheckPos, gave 161,621 mismatches in 200,000 updates, so the comparison can fail.NullReferenceExceptionfrom the consoleserverstatuscommand, which the harness runs every 10 minutes. It came atworldObject.MissileinAdminStatCommands.HandleServerStatus, so most likely from a null in the list thatGetAllWorldObjectsForDiagnosticscopies off a landblock's object dictionary. That race is on master: the command copies the dictionary on the console thread while the landblock tick adds and removes objects. This PR doesn't touch that code.Files
Source/ACE.Server/Physics/Animation/AFrame.csSource/ACE.Server/Physics/Animation/Sequence.csSource/ACE.Server/Physics/Transition.csSource/ACE.Server/Physics/SpherePath.csSource/ACE.Server/Physics/PhysicsObj.csSource/ACE.Server/Physics/CylSphere.csSource/ACE.Server/Physics/BSP/BSPNode.csSource/ACE.Server/Physics/BSP/BSPTree.csSource/ACE.Server/Physics/Common/CellArray.csSource/ACE.Server/Physics/Common/CellStruct.csSource/ACE.Server/Physics/Common/EnvCell.csSource/ACE.Server/Physics/Common/LandCell.csSource/ACE.Server/Physics/Common/ObjCell.csSource/ACE.Server/Physics/Common/Position.csSource/ACE.Server/Physics/Managers/ConstraintManager.csSource/ACE.Server/Physics/Managers/PositionManager.csSource/ACE.Server/Physics/Managers/StickyManager.csSource/ACE.Server/Physics/Particles/ParticleEmitter.csSource/ACE.Server/Entity/Chess/ChessMatch.csSource/ACE.Server/WorldObjects/WorldObject.csSource/ACE.Server/WorldObjects/WorldObject_Magic.cs🤖 Generated with Claude Code