Hacker News new | ask | show | jobs
by pcwalton 3281 days ago
The same way Servo (and the work-in-progress Servo port to Gecko) does it. Compute styles for every DOM node in parallel, using a work-stealing scheduler to distribute nodes among threads in a thread pool. Parent nodes must have their styles computed before children, but other than that constraint it's an embarrassingly parallel problem.
2 comments

In css sibling nodes can certainly affect the position of each other, but that might be ok. A harder problem will be siblings that "run in" to each other, and overlap in various ways.
I'm talking about styling, not layout.
Work-stealing scheduler, thread pool? What is going on here?

Have any of you seen the kind of stuff recent games draw in 16 ms? It's amazing! They sure as hell don't use a work-stealing scheduler in a thread pool for single vertices, though.

Games are at an advantage because they can do a lot of up-front processing. They can statically schedule their content based on knowledge of the hardware, and programmers can and do even tell the artists to tailor their content directly to the system. Conversely, on the Web, we can't do any of this: we have no idea what the content is going to be like until we receive it, and we have to handle specifications that were written without parallelism in mind. All of the decisions we make have to be dynamic rather than static, which is why work stealing is especially effective.

Besides, vertices are a much simpler problem than CSS styling. CSS supports hundreds of properties, all of which are parsed and cascaded differently. You have properties that are inherited and properties that are noninherited. You have properties that depend on other properties: for example, any lengths might be specified in terms of ems, which means you have to have resolved the font size first. You have properties that result in completely different render trees, for example "display". You can't effectively throw stream processors like GPUs at the problem (believe me, I've tried).

Could we do better? Sure. But every proposed replacement for CSS I've seen (e.g. Cassowary) has made the complexity problem worse. And most native styling solutions I've seen are usually just very slow implementations of CSS. Separating presentation and markup is just hard, no matter what.

> But every proposed replacement for CSS I've seen (e.g. Cassowary) has made the complexity problem worse.

Hi, what other proposed replacements for CSS do you know about? I'm interested in seeing what problems they ran into.

I need to go look at Cassowary again. IIRC it's a solver, so sometimes the layout won't be what you expected, and it's very hard to know what to change such that it does what you want.

Cassowary was admittedly a bad example, because it's a layout algorithm rather than a style system. But I've seen folks suggest that we just replace CSS with TeX, for example. Using TeX would make this problem a lot worse, because styling TeX involves expanding textual macros. You would lose any possibility of parallelism, ever.
Or to put it in other words: browser technology is asked to do so much that they had to go invent a new language to keep up. So that they could serve ads.

For us ordinary folks trying to write good applications that can be maintained by one person and scale reasonably well, there's justifiable reason to jump off this rollercoaster and work in a more humble environment with modest perf/safety tradeoffs and native code executables(e.g. Go, Basic, Pascal).

The "so they could serve ads" meme is facile, reductionist, sophomoric. Actually, the fact that Google et al. were able to identify a profit function that could be optimized to fund the last 15 years of technology is an incredible achievement. In ~2000 it wasn't at all obvious that the internet could find a robust funding model.

Where the funding comes from is practically irrelevant, specially if it is a feedback function (i.e., an economic phenomenon, not "phone the legislature to fund more research!!1").

New funding models are good, too, of course. But the tone of the anti-ad camp is asinine.

A realpolitik view of tech only holds under the assumption that any new technology is good technology. It allows nothing to balance or sustainability, and I believe the trajectory of the Web is unsustainable and therefore fundamentally doomed despite its near-term wonders.

The SV camp has said nothing to convince me otherwise.

>Where the funding comes from is practically irrelevant, specially if it is a feedback function (i.e., an economic phenomenon, not "phone the legislature to fund more research!!1").

I preper phoning the legislature -- more democratic and less private interests-driven.

Games do, however, use job systems just like that for plenty of things that aren't single vertices.
Games are a completely different problem space. "styling" is a concept that games don't have; games don't deal with deciding which thing has which style at runtime. Browsers do, because they are just given CSS and have to handle the cascading and assigning of styles on their own.
> "styling" is a concept that games don't have

I disagree. Look up how modern material systems work. There absolutely are analogous things going on to CSS in PBR rendering, shaders, multipass compositing etc.

Of course they do, modern games are a complex interaction of many different shaders operating on all levels of abstraction on many different classes of runtime data that they compose, renew and adapt many many times in 16 ms.

Is the whole CSS debacle a particularly unlikely concept to yield anything close to a reasonable performing implementation? Sure, but that is because nobody has applied the pressure necessary. People won't play a game that doesn't consistently meet a 30 ms deadline but they seem to happily put up with 3 seconds to final render and call it "amazing".

Shaders are a completely different thing from styling (cascading).

GPUs work here because games fundamentally operate on a list-like datastructure (the display list), with little interdependence.

Styling on the other hand operates on a tree like datastructure (the DOM), with lots of interdependence.

Cascading isn't something games need at all, and if it was, it could be precomputed. THis can't be done in webpages.

Game engines are pretty much completely sitting on complex tree structures. Way more than the DOM. The main reason the DOM all looks so tricky is it is a hack on a hack on a hack. A lot of game engines have been rewritten from scratch many times over.
Yes, trees are involved in games too, but not at the level I'm talking about.

You don't have analogues for tree-structure-based "selectors" and anything more than straightforward property inheritance (e.g. inheritance of transformations in a skeleton) in game engines. CSS is much more complex in this aspect.

The DOM having lots of legacy issues is not the reason styling is slow.

CSS is designed for a lot of flexibility and can't be precompiled, which is why it is slow to style.