Chapter 6 - Faster CSS Rule Matching #1405
Tim-Liam-Hill
started this conversation in
General
Replies: 1 comment 1 reply
-
Hi Tim! There's a bunch of optimizations real browsers provide; a couple of sources I recommend looking at are:
But to give you a direct answer, some optimizations I think are shared by all browsers include:
Some differences between browsers include:
I am most familiar with Firefox's matching engine (Stylo) and tend to think it has the nicest code, but I'm sure this is more familiarity than anything. The Chrome source code has great documentation internally (just search the codebase for |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
I have been thoroughly enjoying following this textbook and have been challenging myself to expand on what is given where possible (for example, I expanded on the given HTML parser to handle more HTML examples).
I am currently in the middle of a CSSParser rework to better handle more complex stylesheets and was wondering what techniques are used by modern browsers to allow for faster CSS rule matching. That is to say: at present Chapter 6 results in a list of CSS rules that we iterate through for every single node to look for matches. For longer stylesheets with a lot of class rules this would be slow (and per my experience in web dev this seems to be a common approach).
One thought I have is to better group selectors together. That is to say: we can put all class selectors in a specific group of rules, all tag selectors in a specific group of rules etc. That way if a node doesn't have a class attribute we can skip iterating through that group of rules altogether.
The next idea is to use hashing within these groups. If my tag is 'd'iv then I should be able to immediately access all 'div' related rules in a dict.
These are fairly basic ideas so an further input is appreciated. I imagine there is some cool trickery with bloom filters, trees and other datastructures that modern browsers use.
Beta Was this translation helpful? Give feedback.
All reactions