Skip to content

Commit

Permalink
Simplify SVGPropertyOwnerRegistry slightly
Browse files Browse the repository at this point in the history
https://bugs.webkit.org/show_bug.cgi?id=240839

Reviewed by Yusuke Suzuki.

SVGPropertyOwnerRegistry::findAccessor() had to loop through the hash table entries
because it wanted the behavior of QualifiedName::matches(), which is different from
QualifiedName::operator==. However, we can achieve this by using hash traits, and
the appropriate traits already exist in the form of SVGAttributeHashTranslator.

* Source/WebCore/svg/SVGElementInlines.h:
(WebCore::SVGAttributeHashTranslator::hash): Deleted.
(WebCore::SVGAttributeHashTranslator::equal): Deleted.
* Source/WebCore/svg/properties/SVGPropertyOwnerRegistry.h:
(WebCore::SVGAttributeHashTranslator::hash):
(WebCore::SVGAttributeHashTranslator::equal):
(WebCore::SVGPropertyOwnerRegistry::attributeNameToAccessorMap):
(WebCore::SVGPropertyOwnerRegistry::findAccessor):

Canonical link: https://commits.webkit.org/250948@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@294789 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
smfr committed May 25, 2022
1 parent 1768e5e commit a867a9a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
12 changes: 0 additions & 12 deletions Source/WebCore/svg/SVGElementInlines.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,6 @@ inline void SVGElement::setPresentationalHintStyleIsDirty()
invalidateStyle();
}

struct SVGAttributeHashTranslator {
static unsigned hash(const QualifiedName& key)
{
if (key.hasPrefix()) {
QualifiedNameComponents components = { nullAtom().impl(), key.localName().impl(), key.namespaceURI().impl() };
return computeHash(components);
}
return DefaultHash<QualifiedName>::hash(key);
}
static bool equal(const QualifiedName& a, const QualifiedName& b) { return a.matches(b); }
};

inline bool Element::hasTagName(const SVGQualifiedName& tagName) const
{
return ContainerNode::hasTagName(tagName);
Expand Down
28 changes: 20 additions & 8 deletions Source/WebCore/svg/properties/SVGPropertyOwnerRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,21 @@ namespace WebCore {

class SVGAttributeAnimator;

struct SVGAttributeHashTranslator {
static unsigned hash(const QualifiedName& key)
{
if (key.hasPrefix()) {
QualifiedNameComponents components = { nullAtom().impl(), key.localName().impl(), key.namespaceURI().impl() };
return computeHash(components);
}
return DefaultHash<QualifiedName>::hash(key);
}
static bool equal(const QualifiedName& a, const QualifiedName& b) { return a.matches(b); }

static constexpr bool safeToCompareToEmptyOrDeleted = false;
static constexpr bool hasHashInValue = true;
};

template<typename OwnerType, typename... BaseTypes>
class SVGPropertyOwnerRegistry : public SVGPropertyRegistry {
public:
Expand Down Expand Up @@ -300,9 +315,11 @@ class SVGPropertyOwnerRegistry : public SVGPropertyRegistry {

private:
// Singleton map for every OwnerType.
static HashMap<QualifiedName, const SVGMemberAccessor<OwnerType>*>& attributeNameToAccessorMap()
using QualifiedNameAccessorHashMap = HashMap<QualifiedName, const SVGMemberAccessor<OwnerType>*, SVGAttributeHashTranslator>;

static QualifiedNameAccessorHashMap& attributeNameToAccessorMap()
{
static NeverDestroyed<HashMap<QualifiedName, const SVGMemberAccessor<OwnerType>*>> attributeNameToAccessorMap;
static NeverDestroyed<QualifiedNameAccessorHashMap> attributeNameToAccessorMap;
return attributeNameToAccessorMap;
}

Expand Down Expand Up @@ -331,12 +348,7 @@ class SVGPropertyOwnerRegistry : public SVGPropertyRegistry {

static const SVGMemberAccessor<OwnerType>* findAccessor(const QualifiedName& attributeName)
{
// Here we need to loop through the entries in the map and use matches() to compare them with attributeName.
// m_map.contains() uses QualifiedName::operator==() which compares the impl pointers only while matches()
// compares the contents if the impl pointers differ.
auto it = std::find_if(attributeNameToAccessorMap().begin(), attributeNameToAccessorMap().end(), [&attributeName](const auto& entry) -> bool {
return entry.key.matches(attributeName);
});
auto it = attributeNameToAccessorMap().find(attributeName);
return it != attributeNameToAccessorMap().end() ? it->value : nullptr;
}

Expand Down

0 comments on commit a867a9a

Please sign in to comment.