Skip to content

🐛 fix(dom): look up and remove attributes by name without rescans - #2174

Merged
gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/82z8ogxr
Nov 15, 2026
Merged

gaborbernat merged 2 commits into
tox-dev:mainfrom
gaborbernat:sec-fix/82z8ogxr

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

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() or pop(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() and popitem() remove by position and skip the name lookup.

Behavior changes

  • Attribute order is the same after every removal, and all lookups return what they did before.
  • Removing an attribute from the middle of a very large element still moves the attributes on its shorter side, as deleting from the middle of a Python list does. The editing guide now says so.
  • The first lookup by name on an element with 32 or more attributes now builds the index, which only a write built before. Such elements then use 68 bytes per attribute slot instead of 24 (40 when a write had already indexed them). If that allocation fails, the lookup falls back to a scan.

Fixes #1709

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.
@gaborbernat gaborbernat added the bug Something isn't working label Nov 15, 2026
@codspeed

codspeed Bot commented Nov 15, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 5.77%

2 warnings

⚠️ 2 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 2 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

❌ 2 regressed benchmarks
✅ 580 untouched benchmarks
⏩ 89 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_feature[text-annotation-rules-wildcard] 89.4 ms 95 ms -5.81%
❌ test_feature[text-annotation-rules-matching] 89.7 ms 95.2 ms -5.73%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing gaborbernat:sec-fix/82z8ogxr (f34b86a) with main (7a2a98c)

Open in CodSpeed

Footnotes

  1. 89 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩

@gaborbernat
gaborbernat merged commit fe2588a into tox-dev:main Nov 15, 2026
30 of 31 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Clearing an element's attributes takes quadratic CPU time in the attribute count

1 participant