News for hackers. Stuff that matters.
Re:A Shocking Truth About CSS by Jim72 on Tuesday March 09, 2010 @10:05AM (#1178382)
Please, using more CSS selectors than DOMs isn't going to kill a site. In fact, it isn't even close to being a bottleneck. We are talking milliseconds. If you want speed gains, properly compress your images!
Re:A Shocking Truth About CSS by flatline on Tuesday March 09, 2010 @10:19AM (#1178415)
I've known about how selectors are processed for some time, I discovered it when I was looking into optimization strategies for JavaScript and figured I should look into how CSS is processed. I made a variety of tweaks, including finally just removing nearly all the CSS, and (somewhat disappointed) did not see any visible changes in the speed at which the browser rendered the page. Not even on IE6/7. Anecdotal evidence only, but to Jim72's point, I wouldn't waste time on it.
Re:A Shocking Truth About CSS by jrockway on Tuesday March 09, 2010 @10:15AM (#1178400)
Wait, are you saying that a computer capable of executing millions of instructions per second doesn't take very long to do a few comparisons on a dozen or so elements? It takes me a long time to do by hand, so I figured the computer would be really slow too!
Re:A Shocking Truth About CSS by Tuna-Fish on Tuesday March 09, 2010 @10:58AM (#1178538)
> a computer capable of executing millions of instructions per second

Billions, not millions.

Re:A Shocking Truth About CSS by btilly on Tuesday March 09, 2010 @11:20AM (#1178597)
Billions of cycles per second. But executing an instruction frequently takes 20 cycles or so. That reduces instructions to hundreds of millions.
Re:A Shocking Truth About CSS by Tuna-Fish on Tuesday March 09, 2010 @05:50PM (#1179609)
The core i7 can issue 4 instructions, and execute 6 micro-ops per cycle. As long as your code is not a single huge dependency chain, you are mostly reading data out of L1 cache, and you don't use complex instructions like division, you can expect to get at least 2 instructions per clock. I have profiled real code (crypto stuff) that got a tad bit over 3 instructions per cycle. That's 10 billion instructions a second on the processor it ran on.

Most instructions have not taken over 2 cycles on x86 since P4. This is a mesofact, update your worldview. :)

Re:A Shocking Truth About CSS by kscaldef on Tuesday March 09, 2010 @11:56AM (#1178725)
Except that pipelining, multiple cores, and hyperthreading then bring it back up. So, let's not get overly pedantic here.
Re:A Shocking Truth About CSS by sp332 on Tuesday March 09, 2010 @12:03PM (#1178753)
Well, it doesn't happen in real life, but an Intel i7 is capable of issuing 4 instructions per clock cycle (sometimes 5, with a little microcode cheating). A 3.2 GHz quad-core could issue 51.2 billion instructions per second.
Re:A Shocking Truth About CSS by jacoblyles on Tuesday March 09, 2010 @11:50AM (#1178707)
Presumably any modern processor would use some form of pipelining, which I think would take instructions back up into the billions. (The pentium 4, if I recall correctly, had a 20+ stage pipeline)
Re:A Shocking Truth About CSS by bruceboughton on Tuesday March 09, 2010 @12:17PM (#1178805)
From CSS to processor design in just 5 comments! Only on HN ;)
Re:A Shocking Truth About CSS by gridspy on Tuesday March 09, 2010 @03:45PM (#1179314)
Long pipelines cause issues when branches get mis-predicted. The half processed instructions have to be thrown away.

What you want is a CPU with a short pipeline, a fast clock rate and if possible instruction reordering and multiple dispatch.

Large caches and good branch prediction help a lot too.

Re:A Shocking Truth About CSS by goodside on Tuesday March 09, 2010 @09:34AM (#1178301)
Scroll to the botom, and:

11:44:33 AM Alex K: that makes it sound like it may not be worth huge optimization?

11:44:46 AM Alex M: no, not really

Re:A Shocking Truth About CSS by jbox on Tuesday March 09, 2010 @01:12PM (#1178981)
Steve's final conclusion:

“For most web sites, the possible performance gains from optimizing CSS selectors will be small, and are not worth the costs. ”

http://www.stevesouders.com/blog/2009/03/10/performance-impa...

Re:A Shocking Truth About CSS by rimantas on Tuesday March 09, 2010 @01:25PM (#1179023)
Exactly. I have an obsession with "optimized" markup, that means avoiding unnecessary elements, ids and classes. Let's not forget that all this stuff sits in markup and gets downloaded on each request. Even if that does not add up to much, I still want to have my code "clean". There is one particular site that inspires me: http://camendesign.com/ — take a look, not a single id or class.
Re:A Shocking Truth About CSS by jqueryin on Tuesday March 09, 2010 @02:43PM (#1179189)
He did this, however, at the expense of convoluted selectors, pseudo-elements, and pattern matching. Personally, I don't find his implementation to be very clean. I guess it's better to have the mess in your closet than wide out in the open, though.
Re:A Shocking Truth About CSS by arsduo on Tuesday March 09, 2010 @06:18PM (#1179663)
It's not every day you can experience all the astonishment of overturning a fundamental assumption without any of the pain of forced changes. I enjoyed that conversation. (I'm Alex K, btw.)

Thanks for all the comments, everyone! Very cool to have that post picked up by Hacker News.

Re:A Shocking Truth About CSS by daleharvey on Tuesday March 09, 2010 @12:53PM (#1178916)
most of the time this certainly falls under pointless microptimisation, but if you are doing a complex web application with cpu intensive operations, it can be really handy to know how css is applied (and how reflow / redraw works) to help the user experience.
Re:A Shocking Truth About CSS by stanleydrew on Tuesday March 09, 2010 @09:57AM (#1178357)
Google describes this and a bunch of other great stuff about how to optimize page rendering: http://code.google.com/speed/page-speed/docs/rendering.html#...
Re:A Shocking Truth About CSS by bwill on Tuesday March 09, 2010 @09:40AM (#1178318)
Can anyone point to where this is required by the css specification? Or is this an implementation detail? The reality may be that all the browsers do it this way, but why couldn't someone add some more smarts to choose selectors based on selectivity, just as a SQL query planner would?
Re:A Shocking Truth About CSS by robin_reala on Tuesday March 09, 2010 @09:47AM (#1178329)
It’s quicker to walk up a DOM tree than it is to walk down - that’s the root reason why browsers have all implemented it this way.
Re:A Shocking Truth About CSS by neilk on Tuesday March 09, 2010 @10:02AM (#1178371)
Yeah, but if we do something like this:

    #alert > p { background: yellow }
Surely the intelligent thing for the CSS engine is to look for the one-and-only #alert, rather than work backwards from all the <p>'s? (I am assuming that nodes with ids have their own fast index.)
Re:A Shocking Truth About CSS by robin_reala on Tuesday March 09, 2010 @10:20AM (#1178418)
id attributes are unique in well formed pages, but then a lot of HTML is not well formed. Browsers do maintain a hashmap (or equiv) of id to node though, so in that case you’re right, that should be faster. Maybe the overhead of parsing and choosing multiple code paths is slower than just walking the tree?
Re:A Shocking Truth About CSS by gizmo on Tuesday March 09, 2010 @12:36PM (#1178867)
It's a little trickier when you do it left to right.

Suppose you have a DOM tree like this:

    div #alert - p, p, p
           - div #alert - p [lots of children]
                        - p - div #alert [lots of children]
[right to left]

In order to find all Ps with an #alert parent, you first get a set of all paragraph elements. Most likely a list of all paragraphs is kept in memory, so it can be enumerated quickly. Since DOM trees are generally well balanced the number of parents of any one paragraph is going to be roughly O( 2log(|DOMSIZE|) ) and it can be enumerated in C, so this is basically nothing (compared to how grossly inefficient javascript is). Total complexity: O( |P NODES| * 2log( |DOMSIZE| ) ). Memory complexity: 1 linked list of size of result set.

[left to right]

If we were to look at it from left to right, it gets trickier. You have to find all #alert nodes (easy & fast), but then you have to take all children of all #alert nodes. Every sub-tree of the DOM can be a significant fraction of the entire DOM tree, or perhaps the _entire_ dom tree. Not only that, but you CANNOT filter for all "p" children, because then you'd need to have lookup tables for every level in the tree (hugely inefficient). So you're left with one solution, that is to enumerate all children (and a DOM node can have a few thousand child nodes easily). But, wait, one #alert contains another #alert. We're already looking at O( |DOMSIZE| * |DOMSIZE| ), and now we have to consider uniqueness as well. We cannot alloc a hash map to keep track of uniqueness, because that would completely fragement your memory space; you'd end up allocating&deallocating hashmaps for every selector call. So you end up having to traverse the result set linked list for every node you add, to see if it isn't already in there. This, needless to say, is another O(N^2) detour. Total worst case complexity for M selectors:

    O( M * |DOMSIZE|^2 + M * |RESULTSET|^2 ).
Memory complexity: M + 1 linked list.

Difference becomes more profound as complexity increases:

    #uniq p p span b
(R2L) find all "b" nodes, append those with matching parents to linked list. Return linked list

(L2R) find all #uniq nodes, enumerate all "p" children. Make sure all "p" children are unique. Take all children of these "p" nodes, filter for "p" children, make sure result set is unique. Take all children of these "p" nodes, filter for "span", make sure.... and so it goes.

So that's why.

Re:A Shocking Truth About CSS by qjz on Tuesday March 09, 2010 @01:21PM (#1179007)
Your examples aren't valid HTML, so that approach is fine for quirks mode. But what about valid pages that declare a DOCTYPE? Shouldn't those be parsed from left to right, so the first matching ID that is encountered wins (with any other matching IDs considered errors and disregarded)?

OT, this was one of the problems XHTML was supposed to solve, since it was originally assumed by many that pages that weren't well-formed XML would not be rendered at all, forcing the developers to fix the code. This didn't happen, and developers are still at the mercy of how individual browsers implementat quirks mode.

Re:A Shocking Truth About CSS by gizmo on Tuesday March 09, 2010 @04:41PM (#1179469)
You have to write R2L parsing logic anyway, because it's what you want most of the time (e.g. "div span a.quote"). Yes, in the special case where #uniq identifiers are used and you know the page to be valid (X)HTML and you know that you get a substantial speedup by filtering on the #uniq node first L2R parsing is preferable. But how realistic is that, really?

Isn't it just easier to teach web developers to write their DOM selectors in a specific way? The R2L approach is (a) easy to understand (b) has predictable (and stable) performance (c) doesn't malloc (d) is easy to implement. I see this as a simple case of "good enough".

Re:A Shocking Truth About CSS by isleyaardvark on Tuesday March 09, 2010 @01:46PM (#1179072)
But then the browser would have to validate the page, and then render or re-render the document, wouldn't it?
Re:A Shocking Truth About CSS by DougBTX on Tuesday March 09, 2010 @07:13PM (#1179769)
that approach is fine for quirks mode

And it is also fine in standards mode - double win.

Re:A Shocking Truth About CSS by niyazpk on Tuesday March 09, 2010 @11:41AM (#1178674)
Can you please explain why this is so?
Re:A Shocking Truth About CSS by robin_reala on Tuesday March 09, 2010 @11:53AM (#1178715)
A DOM node can have multiple children but only one parent. To go up the tree and check for a hit you simply call (in pseudocode) node.parent.isOK?.parent.isOK? etc. To go down the tree you have to not only choose your search strategy (depth-first, breadth-first) but also check a whole lot more nodes in the hope that you’ll find what you want.
Re:A Shocking Truth About CSS by ableal on Tuesday March 09, 2010 @09:37AM (#1178310)
(from the header:)

The Two Alexs

Alexes? Alex's? Alexi? Alexim?

In hackanonical form, Alexen. As in oxen, vaxen, boxen.

Re:A Shocking Truth About CSS by pohl on Tuesday March 09, 2010 @11:25AM (#1178613)
Hackanonical is my new favorite word. Thank you for that.
Re:A Shocking Truth About CSS by riffraff on Tuesday March 09, 2010 @11:03AM (#1178546)
IIRC oxen is a vestigial dual (not plural) form, so quite appropriate.
Re:A Shocking Truth About CSS by bdr on Tuesday March 09, 2010 @04:05PM (#1179369)
According to http://www.etymonline.com/index.php?term=ox -- "Oxen is the only true survival in Mod.Eng. of the O.E. weak plural."

From what I can tell, it looks like OE nouns could have either "strong" or "weak" declensions, and this is different from the dual form. Someone on another forum claims that "brethren" is another example.

Re:A Shocking Truth About CSS by riffraff on Tuesday March 09, 2010 @04:16PM (#1179407)
may well be true, my source is an old State of the Onion talk from Larry Wall, which although a linguist is probably not the most qualified in the world :)
Re:A Shocking Truth About CSS by jamesbritt on Tuesday March 09, 2010 @12:07PM (#1178759)
So four ox = oxenen?
Re:A Shocking Truth About CSS by goodside on Tuesday March 09, 2010 @09:43AM (#1178324)
"Alices", by analogy with "codex".
Re:A Shocking Truth About CSS by davidw on Tuesday March 09, 2010 @09:49AM (#1178335)
It's a Greek name, so I'd go with that for plurals, rather than German or Latin templates.
Re:A Shocking Truth About CSS by Torn on Tuesday March 09, 2010 @09:48AM (#1178334)
Alexes is the best of the bunch.
Re:A Shocking Truth About CSS by barredo on Tuesday March 09, 2010 @11:46AM (#1178690)
Re:A Shocking Truth About CSS by ableal on Tuesday March 09, 2010 @11:59AM (#1178736)
Sorry about that, just made it up on the fly. Hacker(-speak) + canonical (form) portmanteau.
Re:A Shocking Truth About CSS by arsduo on Tuesday March 09, 2010 @06:34PM (#1179699)
Alexen is awesome. I just added it to the header. (Hackanonical is even more awesome, but it sadly doesn't fit there.)

Thanks for the words!

Alex K

PS I appreciate how strong codex/codices example is, but, well, Alices, not so much.

Re:A Shocking Truth About CSS by blasdel on Wednesday March 10, 2010 @05:11AM (#1180589)
Take Alex's, Alexi, Alexim, Alexen, and boxen out back to be euthanized along with the awful virii and anything else that would lead Cory Doctorow to chortle while typing.

Hackanonical, however, is without flaw. It should be in the name of this site!

Re:A Shocking Truth About CSS by techiferous on Tuesday March 09, 2010 @10:11AM (#1178397)
As in regexen. :)
Re:A Shocking Truth About CSS by eru on Tuesday March 09, 2010 @11:39AM (#1178667)
And Socksen.
Re:A Shocking Truth About CSS by CoryMathews on Tuesday March 09, 2010 @10:45AM (#1178488)
micro optimization... blah.

Why do I really doubt Alex's yoga site needs to worry about this.

Re:A Shocking Truth About CSS by ihumanable on Tuesday March 09, 2010 @11:22AM (#1178605)
John Resig (of jQuery fame) talks about why this approach was taken in the design of the sizzle engine that powers jQuery. Its towards the end of this talk (which is quite good by the way), I would find the exact time but my work internet thinks its inappropriate content and won't let me watch the video :(
Re:A Shocking Truth About CSS by todd3834 on Tuesday March 09, 2010 @11:29AM (#1178623)
could you post a link to it?
Re:A Shocking Truth About CSS by eneveu on Wednesday March 17, 2010 @09:12PM (#1200196)
Reminds me of how you may optimize your javascript DOM/style manipulations to avoid re-calculating the styles too often. It was mentioned in this Google I/O session on GWT performance: http://www.youtube.com/watch?v=q9hhENmVTWg#t=32m38s (slides located here: http://code.google.com/events/io/2009/sessions/MeasureMillis... )

This is where Speed Tracer comes in handy ( http://code.google.com/webtoolkit/speedtracer/ )

Re:A Shocking Truth About CSS by kellegous on Tuesday March 09, 2010 @01:14PM (#1178992)
If you want to know how much CSS selector matching (sometimes called Style Recalculation) is affecting your site you can try Speed Tracer:

https://chrome.google.com/extensions/detail/ognampngfcbddbfe...

It's Chrome-only but you can sometimes get cross-browser insights. It's also a lot easier to conduct a cursory investigation using this tool than it is to start changing your selectors.

Re:A Shocking Truth About CSS by smackfu on Tuesday March 09, 2010 @10:24AM (#1178433)
I do not like their blog gimmick.
Re:A Shocking Truth About CSS by arsduo on Tuesday March 09, 2010 @06:43PM (#1179716)
FWIW, the post is a slightly cleaned up version of an actual conversation I had with my husband. (I'm K, he's M.) It's not a format I'm likely to reuse (or have used before), but it fit the occasion and captured very well our feelings of surprise at learning how CSS parses selectors.

Plus, it's always fun to try out new formats, even -- or maybe especially -- if they don't stick.

Re:A Shocking Truth About CSS by jamesr on Wednesday March 10, 2010 @02:52AM (#1180429)
The definitive guide to this was written by then-Mozilla dev Dave Hyatt:

https://developer.mozilla.org/en/Writing_Efficient_CSS

That's from April 21st, 2000 - almost a decade ago.

Re:A Shocking Truth About CSS by scotty79 on Tuesday March 09, 2010 @10:32PM (#1180155)
11:44:33 AM Alex K: that makes it sound like it may not be worth huge optimization? 11:44:46 AM Alex M: no, not really 11:44:56 AM Alex M: I’m a purist though 11:45:18 AM Alex M: so this makes my world

Last two lines scares the hell out of me.

Re:A Shocking Truth About CSS by aw3c2 on Tuesday March 09, 2010 @12:56PM (#1178929)
I don't want to read several pages of badly spoken chat english to find the conclusion. Could someone be so kind and explain what the shocking truth is?
Re:A Shocking Truth About CSS by gscott on Tuesday March 09, 2010 @10:43PM (#1180165)
That tables really are the best, CSS is good for changing fonts all at once though. I read it all for you so don't worry about double checking.
Re:A Shocking Truth About CSS by aw3c2 on Wednesday March 10, 2010 @07:01AM (#1180693)
Thanks!
Re:A Shocking Truth About CSS by FluidDjango on Tuesday March 09, 2010 @09:41AM (#1178323)
The "Two Alexes" are just discovering (Feb 2010) what Steve Souders explained in detail at http://www.stevesouders.com/blog/2009/06/18/simplifying-css-... [which is probably the site you want to visit on "How CSS strategies affect site performance"] (June 2009).
Re:A Shocking Truth About CSS by tjic on Tuesday March 09, 2010 @09:53AM (#1178345)
Being "scooped" by a mere 6 months, on a 10 year old technology doesn't really make their realization dated.

It's new to me, and I bet it's new to most news.yc-ers.

Re:A Shocking Truth About CSS by jamesr on Wednesday March 10, 2010 @02:53AM (#1180430)
Scooped by 10 years on a 10 year old technology, not 6 months. http://news.ycombinator.com/item?id=1180429
Re:A Shocking Truth About CSS by jrockway on Tuesday March 09, 2010 @10:17AM (#1178409)
/me mumbles something about Reading The Fine Source. Then you don't have to guess what your browser is doing.
Re:A Shocking Truth About CSS by smackfu on Tuesday March 09, 2010 @10:25AM (#1178436)
Trying to figure out HTML from reading the Firefox source is kind of insane.
Re:A Shocking Truth About CSS by jrockway on Tuesday March 09, 2010 @10:47AM (#1178498)
Yeah, OK. So just be ignorant instead.
Re:A Shocking Truth About CSS by etherealG on Tuesday March 09, 2010 @11:34AM (#1178639)
do you read the source of the linux kernel to understand how to use bash? how about the source of a wifi driver before connecting to a network?

your premise is rediculous.

Re:A Shocking Truth About CSS by jrockway on Tuesday March 09, 2010 @12:28PM (#1178838)
I don't do those things, but that's not because it's a bad method, but because I don't care. If there was some bug in some system call, or if I needed to investigate why hardware is misbehaving, of course I'd read the Linux source. That's one of the great things about open source software; you don't have to treat it like a black box. You can just look at the internals and see how they work.

(And I have proof that I do actually do this; I had a misbehaving joystick, and the linux fork in my github contains the fix.)

Anyway, in conclusion, if you want to know what algorithm Firefox uses to process CSS selectors, just read the Firefox source code. If you don't give a damn, then don't do that instead. But if you care and you do nothing, that's just silly.

Re:A Shocking Truth About CSS by threepointone on Tuesday March 09, 2010 @12:28PM (#1178840)
No no, his premise is delicious!

yummy trollspam.

Re:A Shocking Truth About CSS by scotty79 on Tuesday March 09, 2010 @07:46PM (#1179845)
Don't think about how fast it is unless you have solid evidence that it is slow.