Rename Node::childNodeCount() to countChildNodes() and avoid inefficient uses
authorcdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 14 Sep 2014 21:18:27 +0000 (21:18 +0000)
committercdumez@apple.com <cdumez@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Sun, 14 Sep 2014 21:18:27 +0000 (21:18 +0000)
commit4a5c378deb62c311e5dafc16bee2c94a4ca6419c
treeb7c4dfe507c3992c25d07cf2506256b0cc3bbf6d
parent5a0fd85442eeba656be9c34c04e425b32c47c887
Rename Node::childNodeCount() to countChildNodes() and avoid inefficient uses
https://bugs.webkit.org/show_bug.cgi?id=136789

Reviewed by Darin Adler.

Source/WebCore:

Rename Node::childNodeCount() to countChildNodes() to make it clearer
that the method actually computes the result rather than returning a
cached value.

This patch also introduces a new Node::hasOneChild() method that is
used to check if a Node has a single child. This is much more efficient
than calling countChildNodes() == 1.

The patch also leverages Node::hasChildNodes() in a lot of places
instead of calling countChildNodes().

Finally, in a couple of places, we now use childrenOfType() to iterate
over children more efficient than using countChildNodes() and
childNode(index).

No new tests, no behavior change.

* WebCore.exp.in:
* WebCore.order:
Update the name of the exposed symbol for countChildNodes().

* dom/ContainerNode.cpp:
(WebCore::ContainerNode::removeChildren):
(WebCore::ContainerNode::countChildNodes):
(WebCore::ContainerNode::childNodeCount): Deleted.
Rename childNodeCount() to countChildNodes() to make it obvious it is
computing the result rather than returning a cached value.

* dom/ContainerNode.h:
(WebCore::ContainerNode::hasOneChild):
Introduce an efficient way to check in a ContainerNode has a single
child as "countChildNodes() == 1" calls were frequent and inefficient.

(WebCore::Node::countChildNodes):
(WebCore::Node::childNodeCount): Deleted.
* dom/Node.h:
Rename childNodeCount() to countChildNodes().

* dom/Position.cpp:
(WebCore::Position::parentAnchoredEquivalent):
* dom/Position.h:
(WebCore::lastOffsetInNode):
* dom/Range.cpp:
Mechanical renaming.

(WebCore::lengthOfContentsInNode):
* editing/ApplyStyleCommand.cpp:
(WebCore::ApplyStyleCommand::applyInlineStyleToNodeRange):
(WebCore::ApplyStyleCommand::shouldApplyInlineStyleToRun):
(WebCore::ApplyStyleCommand::applyInlineStyleToPushDown):
(WebCore::ApplyStyleCommand::mergeEndWithNextIfIdentical):
(WebCore::ApplyStyleCommand::applyInlineStyleChange):
Call hasChildNodes() rather than countChildNodes() as it is a lot more
efficient.

* editing/Editor.cpp:
(WebCore::Editor::setTextAsChildOfElement):
(WebCore::Editor::rangeOfString):
(WebCore::Editor::countMatchesForText):
* editing/FrameSelection.cpp:
(WebCore::FrameSelection::elementRangeContainingCaretSelection):
Mechanical renaming.

* editing/ReplaceSelectionCommand.cpp:
(WebCore::ReplaceSelectionCommand::insertAsListItems):
Call hasOneChild() instead of countChildNodes() == 1. Also remove
redundant listElement->hasChildNodes() check as hasOneChild() takes
care of this already.

* editing/TextIterator.cpp:
(WebCore::SimplifiedBackwardsTextIterator::SimplifiedBackwardsTextIterator):
* editing/VisibleUnits.cpp:
(WebCore::endOfDocument):
* editing/htmlediting.cpp:
(WebCore::lastOffsetForEditing):
Mechanical renaming.

(WebCore::visiblePositionBeforeNode):
(WebCore::visiblePositionAfterNode):
Call hasChildNodes() instead of converting countChildNodes() to a
boolean.

* editing/markup.cpp:
(WebCore::StyledMarkupAccumulator::traverseNodesForSerialization):
Call !hasChildNodes() instead of !countChildNodes().

(WebCore::isPlainTextMarkup):
- Drop !node->isElementNode() check as !node->hasTagName(divTag) takes
  care of discarding non-Element Nodes already.
- Cast the Node to an HTMLDivElement as soon as possible to avoid
  calling calling the slower Node APIs in several cases.
- Call hasOneChild() instead of countChildNodes() == 1.

* html/HTMLDivElement.h:
* html/HTMLTagNames.in:
Generate casting helpers as I use them in WebCore::isPlainTextMarkup().

* html/HTMLScriptElement.cpp:
(WebCore::HTMLScriptElement::setText):
* html/HTMLTitleElement.cpp:
(WebCore::HTMLTitleElement::setText):
Call hasOneChild() / hasChildNodes() instead of countChildNodes().

* html/shadow/MediaControlElements.cpp:
(WebCore::MediaControlTimelineContainerElement::setTimeDisplaysHidden):
(WebCore::MediaControlTextTrackContainerElement::updateDisplay):
Use childrenOfType<Element>() to iterate of child Elements instead of
slower childNodeCount() + childNode(index) + isElementNode(). Also,
childNodeCount() was not even cached before the loop.

* html/track/VTTRegion.cpp:
(WebCore::VTTRegion::displayLastTextTrackCueBox):
Use childrenOfType<Element>() to iterate of child Elements, thus
avoiding calling childNodeCount() + childNode(index).

* page/DOMSelection.cpp:
(WebCore::DOMSelection::extend):
(WebCore::DOMSelection::selectAllChildren):
* rendering/RenderObject.cpp:
(WebCore::RenderObject::caretMaxOffset):
* rendering/RenderReplaced.cpp:
(WebCore::RenderReplaced::isSelected):
* rendering/RenderView.cpp:
(WebCore::RenderView::splitSelectionBetweenSubtrees):
Mechanical renaming.

Source/WebKit/mac:

Rename childNodeCount() to countChildNodes().

* WebView/WebHTMLView.mm:
(-[WebHTMLView attributedString]):

Source/WebKit2:

Avoid calling slow Node::countChildNodes().

* WebProcess/WebPage/CoordinatedGraphics/WebPageCoordinatedGraphics.cpp:
(WebKit::WebPage::findZoomableAreaForPoint):
Replace call to "node->parentNode()->childNodeCount() != 1" by
"!node->parentNode()->hasOneChild()" which is equivalent but more
efficient.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@173606 268f45cc-cd09-0410-ab3c-d52691b4dbfc
31 files changed:
Source/WebCore/ChangeLog
Source/WebCore/WebCore.exp.in
Source/WebCore/WebCore.order
Source/WebCore/dom/ContainerNode.cpp
Source/WebCore/dom/ContainerNode.h
Source/WebCore/dom/Node.h
Source/WebCore/dom/Position.cpp
Source/WebCore/dom/Position.h
Source/WebCore/dom/Range.cpp
Source/WebCore/editing/ApplyStyleCommand.cpp
Source/WebCore/editing/Editor.cpp
Source/WebCore/editing/FrameSelection.cpp
Source/WebCore/editing/ReplaceSelectionCommand.cpp
Source/WebCore/editing/TextIterator.cpp
Source/WebCore/editing/VisibleUnits.cpp
Source/WebCore/editing/htmlediting.cpp
Source/WebCore/editing/markup.cpp
Source/WebCore/html/HTMLDivElement.h
Source/WebCore/html/HTMLScriptElement.cpp
Source/WebCore/html/HTMLTagNames.in
Source/WebCore/html/HTMLTitleElement.cpp
Source/WebCore/html/shadow/MediaControlElements.cpp
Source/WebCore/html/track/VTTRegion.cpp
Source/WebCore/page/DOMSelection.cpp
Source/WebCore/rendering/RenderObject.cpp
Source/WebCore/rendering/RenderReplaced.cpp
Source/WebCore/rendering/RenderView.cpp
Source/WebKit/mac/ChangeLog
Source/WebKit/mac/WebView/WebHTMLView.mm
Source/WebKit2/ChangeLog
Source/WebKit2/WebProcess/WebPage/CoordinatedGraphics/WebPageCoordinatedGraphics.cpp