Skip to content

perf(css): a whole stylesheet was parsed twice, once per block - #89

Merged
jamesconwaydev merged 3 commits into
mainfrom
css-parse-speed
Nov 19, 2026
Merged

jamesconwaydev merged 3 commits into
mainfrom
css-parse-speed

Conversation

@jamesconwaydev

Copy link
Copy Markdown
Collaborator

Problem

parse_stylesheet keeps each rule's {} block as component values, so getting every declaration in a sheet meant building each block as a tree and then walking it again with contents(). On bench -- css that was 148 MB/s, where Servo's cssparser 0.38 parses at 196 (#88).

Change

  • parse_stylesheet_deep(css): the whole stylesheet with every block's declarations and nested rules, parsed straight from the tokenizer as the parser reaches each block. The open blocks are a stack, so nesting doesn't recurse, and their items share one buffer, so each list is allocated once at its size. Where an ident turns out not to start a declaration (a:hover { … } nested), the tokenizer goes back to the ident and reads it again as a rule: the spec's "put back", with a position.
  • The rule types take what their block is kept as as a type parameter, SimpleBlock by default, so parse_stylesheet, contents() and every other entry point are unchanged and code written against feat(css): Parsec had no CSS parser to run css-parsing-tests against #87 compiles as it was. The deep parse returns Stylesheet<Block>, where Block is items plus a span, with a Drop that keeps a million nested blocks off the stack.
  • Display for Stylesheet<Block>, Rule<Block> and Block, written with a stack rather than recursion.
  • The bench's Parsec parse is now parse_stylesheet_deep.
  • Fixed in the raw path: a declaration with an empty value (a{foo:}) left its colon behind, because skip(n).take(0) never runs the skip. The colon then read as a broken rule and put a spurious InvalidRule into errors. The new equivalence check found it.

Tests

  • css-parsing-tests still 293 passed, 12 failed (the same listed 12), 8,061 skipped; <urange> 12 of 12; css-tokenizer-tests 287 of 287.
  • New: for every input in css-parsing-tests, and for each again wrapped in a{…}, parse_stylesheet_deep must give exactly what parse_stylesheet plus contents() on every block gives, spans and errors included, and its serialization must parse back to the same text.
  • The fuzz target checks the same equivalence on every input; it ran 819,000 inputs in five minutes, clean. (It doesn't check the deep serialization reads back: a rule like a: {}, left when a broken declaration is reparsed as a rule, writes the text of a declaration. No selector looks like that.)
  • Unit tests for 300,000 nested blocks (parse, write, drop) and for an ident read again as a rule.

cargo run --release --manifest-path bench/Cargo.toml -- css, twice on an M2 Max, each started with the one-minute load at 2.5, ten real stylesheets (1.87 MB), MB/s for all ten, second run:

Parsec Lexbor 3.0.1 cssparser 0.38
Tokenizing 389 210 318
Parsing 211 112 196

The first run gave 220, 115 and 190 parsing (1.16 times cssparser's speed; the second is 1.08). Parsec parses faster than cssparser on every sheet in the first run, by 1.09 to 1.27 times, and on nine of ten in the second, where Bulma came out level (232 to 234; the first run had it 263 to 233). It's ahead of Lexbor on every sheet in both, by 1.6 to 2.7 times parsing.

cargo fmt --all, cargo clippy --workspace --all-targets -- -D warnings, cargo test --workspace, cargo deny check, and the bench's clippy pass.

Closes #88

parse_stylesheet kept each rule's {} block as component values, and a
caller wanting every declaration then walked each block again with
contents(). parse_stylesheet_deep does both in one pass: each block's
declarations and rules are parsed straight from the tokenizer as the
parser reaches them, with a stack of open blocks so nesting doesn't
recurse, and a shared buffer so each list is allocated once at its size.

The rule types take the block as a type parameter, defaulting to the
raw SimpleBlock, so every existing entry point and the raw-block API
are unchanged; the deep parse returns Stylesheet<Block>.

Also fixes the raw path leaving a declaration's colon behind when its
value was empty (`a{foo:}`), which then read as a rule and reported a
spurious InvalidRule error.
@jamesconwaydev
jamesconwaydev merged commit 496ba8b into main Nov 19, 2026
32 checks passed
@jamesconwaydev
jamesconwaydev deleted the css-parse-speed branch November 19, 2026 05:18
@jamesconwaydev jamesconwaydev mentioned this pull request Nov 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

parsec-css parses 0.76x as fast as cssparser

1 participant