3 ms·
Are CSS parsers multithreaded? Maybe they should be? Maybe then a bit larger lookaheads woulndn't matter?
by scotty79 4y ago
Are CSS parsers multithreaded? Maybe they should be? Maybe then a bit larger lookaheads woulndn't matter?
- deleted 4y ago[deleted]
- deleted 4y ago[deleted]
- pointlessone 4y agoIt's not a question of implementation power. It's about spec requirements. Until now CSS didn't require more than 1 token to know where we are. Basically, parser doesn't need to backtrack at all. SCSS apporach, for instance, requires look ahead. Consider: color:hover over some random markup & Currently CSS can be sure whether that is a selector or a property depending where it finds it. If it's in a ruleset or at-rule block, it's a selector. If it's in a declaration list, it's a property-value pair. Now, with nesting it becomes confusing. Parsers can not rely on the state to decide what they're looking at. One way to fix this is, like many suggested here, to require look-ahead from parser. Which can be a solution in a well defined environment. Unfortunately, the Web is not one of them. We have many underpowered kiosk-type devices that probably can not spare arbitrarily large look-ahead buffers. Another solution is to somehow let the parser know what it's looking at. That's why leading & is a good indicator it's a selector. & is not used for anything else in CSS. But for selectors where it's not first we need @nest (or anything else that unambiguously marks a selector). Look-ahead requirements are bad for another reason, too. It makes CSS platform-dependent. Currently any version of CSS clearly states what it provides. Implementations can be partial but they can not claim they support CSS 2.1, for example, when they don't implement 100% of it. Wit look-aheads things become murkier. An implementation can support 100% of the spec but depending on the look-ahead required to parse a stylesheet they might not work. Currently, I don't think there are any CSS features that depend on platform limitations, at least not for parsing. Requiring a look-ahead buffer to parse a stylesheet can be very problematic. Imagine, Twitter got hacked and their embeds started stylesheet have a couple selectors that require 1M tokens look-ahead buffer. How that would break every site that embeds tweets?
- scotty79 4y agoI feel like one could just split the css around (uncontained) } and hand off each portion to microthread. Basically do it by multithreaded recursive descent. Then what on the left of { is a selector and what's on the right are the properties.
- mort96 4y agoParsing is a really difficult-to-parallelize task. There's no obvious simple way to chunk up the task, since a `{` at line 1 might affect the parsing of a construct on line 100000. What you could conceivably do is to have a very simple parser whose only job is to have just enough understanding of CSS to be able to split the CSS document into chunks, and then have a proper parser which works in parallel on the chunks produced by the first parser. I can't imagine this would be faster on most code though, considering how simple the full CSS parser is today, and it still requires a single thread which scans through and parses the entire CSS document. Regardless, browsers are already really multi-threaded. When you parallelize parsing in this way, even if parsing gets a bit faster in isolation, you're increasing the amount of computation necessary to parse the CSS (i.e spending 1 second on one core is less computational work than spending 0.5 seconds on 4 cores). Your parallel CSS parsing will be taking cycles away from HTML parsing and javascript execution.