Bug 45507 - Improve the local{SharedStyle,CousinList} algorithm
Summary: Improve the local{SharedStyle,CousinList} algorithm
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: CSS (show other bugs)
Version: 528+ (Nightly build)
Hardware: All All
: P2 Normal
Assignee: Julien Chaffraix
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-09-09 17:53 PDT by Julien Chaffraix
Modified: 2011-02-21 22:48 PST (History)
5 users (show)

See Also:


Attachments
Proposed fix: Use a budgeted algorithm that simplifies the code and is a slightly better trade-off (7.03 KB, patch)
2010-09-09 19:29 PDT, Julien Chaffraix
no flags Details | Formatted Diff | Diff
Proposed fix: Use a budgeted algorithm that simplifies the code and is a slightly better trade-off (without the style issues) (7.49 KB, patch)
2010-09-10 18:45 PDT, Julien Chaffraix
darin: review+
Details | Formatted Diff | Diff
Rebased patch - Also addressed Darin's comments (6.34 KB, patch)
2011-02-17 18:42 PST, Julien Chaffraix
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Julien Chaffraix 2010-09-09 17:53:48 PDT
Currently the algorithm in the CSSStyleSelector to look for cousins iterates twice to avoid a bad runtime complexity. This is arbitrary and will miss some cousin.

The idea is to change this algorithm to one that guarantees a number of visited nodes while limiting the exponential complexity (locateCousin is called in a for loop and calls itself recursively).
Comment 1 Julien Chaffraix 2010-09-09 19:29:27 PDT
Created attachment 67142 [details]
Proposed fix: Use a budgeted algorithm that simplifies the code and is a slightly better trade-off
Comment 2 WebKit Review Bot 2010-09-09 19:30:29 PDT
Attachment 67142 [details] did not pass style-queue:

Failed to run "['WebKitTools/Scripts/check-webkit-style']" exit_code: 1
WebCore/css/CSSStyleSelector.cpp:965:  Extra space before ) in if  [whitespace/parens] [5]
WebCore/css/CSSStyleSelector.cpp:966:  This { should be at the end of the previous line  [whitespace/braces] [4]
WebCore/css/CSSStyleSelector.cpp:975:  Extra space before last semicolon. If this should be an empty statement, use { } instead.  [whitespace/semicolon] [5]
Total errors found: 3 in 3 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 3 Julien Chaffraix 2010-09-10 18:45:26 PDT
Created attachment 67282 [details]
Proposed fix: Use a budgeted algorithm that simplifies the code and is a slightly better trade-off (without the style issues)
Comment 4 Dave Hyatt 2010-09-12 13:07:40 PDT
Comment on attachment 67282 [details]
Proposed fix: Use a budgeted algorithm that simplifies the code and is a slightly better trade-off (without the style issues)

I like it!
Comment 5 Darin Adler 2010-10-13 18:02:05 PDT
Comment on attachment 67282 [details]
Proposed fix: Use a budgeted algorithm that simplifies the code and is a slightly better trade-off (without the style issues)

View in context: https://bugs.webkit.org/attachment.cgi?id=67282&action=review

> WebCore/css/CSSStyleSelector.cpp:949
> +Node* CSSStyleSelector::locateCousinList(Element* parent, unsigned &visitedNodesCount)

The "&" goes next to the type rather than next to the variable name.

> WebCore/css/CSSStyleSelector.cpp:951
> +    if (visitedNodesCount >= (cStyleSearchThreshold * cStyleSearchLevelThreshold))

The parentheses here are not helpful. Please remove them.

> WebCore/css/CSSStyleSelector.cpp:1076
> +    unsigned visitedNodesCount = 0;

The phrase “visited nodes count” is not good grammar, but “visited node count” would be.

> WebCore/css/CSSStyleSelector.cpp:1079
> +    // FIXME: We should use the ElementTraversal API instead of this for-loop but we need to test the performance implication of this change.

I’m glad that you would test performance before making the change, but I don’t agree with the FIXME. Just because element traversal is a public DOM API doesn’t mean it’s a good idea to use it inside the engine. With both previousSibling and isElementNode implemented as efficient inline functions, it’s almost certainly better to call code that can do this inline. If you’d like to come back here and deploy some kind of fast implementation of element traversal, that’s OK, but I do not think it deserves a FIXME comment.

> WebCore/css/CSSStyleSelector.h:113
> +        Node* locateCousinList(Element* parent, unsigned &visitedNodes);

Please move the & next to the type.

The name visitedNodes is not good. This variable does not hold nodes, so it should not be named “visited nodes”. I think visitedNodeCount is an acceptable name.
Comment 6 Eric Seidel (no email) 2010-12-14 01:51:47 PST
What's the next step here, Julien?  Are you going to post an updated patch?
Comment 7 Julien Chaffraix 2011-02-17 18:42:28 PST
Created attachment 82888 [details]
Rebased patch - Also addressed Darin's comments

Finally got around to update this patch. Re-applied the logic on top of ToT.
Comment 8 Antti Koivisto 2011-02-20 11:06:34 PST
Looks good. Did you measure performance? I would be interested in hearing the sharing percentage on some major web sites with this vs. the old code. It should be easy to find out with some debugging code thrown in.
Comment 9 Julien Chaffraix 2011-02-21 17:03:33 PST
(In reply to comment #8)
> Looks good. Did you measure performance? I would be interested in hearing the sharing percentage on some major web sites with this vs. the old code. It should be easy to find out with some debugging code thrown in.

We did some sharing measurements on major websites. Most websites were showing small benefits (less than 5%) but on at least 2 (amazon & rakuten.co.jp) we found out that much sharing was missed by the current algorithm. In those 2 cases, the new algorithm would improve the sharing by resp. 33% (amazon) and 16% (rakuten).
Comment 10 Antti Koivisto 2011-02-21 22:10:38 PST
Comment on attachment 82888 [details]
Rebased patch - Also addressed Darin's comments

Nice. r=me assuming layout tests pass.
Comment 11 WebKit Commit Bot 2011-02-21 22:48:42 PST
Comment on attachment 82888 [details]
Rebased patch - Also addressed Darin's comments

Clearing flags on attachment: 82888

Committed r79293: <http://trac.webkit.org/changeset/79293>
Comment 12 WebKit Commit Bot 2011-02-21 22:48:48 PST
All reviewed patches have been landed.  Closing bug.