Skip to content

🐛 fix(cssom): keep computed_style linear in any call order - #2181

Merged
gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:sec-fix/bkgigz68
Nov 15, 2026
Merged

gaborbernat merged 1 commit into
tox-dev:mainfrom
gaborbernat:sec-fix/bkgigz68

Conversation

@gaborbernat

@gaborbernat gaborbernat commented Nov 15, 2026 •

Copy link
Copy Markdown
Member

Calling computed_style() on an element of a deeply nested page restyled every ancestor from the root on each call, so styling every element of such a page cost time that grew with the square of the document for XML trees. 🎨 Once this merges, each call restyles a bounded number of elements, whatever order the calls come in. Fixes #1718.

The style cache now keeps the computed values along the path of the last element asked about, and a few checkpoints further up. A call climbs only to the nearest ancestor the cache already holds, so a walk in document order styles each element once, and a call that jumps between branches restyles at most a few dozen levels above the nearest checkpoint. The computed values point at text the cache already keeps, such as the stylesheet buffers and each element's parsed style attribute, instead of copying it at every level.

Behavior changes

  • Results are unchanged.
  • A document keeps more between calls, until it is changed or released: about 1 KB per kept level, so roughly 1 MB for a chain deeper than 1024 levels.

computed_style() cascaded every ancestor of an element whose parent was
not one of the two cached maps, so its cost grew with depth times calls.

The cache now keeps the chain of the last element asked about, plus one
checkpoint per sixteen levels of chains it has walked, keyed by node, so
any access order cascades a bounded number of levels per call. Computed
values borrow stylesheet, initial and inline style text the cache holds
instead of copying it per element.

Fixes tox-dev#1718
@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 regress 3 benchmarks

2 warnings

⚠️ 19 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

⚠️ 19 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

⚡ 16 improved benchmarks
❌ 3 regressed benchmarks
✅ 563 untouched benchmarks
⏩ 89 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ test_feature[linkify-traversal-wide-callbacks] 2.5 ms 2.7 ms -6.84%
❌ test_feature[text-content] 2.6 ms 2.8 ms -6.44%
❌ test_feature[xpath-concat-nested] 5.1 ms 5.4 ms -5.16%
⚡ test_feature[computed-style-filter] 42.3 ms 29.5 ms +43.43%
⚡ test_feature[computed-style-filter-class-first] 41.8 ms 29.2 ms +42.87%
⚡ test_feature[computed-style-filter-matching] 58.8 ms 46.6 ms +26.19%
⚡ test_feature[computed-style-dense] 30.8 ms 24.6 ms +25.17%
⚡ test_feature[computed-style] 8.5 ms 7.3 ms +16.1%
⚡ test_feature[computed-style-selectors] 44.5 ms 40.5 ms +9.97%
⚡ test_feature[computed-style-selectors-reverse] 336.7 ms 306.4 ms +9.91%
⚡ test_feature[computed-style-types-reverse] 668.9 ms 609.4 ms +9.77%
⚡ test_feature[computed-style-deep] 8.8 ms 8.1 ms +9.55%
⚡ test_feature[computed-style-selectors-shallow] 1.2 ms 1.1 ms +8.91%
⚡ test_feature[computed-style-selectors-matching] 44.2 ms 40.6 ms +8.82%
⚡ test_feature[query-closest] 273.9 µs 256.7 µs +6.71%
⚡ test_feature[serialize-inner] 5.8 ms 5.5 ms +5.54%
⚡ test_feature[computed-style-specificity] 44.7 ms 42.4 ms +5.5%
⚡ test_feature[serialize-inner-indent] 6.2 ms 5.9 ms +5.33%
⚡ test_feature[select-nth] 461.8 µs 438.6 µs +5.29%

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/bkgigz68 (845df24) with main (54b4b9b)2

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. ↩

  2. No successful run was found on main (4e1f309) during the generation of this report, so 54b4b9b was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@gaborbernat
gaborbernat merged commit f904986 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.

computed_style() walks in document order take super-linear CPU time on nested markup

1 participant