Skip to content

Commit

Permalink
Use CheckedPtr<Element> / CheckedRef<Element> less for non-stack vari…
Browse files Browse the repository at this point in the history
…ables

https://bugs.webkit.org/show_bug.cgi?id=266286

Reviewed by Ryosuke Niwa.

Use CheckedPtr<Element> / CheckedRef<Element> less for non-stack variables
since it introduces crashes that are not actionable.

Use WeakRef<Element> / WeakPtr<Element> instead.

This tested as performance neutral on Speedometer 2 & 3.

* Source/WebCore/dom/Attr.h:
* Source/WebCore/dom/DatasetDOMStringMap.h:
* Source/WebCore/dom/Document.cpp:
(WebCore::findNearestCommonComposedAncestor):
(WebCore::Document::registerArticleElement):
(WebCore::Document::unregisterArticleElement):
(WebCore::Document::updateMainArticleElementAfterLayout):
* Source/WebCore/dom/Document.h:
* Source/WebCore/dom/Element.cpp:
(WebCore::Element::addShadowRoot):
* Source/WebCore/dom/NamedNodeMap.h:
* Source/WebCore/dom/RadioButtonGroups.cpp:
* Source/WebCore/dom/ScriptElement.h:
* Source/WebCore/dom/ShadowRoot.cpp:
* Source/WebCore/dom/ShadowRoot.h:
* Source/WebCore/dom/SimulatedClick.cpp:
(WebCore::simulateClick):
* Source/WebCore/html/HTMLCollection.h:
* Source/WebCore/html/HTMLCollectionInlines.h:
(WebCore::CollectionNamedElementCache::findElementsWithId const):
(WebCore::CollectionNamedElementCache::findElementsWithName const):
(WebCore::CollectionNamedElementCache::find const):
(WebCore::CollectionNamedElementCache::append):

Canonical link: https://commits.webkit.org/271949@main
  • Loading branch information
cdumez committed Dec 12, 2023
1 parent d3e0a4b commit 12e1fe4
Show file tree
Hide file tree
Showing 13 changed files with 28 additions and 27 deletions.
2 changes: 1 addition & 1 deletion Source/WebCore/dom/Attr.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class Attr final : public Node {

// Attr wraps either an element/name, or a name/value pair (when it's a standalone Node.)
// Note that m_name is always set, but m_element/m_standaloneValue may be null.
CheckedPtr<Element> m_element;
WeakPtr<Element, WeakPtrImplWithEventTargetData> m_element;
QualifiedName m_name;
AtomString m_standaloneValue;

Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/dom/DatasetDOMStringMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

#include "ExceptionOr.h"
#include "ScriptWrappable.h"
#include <wtf/CheckedRef.h>
#include <wtf/WeakRef.h>

namespace WebCore {

Expand Down Expand Up @@ -57,7 +57,7 @@ class DatasetDOMStringMap final : public ScriptWrappable {
private:
const AtomString* item(const String& name) const;

CheckedRef<Element> m_element;
WeakRef<Element, WeakPtrImplWithEventTargetData> m_element;
};

} // namespace WebCore
8 changes: 4 additions & 4 deletions Source/WebCore/dom/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7954,7 +7954,7 @@ static Element* findNearestCommonComposedAncestor(Element* elementA, Element* el
if (elementA == elementB)
return elementA;

HashSet<CheckedRef<Element>> ancestorChain;
HashSet<Ref<Element>> ancestorChain;
for (auto* element = elementA; element; element = element->parentElementInComposedTree())
ancestorChain.add(*element);

Expand Down Expand Up @@ -8841,12 +8841,12 @@ std::optional<FrameIdentifier> Document::frameID() const

void Document::registerArticleElement(Element& article)
{
m_articleElements.add(&article);
m_articleElements.add(article);
}

void Document::unregisterArticleElement(Element& article)
{
m_articleElements.remove(&article);
m_articleElements.remove(article);
if (m_mainArticleElement == &article)
m_mainArticleElement = nullptr;
}
Expand Down Expand Up @@ -8882,7 +8882,7 @@ void Document::updateMainArticleElementAfterLayout()
secondTallestArticleHeight = tallestArticleHeight;
tallestArticleHeight = height;
tallestArticleWidth = box ? box->width().toFloat() : 0;
tallestArticle = article.get();
tallestArticle = article.ptr();
} else if (height >= secondTallestArticleHeight)
secondTallestArticleHeight = height;
}
Expand Down
6 changes: 3 additions & 3 deletions Source/WebCore/dom/Document.h
Original file line number Diff line number Diff line change
Expand Up @@ -2081,7 +2081,7 @@ class Document

std::unique_ptr<Style::Update> m_pendingRenderTreeUpdate;

CheckedPtr<Element> m_cssTarget;
WeakPtr<Element, WeakPtrImplWithEventTargetData> m_cssTarget;

std::unique_ptr<LazyLoadImageObserver> m_lazyLoadImageObserver;

Expand Down Expand Up @@ -2139,8 +2139,8 @@ class Document
WeakPtr<HTMLMediaElement, WeakPtrImplWithEventTargetData> m_mediaElementShowingTextTrack;
#endif

CheckedPtr<Element> m_mainArticleElement;
HashSet<CheckedPtr<Element>> m_articleElements;
WeakPtr<Element, WeakPtrImplWithEventTargetData> m_mainArticleElement;
HashSet<WeakRef<Element, WeakPtrImplWithEventTargetData>> m_articleElements;

WeakHashSet<VisibilityChangeClient> m_visibilityStateCallbackClients;

Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/dom/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2896,7 +2896,7 @@ void Element::addShadowRoot(Ref<ShadowRoot>&& newShadowRoot)

ensureElementRareData().setShadowRoot(WTFMove(newShadowRoot));

shadowRoot->setHost(this);
shadowRoot->setHost(*this);
shadowRoot->setParentTreeScope(treeScope());

NodeVector postInsertionNotificationTargets;
Expand Down
3 changes: 2 additions & 1 deletion Source/WebCore/dom/NamedNodeMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include "Element.h"
#include "ExceptionOr.h"
#include "ScriptWrappable.h"
#include <wtf/WeakRef.h>

namespace WebCore {

Expand Down Expand Up @@ -59,7 +60,7 @@ class NamedNodeMap final : public ScriptWrappable {
Ref<Element> protectedElement() const;

private:
CheckedRef<Element> m_element;
WeakRef<Element, WeakPtrImplWithEventTargetData> m_element;
};

} // namespace WebCore
4 changes: 2 additions & 2 deletions Source/WebCore/dom/RadioButtonGroups.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,8 @@ class RadioButtonGroup {
bool isValid() const;
void setCheckedButton(HTMLInputElement*);

HashSet<CheckedRef<HTMLInputElement>> m_members;
CheckedPtr<HTMLInputElement> m_checkedButton;
HashSet<WeakRef<HTMLInputElement, WeakPtrImplWithEventTargetData>> m_members;
WeakPtr<HTMLInputElement, WeakPtrImplWithEventTargetData> m_checkedButton;
size_t m_requiredCount { 0 };
};

Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/dom/ScriptElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ class ScriptElement {

virtual bool isScriptPreventedByAttributes() const { return false; }

CheckedRef<Element> m_element;
WeakRef<Element, WeakPtrImplWithEventTargetData> m_element;
OrdinalNumber m_startLineNumber { OrdinalNumber::beforeFirst() };
JSC::SourceTaintedOrigin m_taintedOrigin;
ParserInserted m_parserInserted : bitWidthOfParserInserted;
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/dom/ShadowRoot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ WTF_MAKE_ISO_ALLOCATED_IMPL(ShadowRoot);

struct SameSizeAsShadowRoot : public DocumentFragment, public TreeScope {
uint8_t flagsAndModes[3];
CheckedPtr<Element> host;
WeakPtr<Element, WeakPtrImplWithEventTargetData> host;
void* styleSheetList;
void* styleScope;
void* slotAssignment;
Expand Down
4 changes: 2 additions & 2 deletions Source/WebCore/dom/ShadowRoot.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class ShadowRoot final : public DocumentFragment, public TreeScope {

Element* host() const { return m_host.get(); }
RefPtr<Element> protectedHost() const { return m_host.get(); }
void setHost(CheckedPtr<Element>&& host) { m_host = WTFMove(host); }
void setHost(WeakPtr<Element, WeakPtrImplWithEventTargetData>&& host) { m_host = WTFMove(host); }

ExceptionOr<void> setHTMLUnsafe(const String&);

Expand Down Expand Up @@ -168,7 +168,7 @@ class ShadowRoot final : public DocumentFragment, public TreeScope {
ShadowRootMode m_mode { ShadowRootMode::UserAgent };
SlotAssignmentMode m_slotAssignmentMode { SlotAssignmentMode::Named };

CheckedPtr<Element> m_host;
WeakPtr<Element, WeakPtrImplWithEventTargetData> m_host;
RefPtr<StyleSheetList> m_styleSheetList;

std::unique_ptr<Style::Scope> m_styleScope;
Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/dom/SimulatedClick.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ bool simulateClick(Element& element, Event* underlyingEvent, SimulatedClickMouse
if (element.isDisabledFormControl())
return false;

static MainThreadNeverDestroyed<HashSet<CheckedRef<Element>>> elementsDispatchingSimulatedClicks;
static MainThreadNeverDestroyed<HashSet<Ref<Element>>> elementsDispatchingSimulatedClicks;
if (!elementsDispatchingSimulatedClicks.get().add(element).isNewEntry)
return false;

Expand Down
8 changes: 4 additions & 4 deletions Source/WebCore/html/HTMLCollection.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ namespace WebCore {
class CollectionNamedElementCache {
WTF_MAKE_FAST_ALLOCATED;
public:
inline const Vector<CheckedRef<Element>>* findElementsWithId(const AtomString& id) const;
inline const Vector<CheckedRef<Element>>* findElementsWithName(const AtomString& name) const;
inline const Vector<WeakRef<Element, WeakPtrImplWithEventTargetData>>* findElementsWithId(const AtomString& id) const;
inline const Vector<WeakRef<Element, WeakPtrImplWithEventTargetData>>* findElementsWithName(const AtomString& name) const;
const Vector<AtomString>& propertyNames() const { return m_propertyNames; }

inline void appendToIdCache(const AtomString& id, Element&);
Expand All @@ -44,9 +44,9 @@ class CollectionNamedElementCache {
inline size_t memoryCost() const;

private:
typedef HashMap<AtomStringImpl*, Vector<CheckedRef<Element>>> StringToElementsMap;
typedef HashMap<AtomStringImpl*, Vector<WeakRef<Element, WeakPtrImplWithEventTargetData>>> StringToElementsMap;

inline const Vector<CheckedRef<Element>>* find(const StringToElementsMap&, const AtomString& key) const;
inline const Vector<WeakRef<Element, WeakPtrImplWithEventTargetData>>* find(const StringToElementsMap&, const AtomString& key) const;
inline void append(StringToElementsMap&, const AtomString& key, Element&);

StringToElementsMap m_idMap;
Expand Down
8 changes: 4 additions & 4 deletions Source/WebCore/html/HTMLCollectionInlines.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ inline ContainerNode& HTMLCollection::rootNode() const
return ownerNode();
}

inline const Vector<CheckedRef<Element>>* CollectionNamedElementCache::findElementsWithId(const AtomString& id) const
inline const Vector<WeakRef<Element, WeakPtrImplWithEventTargetData>>* CollectionNamedElementCache::findElementsWithId(const AtomString& id) const
{
return find(m_idMap, id);
}

inline const Vector<CheckedRef<Element>>* CollectionNamedElementCache::findElementsWithName(const AtomString& name) const
inline const Vector<WeakRef<Element, WeakPtrImplWithEventTargetData>>* CollectionNamedElementCache::findElementsWithName(const AtomString& name) const
{
return find(m_nameMap, name);
}
Expand All @@ -66,7 +66,7 @@ inline void CollectionNamedElementCache::didPopulate()
reportExtraMemoryAllocatedForCollectionIndexCache(cost);
}

inline const Vector<CheckedRef<Element>>* CollectionNamedElementCache::find(const StringToElementsMap& map, const AtomString& key) const
inline const Vector<WeakRef<Element, WeakPtrImplWithEventTargetData>>* CollectionNamedElementCache::find(const StringToElementsMap& map, const AtomString& key) const
{
ASSERT(m_didPopulate);
auto it = map.find(key.impl());
Expand All @@ -77,7 +77,7 @@ inline void CollectionNamedElementCache::append(StringToElementsMap& map, const
{
if (!m_idMap.contains(key.impl()) && !m_nameMap.contains(key.impl()))
m_propertyNames.append(key);
map.add(key.impl(), Vector<CheckedRef<Element>>()).iterator->value.append(element);
map.add(key.impl(), Vector<WeakRef<Element, WeakPtrImplWithEventTargetData>>()).iterator->value.append(element);
}

inline bool HTMLCollection::isRootedAtTreeScope() const
Expand Down

0 comments on commit 12e1fe4

Please sign in to comment.