Bug 136830 - Use an AtomicString as key for caching ClassNodeList objects
Summary: Use an AtomicString as key for caching ClassNodeList objects
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: DOM (show other bugs)
Version: 528+ (Nightly build)
Hardware: Unspecified Unspecified
: P2 Normal
Assignee: Chris Dumez
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2014-09-15 12:08 PDT by Chris Dumez
Modified: 2014-09-15 21:10 PDT (History)
4 users (show)

See Also:


Attachments
Patch (13.82 KB, patch)
2014-09-15 12:21 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (16.58 KB, patch)
2014-09-15 19:28 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Chris Dumez 2014-09-15 12:08:08 PDT
Use an AtomicString as key for caching ClassNodeList objects instead of a String. ClassNodeList is the only type using a String instead of an AtomicString as key in the cache HashTable. This brings some complexity.
I believe this was done to avoid unnecessarily atomizing the String, for performance reasons. However, at the moment, the String gets atomized anyway when constructing the ClassNodeList object. This is because the ClassNodeList::m_classNames member is of SpaceSplitString type and the SpaceSplitString constructor takes an AtomicString in argument.
Comment 1 Chris Dumez 2014-09-15 12:21:34 PDT
Created attachment 238137 [details]
Patch
Comment 2 WebKit Commit Bot 2014-09-15 12:24:30 PDT
Attachment 238137 [details] did not pass style-queue:


ERROR: Source/WebCore/dom/NodeRareData.h:311:  Multi line control clauses should use braces.  [whitespace/braces] [4]
Total errors found: 1 in 5 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 3 Chris Dumez 2014-09-15 15:29:41 PDT
Comment on attachment 238137 [details]
Patch

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

> Source/WebCore/dom/NodeRareData.h:312
> +        + m_tagNodeListCacheNS.size() + m_cachedCollections.size() != 1) {

I think I did what the style checker asked (add the curly brackets), it is still complaining though.
Comment 4 Benjamin Poulain 2014-09-15 16:47:19 PDT
Comment on attachment 238137 [details]
Patch

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

> Source/WebCore/ChangeLog:23
> +        No new tests, no behavior change.

I disagree. :)

If not already covered, you should have a test ensuring that there are no name conflicts in the cache.

For example:
   getElemensByTagName("foo")
   getElementsByClassNames("foo")
   ... etc.
should have independent node lists.

If such a test already exist, you should mention it in the changelog.

> Source/WebCore/dom/ClassNodeList.h:45
> +    static PassRef<ClassNodeList> create(ContainerNode& rootNode, const AtomicString& classNames)
>      {
>          return adoptRef(*new ClassNodeList(rootNode, classNames));
>      }

Looks like poor inlining. The create() function is inline and the constructor is out of line.

> Source/WebCore/dom/ContainerNode.cpp:1057
>  PassRefPtr<NodeList> ContainerNode::getElementsByClassName(const String& classNames)

You should convert this argument to AtomicString to avoid any conversion on the call sites.

> Source/WebCore/dom/NodeRareData.h:-139
> -        NodeListNameCacheMap::AddResult result = m_nameCaches.fastAdd(namedNodeListKey<T>(name), nullptr);

Shouldn't you also update namedNodeListKey() to remove the generalization?
Comment 5 Chris Dumez 2014-09-15 16:51:44 PDT
Comment on attachment 238137 [details]
Patch

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

>> Source/WebCore/ChangeLog:23
>> +        No new tests, no behavior change.
> 
> I disagree. :)
> 
> If not already covered, you should have a test ensuring that there are no name conflicts in the cache.
> 
> For example:
>    getElemensByTagName("foo")
>    getElementsByClassNames("foo")
>    ... etc.
> should have independent node lists.
> 
> If such a test already exist, you should mention it in the changelog.

I will check if we have a test and I can easily add one if missing. In any case, this case is not an issue because both the <NodeList Type, AtomicString> pair is used for hashing.

>> Source/WebCore/dom/NodeRareData.h:-139
>> -        NodeListNameCacheMap::AddResult result = m_nameCaches.fastAdd(namedNodeListKey<T>(name), nullptr);
> 
> Shouldn't you also update namedNodeListKey() to remove the generalization?

I updated namedNodeListKey() below already, to take an AtomicString in argument instead of a String. Note that the generalization is for the NodeList type, not the String type though.
Comment 6 Ryosuke Niwa 2014-09-15 18:58:59 PDT
Comment on attachment 238137 [details]
Patch

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

>> Source/WebCore/dom/NodeRareData.h:312
>> +        + m_tagNodeListCacheNS.size() + m_cachedCollections.size() != 1) {
> 
> I think I did what the style checker asked (add the curly brackets), it is still complaining though.

We shouldn't have curly brackets here.  "return false" is a single line statement.
Comment 7 Chris Dumez 2014-09-15 19:01:18 PDT
Comment on attachment 238137 [details]
Patch

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

>>> Source/WebCore/dom/NodeRareData.h:312
>>> +        + m_tagNodeListCacheNS.size() + m_cachedCollections.size() != 1) {
>> 
>> I think I did what the style checker asked (add the curly brackets), it is still complaining though.
> 
> We shouldn't have curly brackets here.  "return false" is a single line statement.

I added them because I thought this was what the style script meant:
"ERROR: Source/WebCore/dom/NodeRareData.h:311:  Multi line control clauses should use braces."

Anyway, I'll remove them since the script still isn't happy.
Comment 8 Chris Dumez 2014-09-15 19:28:50 PDT
Created attachment 238152 [details]
Patch
Comment 9 WebKit Commit Bot 2014-09-15 19:30:02 PDT
Attachment 238152 [details] did not pass style-queue:


ERROR: Source/WebCore/dom/NodeRareData.h:311:  Multi line control clauses should use braces.  [whitespace/braces] [4]
Total errors found: 1 in 8 files


If any of these errors are false positives, please file a bug against check-webkit-style.
Comment 10 Chris Dumez 2014-09-15 19:31:02 PDT
Comment on attachment 238137 [details]
Patch

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

>>> Source/WebCore/ChangeLog:23
>>> +        No new tests, no behavior change.
>> 
>> I disagree. :)
>> 
>> If not already covered, you should have a test ensuring that there are no name conflicts in the cache.
>> 
>> For example:
>>    getElemensByTagName("foo")
>>    getElementsByClassNames("foo")
>>    ... etc.
>> should have independent node lists.
>> 
>> If such a test already exist, you should mention it in the changelog.
> 
> I will check if we have a test and I can easily add one if missing. In any case, this case is not an issue because both the <NodeList Type, AtomicString> pair is used for hashing.

I added a test even (my patch does not change web-exposed behavior though) and the test would pass with or without my patch.

>> Source/WebCore/dom/ClassNodeList.h:45
>>      }
> 
> Looks like poor inlining. The create() function is inline and the constructor is out of line.

Done.

>> Source/WebCore/dom/ContainerNode.cpp:1057
>>  PassRefPtr<NodeList> ContainerNode::getElementsByClassName(const String& classNames)
> 
> You should convert this argument to AtomicString to avoid any conversion on the call sites.

Done (it is only called by the JS bindings).

>>>> Source/WebCore/dom/NodeRareData.h:312
>>>> +        + m_tagNodeListCacheNS.size() + m_cachedCollections.size() != 1) {
>>> 
>>> I think I did what the style checker asked (add the curly brackets), it is still complaining though.
>> 
>> We shouldn't have curly brackets here.  "return false" is a single line statement.
> 
> I added them because I thought this was what the style script meant:
> "ERROR: Source/WebCore/dom/NodeRareData.h:311:  Multi line control clauses should use braces."
> 
> Anyway, I'll remove them since the script still isn't happy.

Done.
Comment 11 Benjamin Poulain 2014-09-15 19:46:38 PDT
Comment on attachment 238152 [details]
Patch

Looks great. Thanks!
Comment 12 Benjamin Poulain 2014-09-15 19:50:57 PDT
(In reply to comment #10)
> > I will check if we have a test and I can easily add one if missing. In any case, this case is not an issue because both the <NodeList Type, AtomicString> pair is used for hashing.
> 
> I added a test even (my patch does not change web-exposed behavior though) and the test would pass with or without my patch.

I was not doubting the correctness of your patch. It did look absolutely correct the first round.

The reason I asked for the test is to improve the test coverage in case any future change breaks this. Since we are not rushing to finish a release, we can take our time to make perfect patches. We should take any opportunity to improve our (terrifyingly bad) test coverage.
Comment 13 WebKit Commit Bot 2014-09-15 21:10:12 PDT
Comment on attachment 238152 [details]
Patch

Clearing flags on attachment: 238152

Committed r173648: <http://trac.webkit.org/changeset/173648>
Comment 14 WebKit Commit Bot 2014-09-15 21:10:18 PDT
All reviewed patches have been landed.  Closing bug.