From 94d41e8d17b66606cc7a731eee115003330d9af9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Emilio=20Cobos=20=C3=81lvarez?= Date: Wed, 8 Feb 2023 00:55:18 +0000 Subject: [PATCH] Bug 1815558 - Tweak nsAttrValue::Equals(nsAtom*). r=smaug To avoid an unconditional string copy when comparing case-insensitively (by using nsDependentAtomString). Since the string path is not much different than the generic code-path, just use that elsewhere. This shows up in selector matching. Differential Revision: https://phabricator.services.mozilla.com/D169142 --- dom/base/nsAttrValue.cpp | 47 +++++++++++++++------------------------- 1 file changed, 17 insertions(+), 30 deletions(-) diff --git a/dom/base/nsAttrValue.cpp b/dom/base/nsAttrValue.cpp index 11e4e72cc874..597b6223ed6e 100644 --- a/dom/base/nsAttrValue.cpp +++ b/dom/base/nsAttrValue.cpp @@ -960,8 +960,7 @@ bool nsAttrValue::Equals(const nsAString& aValue, nsCaseTreatment aCaseSensitive) const { switch (BaseType()) { case eStringBase: { - nsStringBuffer* str = static_cast(GetPtr()); - if (str) { + if (auto* str = static_cast(GetPtr())) { nsDependentString dep(static_cast(str->Data()), str->StorageSize() / sizeof(char16_t) - 1); return aCaseSensitive == eCaseMatters @@ -970,12 +969,14 @@ bool nsAttrValue::Equals(const nsAString& aValue, } return aValue.IsEmpty(); } - case eAtomBase: + case eAtomBase: { + auto* atom = static_cast(GetPtr()); if (aCaseSensitive == eCaseMatters) { - return static_cast(GetPtr())->Equals(aValue); + return atom->Equals(aValue); } - return nsContentUtils::EqualsIgnoreASCIICase( - nsDependentAtomString(static_cast(GetPtr())), aValue); + return nsContentUtils::EqualsIgnoreASCIICase(nsDependentAtomString(atom), + aValue); + } default: break; } @@ -989,33 +990,19 @@ bool nsAttrValue::Equals(const nsAString& aValue, bool nsAttrValue::Equals(const nsAtom* aValue, nsCaseTreatment aCaseSensitive) const { - if (aCaseSensitive != eCaseMatters) { - // Need a better way to handle this! - nsAutoString value; - aValue->ToString(value); - return Equals(value, aCaseSensitive); - } - - switch (BaseType()) { - case eStringBase: { - nsStringBuffer* str = static_cast(GetPtr()); - if (str) { - nsDependentString dep(static_cast(str->Data()), - str->StorageSize() / sizeof(char16_t) - 1); - return aValue->Equals(dep); - } - return aValue == nsGkAtoms::_empty; + if (BaseType() == eAtomBase) { + auto* atom = static_cast(GetPtr()); + if (atom == aValue) { + return true; } - case eAtomBase: { - return static_cast(GetPtr()) == aValue; + if (aCaseSensitive == eCaseMatters) { + return false; + } + if (atom->IsAsciiLowercase() && aValue->IsAsciiLowercase()) { + return false; } - default: - break; } - - nsAutoString val; - ToString(val); - return aValue->Equals(val); + return Equals(nsDependentAtomString(aValue), aCaseSensitive); } struct HasPrefixFn {