Repository navigation
perf(css): a whole stylesheet was parsed twice, once per block - #89
Merged
Merged
Conversation
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.
Closed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
parse_stylesheetkeeps 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 withcontents(). Onbench -- cssthat 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.SimpleBlockby default, soparse_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 returnsStylesheet<Block>, whereBlockisitemsplus a span, with aDropthat keeps a million nested blocks off the stack.DisplayforStylesheet<Block>,Rule<Block>andBlock, written with a stack rather than recursion.parse_stylesheet_deep.a{foo:}) left its colon behind, becauseskip(n).take(0)never runs the skip. The colon then read as a broken rule and put a spuriousInvalidRuleintoerrors. The new equivalence check found it.Tests
<urange>12 of 12; css-tokenizer-tests 287 of 287.a{…},parse_stylesheet_deepmust give exactly whatparse_stylesheetpluscontents()on every block gives, spans and errors included, and its serialization must parse back to the same text.a: {}, left when a broken declaration is reparsed as a rule, writes the text of a declaration. No selector looks like that.)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: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