-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
LibWeb: Make named character reference tokenization more spec-compliant & efficient #3011
base: master
Are you sure you want to change the base?
LibWeb: Make named character reference tokenization more spec-compliant & efficient #3011
Conversation
Hello! One or more of the commit messages in this PR do not match the Ladybird code submission policy, please check the |
136c05a
to
d8be1ca
Compare
38aeaef
to
38b7c7f
Compare
@squeek502 I mentioned this on discord, but just to restate here: The last commit needs some extra review, but the commits before that that strictly fix bugs without refactoring are non-controversial. If you split out the last commit into its own PR I'll merge the precursors. |
There are two changes happening here: a correctness fix, and an optimization. In theory they are unrelated, but the optimization actually paves the way for the correctness fix. Before this commit, the HTML tokenizer would attempt to look for named character references by checking from after the `&` until the end of m_decoded_input, which meant that it was unable to recognize things like named character references that are inserted via `document.write` one byte at a time. For example, if `∉` was written one-byte-at-a-time with `document.write`, then the tokenizer would only check against `n` since that's all that would exist at the time of the check and therefore erroneously conclude that it was an invalid named character reference. This commit modifies the approach taken for named character reference matching by using a trie-like structure (specifically, a deterministic acyclic finite state automaton or DAFSA), which allows for efficiently matching one-character-at-a-time and therefore it is able to pick up matching where it left off after each code point is consumed. Note: Because it's possible for a partial match to not actually develop into a full match (e.g. `¬indo` which could lead to `⋵̸`), some backtracking is performed after-the-fact in order to only consume the code points within the longest match found (e.g. `¬indo` would backtrack back to `¬`). With this new approach, `document.write` being called one-byte-at-a-time is handled correctly, which allows for passing more WPT tests, with the most directly relevant tests being `/html/syntax/parsing/html5lib_entities01.html` and `/html/syntax/parsing/html5lib_entities02.html` when run with `?run_type=write_single`. Additionally, the implementation now better conforms to the language of the spec (and resolves a FIXME) because exactly the matched characters are consumed and nothing more, so SWITCH_TO is able to be used as the spec says instead of RECONSUME_IN. The new approach is also an optimization: - Instead of a linear search using `starts_with`, the usage of a DAFSA means that it is always aware of which characters can lead to a match at any given point, and will bail out whenever a match is no longer possible. - The DAFSA is able to take advantage of the note in the section `13.5 Named character references` that says "This list is static and will not be expanded or changed in the future." and tailor its Node struct accordingly to tightly pack each node's data into 32-bits. Together with the inherent DAFSA property of redundant node deduplication, the amount of data stored for named character reference matching is minimized. In my testing: - A benchmark tokenizing an arbitrary set of HTML test files was about 1.23x faster (2070ms to 1682ms). - A benchmark tokenizing a file with tens of thousands of named character references mixed in with truncated named character references and arbitrary ASCII characters/ampersands runs about 8x faster (758ms to 93ms). - The size of `liblagom-web.so` was reduced by 94.96KiB. Some technical details: A DAFSA (deterministic acyclic finite state automaton) is essentially a trie flattened into an array, but it also uses techniques to minimize redundant nodes. This provides fast lookups while minimizing the required data size, but normally does not allow for associating data related to each word. However, by adding a count of the number of possible words from each node, it becomes possible to also use it to achieve minimal perfect hashing for the set of words (which allows going from word -> unique index as well as unique index -> word). This allows us to store a second array of data so that the DAFSA can be used as a lookup for e.g. the associated code points. For the Swift implementation, the new NamedCharacterReferenceMatcher was used to satisfy the previous API and the tokenizer was left alone otherwise. In the future, the Swift implementation should be updated to use the same implementation for its NamedCharacterReference state as the updated C++ implementation.
38b7c7f
to
85649cb
Compare
Makes sense; split the straightforward bug fixes into #3163 (just realized you probably preferred the DAFSA commit to go in the new PR; whoops!) |
This PR focuses on named character reference tokenization, both in terms of correctness and performance.
Tons of details in the commit message, so see that for the full explanation. This implementation is also based on this implementation written in Zig, and there are more nitty-gritty details available in its README if you're interested.
The tl;dr is that a DAFSA is generated at compile-time and used for efficient codepoint-by-codepoint named character reference matching.
I was not able to get a working build with
-DENABLE_SWIFT
(instructions on this would be appreciated), so I left the Swift HTMLTokenizer implementation alone and used the new NamedCharacterReferenceMatcher to satisfy the previousmatch_entity_for_named_character_reference
API. In other words, the Swift version will work exactly the same as it did before this PR.If it's helpful for reviewing, here's what the generated NamedCharacterReferences.h/cpp look like:
https://gist.github.com/squeek502/550164a8d5549e8a2bf45ece4ce38045
In a benchmark using an arbitrary set of test files found online, the tokenizer is about 1.23x faster (2070ms to 1682ms).
General benchmark code
(this was a bit surprising, I wasn't expecting this to affect the average case much)
In a benchmark hyper-focused on named character references (see
named-char-test.html
here for the test file used, it's just tens of thousands of valid and invalid named character references), the tokenizer is ~8x faster (758ms to 93ms).Named character reference benchmark code
The amount of data stored for the named character reference matching has also been reduced by around 95KiB (just going off the size of
liblagom-web.so
before/after these changes).With this commit, many WPT subtests now pass, and when combined together with #3163, AFAICT all character reference-related subtests are now passing. Here are the results from running the WPT tests in
/html/syntax/parsing
(comparing master to this branch + #3163):WPT results for /html/syntax/parsing
There are probably other newly passing tests/subtests elsewhere (one I'm aware of is
html/webappapis/dynamic-markup-insertion/document-write/041.html
)