Bug 136825 - Rename Node::childNode(index) to traverseToChildAt(index) for clarity
Summary: Rename Node::childNode(index) to traverseToChildAt(index) for clarity
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 08:42 PDT by Chris Dumez
Modified: 2014-09-16 19:00 PDT (History)
4 users (show)

See Also:


Attachments
Patch (27.41 KB, patch)
2014-09-15 09:22 PDT, Chris Dumez
no flags Details | Formatted Diff | Diff
Patch (27.40 KB, patch)
2014-09-16 17:51 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 08:42:43 PDT
Rename Node::childNode(index) to traverseToChildAt(index) to make it clearer that the method is actually traversing the children and thus is potentially expensive.
We should also avoid calling this method whenever there is a more efficient alternative.
Comment 1 Chris Dumez 2014-09-15 09:22:12 PDT
Created attachment 238131 [details]
Patch
Comment 2 Benjamin Poulain 2014-09-16 00:01:13 PDT
Comment on attachment 238131 [details]
Patch

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

I like it!

> Source/WebCore/WebCore.exp.in:-1629
> -__ZNK7WebCore13ContainerNode9childNodeEj

Check UIKit before landing just in case.

> Source/WebCore/dom/ContainerNode.cpp:945
> +    for (; child && index > 0; --index)

I am surprised you had to manually write the descending loop, clang is usually very good at that.

A random thought: if this code is hot enough, you should dump the average value of "index" in a typical use. If it is large enough, it could be worth doing some manual unrolling.

> Source/WebCore/inspector/DOMPatchSupport.cpp:381
> +    for (size_t i = 0; node && i < newMap.size(); ++i, node = node->nextSibling()) {

size_t -> unsigned.

Personally I would use a while() here instead of for() because of the return in the loop and the non-obvious iteration steps.
Comment 3 Darin Adler 2014-09-16 09:01:16 PDT
We normally use “traverse” to refer to tree traversal, not just walking the linear child list.
Comment 4 Chris Dumez 2014-09-16 09:05:07 PDT
(In reply to comment #3)
> We normally use “traverse” to refer to tree traversal, not just walking the linear child list.

Do you have another naming suggestion? I thought iterating over the direct children still counted as a tree traversal, but if there is a better name for it, I am happy to make the update.
Comment 5 Chris Dumez 2014-09-16 09:17:52 PDT
(In reply to comment #3)
> We normally use “traverse” to refer to tree traversal, not just walking the linear child list.

Since you're using the term "walking", do you think walkToChildAt() would be a better alternative?
I don't feel strongly either way ("traversing" a linked list sounded OK to me).
Comment 6 Chris Dumez 2014-09-16 11:16:08 PDT
Comment on attachment 238131 [details]
Patch

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

>> Source/WebCore/dom/ContainerNode.cpp:945
>> +    for (; child && index > 0; --index)
> 
> I am surprised you had to manually write the descending loop, clang is usually very good at that.
> 
> A random thought: if this code is hot enough, you should dump the average value of "index" in a typical use. If it is large enough, it could be worth doing some manual unrolling.

I did not check the generated assembly to see if there was a difference. Since I was updated this function for coding style, I figured I would refactor it a bit. The purpose was not really to optimize.

>> Source/WebCore/inspector/DOMPatchSupport.cpp:381
>> +    for (size_t i = 0; node && i < newMap.size(); ++i, node = node->nextSibling()) {
> 
> size_t -> unsigned.
> 
> Personally I would use a while() here instead of for() because of the return in the loop and the non-obvious iteration steps.

newMap.size() returns a size_t so wouldn't be using unsigned for |i| be risky?
Comment 7 Chris Dumez 2014-09-16 11:20:43 PDT
Comment on attachment 238131 [details]
Patch

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

>> Source/WebCore/WebCore.exp.in:-1629
>> -__ZNK7WebCore13ContainerNode9childNodeEj
> 
> Check UIKit before landing just in case.

I have just checked, UIKit does not use ContainerNode::childNode().
Comment 8 Chris Dumez 2014-09-16 17:07:32 PDT
Another proposal would be findChildAt(index). I still prefer traverseToChildAt(index) though.
Comment 9 Chris Dumez 2014-09-16 17:51:04 PDT
Created attachment 238230 [details]
Patch
Comment 10 Darin Adler 2014-09-16 18:24:54 PDT
Comment on attachment 238230 [details]
Patch

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

> Source/WebCore/dom/ContainerNode.cpp:945
> +    for (; child && index > 0; --index)

It’d be pretty standard WebKit style to just say "child && index" instead of "child && index > 0".

> Source/WebCore/dom/Position.cpp:249
> -        return m_anchorNode->childNode(m_offset - 1); // -1 converts to childNode((unsigned)-1) and returns null.
> +        return m_offset ? m_anchorNode->traverseToChildAt(m_offset - 1) : nullptr;

Wow, that 0 case could have been really slow before!

> Source/WebCore/inspector/DOMPatchSupport.cpp:381
> +    for (unsigned i = 0; node && i < newMap.size(); ++i, node = node->nextSibling()) {

A little strange to use unsigned here now when it was size_t before.
Comment 11 WebKit Commit Bot 2014-09-16 19:00:46 PDT
Comment on attachment 238230 [details]
Patch

Clearing flags on attachment: 238230

Committed r173684: <http://trac.webkit.org/changeset/173684>
Comment 12 WebKit Commit Bot 2014-09-16 19:00:51 PDT
All reviewed patches have been landed.  Closing bug.