Commit 130931a9 authored by kochi's avatar kochi Committed by Commit bot

Avoid redundant hash calculations in CustomElementUpgradeSorter

Pointed out by tkent in https://codereview.chromium.org/2242743002/

The original code could do 2 hash calculations for
adding a new key-value pair to ParentChildMap
(for find() and add()), and rehashing the map
may involve copying the HeapHashSet<Member<Node>>
objects.

Change the value part of ParentChildMap from a HeapHashMap
object to its pointer, to avoid redundant hash calculation
and object copy.

This does not change any functionality of the class.

BUG=594918

Review-Url: https://codereview.chromium.org/2290543002
Cr-Commit-Position: refs/heads/master@{#415251}
parent df268d9d
......@@ -31,15 +31,16 @@ CustomElementUpgradeSorter::AddResult CustomElementUpgradeSorter::addToParentChi
Node* parent,
Node* child)
{
ParentChildMap::iterator it = m_parentChildMap->find(parent);
if (it != m_parentChildMap->end()) {
it->value.add(child);
ParentChildMap::AddResult result = m_parentChildMap->add(parent, nullptr);
if (!result.isNewEntry) {
result.storedValue->value->add(child);
// The entry for the parent exists; so must its parents.
return kParentAlreadyExistsInMap;
}
ParentChildMap::AddResult result =
m_parentChildMap->add(parent, HeapHashSet<Member<Node>>());
result.storedValue->value.add(child);
ChildSet* childSet = new ChildSet();
childSet->add(child);
result.storedValue->value = childSet;
return kParentAddedToMap;
}
......@@ -86,10 +87,10 @@ void CustomElementUpgradeSorter::sorted(
if (childrenIterator == m_parentChildMap->end())
return;
ChildSet& children = childrenIterator->value;
ChildSet* children = childrenIterator->value.get();
if (children.size() == 1) {
visit(result, children, children.begin());
if (children->size() == 1) {
visit(result, *children, children->begin());
return;
}
......@@ -99,19 +100,19 @@ void CustomElementUpgradeSorter::sorted(
? toElement(parent)->authorShadowRoot()
: nullptr;
if (shadowRoot)
visit(result, children, children.find(shadowRoot));
visit(result, *children, children->find(shadowRoot));
for (Element* e = ElementTraversal::firstChild(*parent);
e && children.size() > 1;
e && children->size() > 1;
e = ElementTraversal::nextSibling(*e)) {
visit(result, children, children.find(e));
visit(result, *children, children->find(e));
}
if (children.size() == 1)
visit(result, children, children.begin());
if (children->size() == 1)
visit(result, *children, children->begin());
DCHECK(children.isEmpty());
DCHECK(children->isEmpty());
}
} // namespace blink
......@@ -32,7 +32,7 @@ public:
private:
using ChildSet = HeapHashSet<Member<Node>>;
using ParentChildMap = HeapHashMap<Member<Node>, ChildSet>;
using ParentChildMap = HeapHashMap<Member<Node>, Member<ChildSet>>;
enum AddResult {
kParentAlreadyExistsInMap,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment