Skip to content

Optimize CSS visibility engine with lazy parsing and cache-friendly evaluation#1933

Merged
karlseguin merged 5 commits intocss-improvementsfrom
css-improvements-perf3
Mar 20, 2026
Merged

Optimize CSS visibility engine with lazy parsing and cache-friendly evaluation#1933
karlseguin merged 5 commits intocss-improvementsfrom
css-improvements-perf3

Conversation

@arrufat
Copy link
Copy Markdown
Contributor

@arrufat arrufat commented Mar 20, 2026

This PR brings significant performance improvements to the new CSS engine by shifting to a lazy architectural model and optimizing hot-path lookups for cache locality.

  • Lazy Architectural Model:
    • Deferred the allocation of heavy JS-backed CSSStyleRule and CSSStyleDeclaration objects. Stylesheets are now only parsed into the full DOM object tree on-demand (e.g., if queried by Javascript).
    • Implemented a fast-path raw text parser for the StyleManager, allowing it to build its internal visibility rules without triggering thousands of JS object allocations during page load.
  • Cache-Friendly Data Structures (SoA):
    • Refactored internal rule storage to use Structure-of-Arrays (SoA) via std.MultiArrayList.
    • This allows the evaluation loop to iterate over lightweight priority integers in a dense cache line, fast-skipping irrelevant rules without loading heavier property or selector data.
  • Simplified Priority Logic:
    • Combined CSS specificity and document order into a single packed u64 priority.
    • This replaced complex tie-breaking logic with a single, native > integer comparison, simplifying the code while increasing speed.
  • Early Rejection:
    • Implemented early exits in evaluation loops for elements already determined to be hidden by higher-priority rules or inline styles.

Impact: Closed the performance regression on large page fetches by around 1 second (from 7.5 sto 6.5 s) while the main branch does it in about 6 s (but doesn't compute the styles correctly)

@arrufat arrufat requested a review from karlseguin March 20, 2026 03:48
@karlseguin karlseguin merged commit 2d91acb into css-improvements Mar 20, 2026
12 checks passed
@karlseguin karlseguin deleted the css-improvements-perf3 branch March 20, 2026 09:07
@github-actions github-actions Bot locked and limited conversation to collaborators Mar 20, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants