Repository navigation
🐛 fix(dom): look up and remove attributes by name without rescans - #2174
Conversation
Reading or removing an attribute by name scanned the element's attribute array from the start, and removing one shifted every attribute after it. The large-element name index only served writes, and any removal left it stale, so the next write rebuilt it in full. On an element with many attributes, reading each name once, clearing the mapping, draining it with popitem() or pop(name), or deleting names in document order cost time quadratic in the attribute count. Lookups by name on an element with 32 or more attributes now use the index. A removal closes the gap from the shorter side: when the gap is in the front half, the attributes before it move up one slot and the array start advances, so removing from either end moves nothing. The index entries count slots from the start of the allocation and move with their attributes, so the index stays current through removals instead of being rebuilt. clear(), popitem() and etree Element.clear() remove by position instead of looking the name up again. Fixes tox-dev#1709
Rework the attribute index layout while resolving mutate.c. The indexed block now holds spare slots after the attribute slots, so attrs advances into them and every writer keeps main's growth check. node_attr_write is again identical to main, and GCC emits the same clones for it. Index entries name the slot modulo the capacity, and a front removal that has used every spare slot moves the array back to the block start. th_node_attr_find reaches the index through an out-of-line helper behind an unlikely branch.
Merging this PR will degrade performance by 5.77%2 warnings
Warning Please fix the performance issues or acknowledge them on CodSpeed. Performance Changes
Tip Investigate this regression by commenting Comparing Footnotes
|
Looking up, removing or draining attributes by name on an element with many attributes took time that grew with the square of the attribute count. ⚡ Parsed documents can carry such elements, and code that reads each name once, clears the mapping, or drains it with
popitem()orpop(name)paid that cost on every step. After this change each of these operations takes time linear in the attribute count, and the results are the same as before.Architecture
Elements with 32 or more attributes already kept a hash index of their names, but only writes consulted it, and a removal left the index stale until the next write rebuilt it. Lookups now use the index for those elements, and removals keep it current. Removal closes the gap from the shorter side of the array, so deleting the first or last attribute moves nothing.
clear()andpopitem()remove by position and skip the name lookup.Behavior changes
Fixes #1709