Skip to content

Fix attribute node switches writing into the shared passive tree - #2606

Merged
Wires77 merged 1 commit into
PathOfBuildingCommunity:devfrom
filipecavalc:fix-attribute-node-shared-tree
Dec 2, 2026
Merged

Wires77 merged 1 commit into
PathOfBuildingCommunity:devfrom
filipecavalc:fix-attribute-node-shared-tree

Conversation

@filipecavalc

Copy link
Copy Markdown
Contributor

Description of the problem being solved:

PassiveSpecClass:SwitchAttributeNode (src/Classes/PassiveSpec.lua) builds the attribute override node with a deep copy of the tree node, metatables kept:

local newNode = copyTableSafe(self.tree.nodes[nodeId], false, true)

self.tree.nodes[nodeId] belongs to the passive tree, which main:LoadTree caches and every spec and build shares. The node holds a ModList, a class instance. The class system gives each instance parent proxies (object.ModStore) whose metatable is the proxy itself, with __newindex = object. copyTableSafe sets the original metatable on the copy first and then fills it in with out[k] = v, so for these proxies every field it copies goes through __newindex into the original instance: the shared tree node's modList gains raw __call, __index, __newindex, _className, _object and _parent fields that point at the copies.

The next switch copies those too, so each copy reaches more than the one before. Every attribute node click with the attribute dialog, every attribute hotkey and every load of a build with attribute choices goes through this function, so the shared tree keeps growing for the whole session.

Effect

  • Loading the same build with attribute choices over and over in one process (newBuild + loading the build's XML, plain PoB code) took memory to between 3 and 12 GB in some runs, usually by the 7th load. The time was spent in SwitchAttributeNode → copyTableSafe → wipeTable(subTableMap).
  • In the test suite, a spec file that loads such a build about 25 times hung in about 1 run in 8.

When this was introduced

The deep copy came with attribute node switching in #14 (2024-12-21). It is in master and in v0.23.1 too.

The fix

A shallow copy, metatable kept:

local newNode = copyTableSafe(self.tree.nodes[nodeId], true, true)

A deep copy is not needed. ReplaceNode then replaces the copy's dn, sd, mods, modKey, modList, icon, overlay, keystoneMod, activeEffectImage and reminderText with the chosen option's, and ProcessStats gives it a new mods and ModList. The other fields are shared with the tree node and are only read: options, linked, the position fields and so on. The override nodes in hashOverrides are only read afterwards: ReplaceNode copies them onto the spec's nodes, and saving reads their dn. ProcessStats may split multi-line sd entries in place, but the options' sd were already processed when the tree loaded, so that changes nothing.

Steps taken to verify a working solution:

  • Added a test in spec/System/TestPassiveSpec_spec.lua. It switches an allocated attribute node's attribute seven times and checks that the tree node's ModList has the same raw fields as before, and that the override ends up as Intelligence with +5 to Intelligence. It fails without the fix (not ok 28) and passes with it. The full suite passes: all 904 tests (docker test image).
  • In the window, after the fix, opening a build with attribute choices ten times in a row kept memory where it was: 1156 MB before, 1165 MB after (private bytes 2134 → 2146 MB).
  • In the window, after the fix: an attribute node allocated with Dexterity, the attribute nodes on a notable's path, and an allocated node switched from Strength to Intelligence each showed the attribute chosen.

Link to a build that showcases this PR:

Any build with attribute nodes allocated, for example a new character with a few attribute nodes switched to Dexterity or Intelligence.

Before screenshot:

Not visual. Before, after a few switches of the same attribute node, the shared tree node's modList has raw __call __index __newindex _className _object _parent fields, which only its ModStore proxy had before.

After screenshot:

Not visual. After, the shared tree node's modList keeps exactly the fields it had when the tree loaded.

SwitchAttributeNode deep-copied the tree node, metatables kept. The node
holds a ModList, and copying its class proxies wrote through their
__newindex into the ModList of the tree every spec shares, so each copy
reached more than the one before. Loading builds with attribute choices
again and again could take gigabytes of memory. A shallow copy is
enough: ReplaceNode and ProcessStats give it its own stats and mods.
@Wires77 Wires77 added bug Something isn't working technical Hidden from release notes and removed bug Something isn't working labels Dec 2, 2026
@Wires77
Wires77 merged commit 8913dc6 into PathOfBuildingCommunity:dev Dec 2, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

technical Hidden from release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants