Reduce the cost of the n^2 loop in collectTreeBoundaryCrossingRules
Reduce the cost of the n^2 loop in collectTreeBoundaryCrossingRules

The loop inside collectTreeBoundaryCrossingRules is iterating all possible
scopes for every element which produces n^2 behavior based on the number
of ShadowRoots in your page.

Fixing the n^2 will require refactoring the way we do tree boundary crossing
rules so that instead of just storing a list of scopes we walk the ancestor
chain of the element and look for all ancestors that contain styles.

In the short term we can skip selector matching in the loop whenever we know
the rules couldn't possibly match.

This reduces the time in the root-styles.html and the reduction-2.html by
77% (from 180ms to 40ms). deep-styles.html still runs in 15ms though, and a
profile shows that the n^2 is just hammering containsIncludingShadowDOM and
resolveReprojection now. When we fix the n^2 it should hopefully fix that.

BUG=372083

Review URL: https://codereview.chromium.org/270953003

git-svn-id: bf5cd6ccde378db821296732a091cfbcf5285fbd@174436 bbb929c8-8fbe-4397-9dbb-9b2b20218538

ondra-novak/blink,crosswalk-project/blink-crosswalk-efl,crosswalk-project/blink-crosswalk-efl,jtg-gg/blink,nwjs/blink,Pluto-tv/blink-crosswalk,hgl888/blink-crosswalk-efl,smishenk/blink-crosswalk,modulexcite/blink,jtg-gg/blink,PeterWangIntel/blink-crosswalk,hgl888/blink-crosswalk-efl,smishenk/blink-crosswalk,kurli/blink-crosswalk,PeterWangIntel/blink-crosswalk,Pluto-tv/blink-crosswalk,ondra-novak/blink,nwjs/blink,hgl888/blink-crosswalk-efl,PeterWangIntel/blink-crosswalk,XiaosongWei/blink-crosswalk,Bysmyyr/blink-crosswalk,smishenk/blink-crosswalk,hgl888/blink-crosswalk-efl,XiaosongWei/blink-crosswalk,jtg-gg/blink,PeterWangIntel/blink-crosswalk,Pluto-tv/blink-crosswalk,modulexcite/blink,smishenk/blink-crosswalk,crosswalk-project/blink-crosswalk-efl,kurli/blink-crosswalk,kurli/blink-crosswalk,modulexcite/blink,hgl888/blink-crosswalk-efl,ondra-novak/blink,nwjs/blink,smishenk/blink-crosswalk,Bysmyyr/blink-crosswalk,Bysmyyr/blink-crosswalk,Bysmyyr/blink-crosswalk,XiaosongWei/blink-crosswalk,Pluto-tv/blink-crosswalk,crosswalk-project/blink-crosswalk-efl,modulexcite/blink,jtg-gg/blink,jtg-gg/blink,ondra-novak/blink,XiaosongWei/blink-crosswalk,crosswalk-project/blink-crosswalk-efl,nwjs/blink,XiaosongWei/blink-crosswalk,smishenk/blink-crosswalk,hgl888/blink-crosswalk-efl,PeterWangIntel/blink-crosswalk,jtg-gg/blink,hgl888/blink-crosswalk-efl,XiaosongWei/blink-crosswalk,ondra-novak/blink,hgl888/blink-crosswalk-efl,hgl888/blink-crosswalk-efl,Bysmyyr/blink-crosswalk,Pluto-tv/blink-crosswalk,Pluto-tv/blink-crosswalk,nwjs/blink,Bysmyyr/blink-crosswalk,kurli/blink-crosswalk,PeterWangIntel/blink-crosswalk,kurli/blink-crosswalk,ondra-novak/blink,modulexcite/blink,crosswalk-project/blink-crosswalk-efl,smishenk/blink-crosswalk,XiaosongWei/blink-crosswalk,modulexcite/blink,smishenk/blink-crosswalk,XiaosongWei/blink-crosswalk,jtg-gg/blink,kurli/blink-crosswalk,Pluto-tv/blink-crosswalk,nwjs/blink,nwjs/blink,kurli/blink-crosswalk,jtg-gg/blink,Bysmyyr/blink-crosswalk,Bysmyyr/blink-crosswalk,hgl888/blink-crosswalk-efl,modulexcite/blink,nwjs/blink,jtg-gg/blink,crosswalk-project/blink-crosswalk-efl,kurli/blink-crosswalk,PeterWangIntel/blink-crosswalk,crosswalk-project/blink-crosswalk-efl,Pluto-tv/blink-crosswalk,ondra-novak/blink,PeterWangIntel/blink-crosswalk,modulexcite/blink,kurli/blink-crosswalk,modulexcite/blink,PeterWangIntel/blink-crosswalk,Pluto-tv/blink-crosswalk,nwjs/blink,ondra-novak/blink,PeterWangIntel/blink-crosswalk,smishenk/blink-crosswalk,ondra-novak/blink,XiaosongWei/blink-crosswalk,crosswalk-project/blink-crosswalk-efl,Bysmyyr/blink-crosswalk,kurli/blink-crosswalk,modulexcite/blink,nwjs/blink,Bysmyyr/blink-crosswalk,jtg-gg/blink,smishenk/blink-crosswalk,Pluto-tv/blink-crosswalk,XiaosongWei/blink-crosswalk