Skip to content
Permalink
Browse files
Leverage the known length of an ASCIILiteral when comparing it to a S…
…tring / AtomString

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

Reviewed by Yusuke Suzuki and Darin Adler.

Leverage the known length of an ASCIILiteral when comparing it to a String / AtomString.
Make sure we call ASCIILiteral::length() inline to allow the compiler to optimize out
the strlen() call, then call the more efficient `equal(StringImpl*, const LChar*, unsigned)`
instead of `equal(StringImpl*, const LChar*)`.

* Source/JavaScriptCore/jsc.cpp:
(printInternal):
* Source/JavaScriptCore/runtime/JSCJSValueInlines.h:
(JSC::toPreferredPrimitiveType):
* Source/JavaScriptCore/runtime/PropertyName.h:
(JSC::isCanonicalNumericIndexString):
* Source/WTF/wtf/text/AtomString.h:
(WTF::operator==):
* Source/WTF/wtf/text/StringImpl.h:
(WTF::equal):
* Source/WTF/wtf/text/WTFString.h:
(WTF::operator==):
(WTF::operator!=):

Canonical link: https://commits.webkit.org/250097@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@293588 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
cdumez committed Apr 28, 2022
1 parent df716c2 commit a4413984664e87a814fd5c668d608094632abebf
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 10 deletions.
@@ -1278,7 +1278,7 @@ static EncodedJSValue printInternal(JSGlobalObject* globalObject, CallFrame* cal

if (asyncTestExpectedPasses) {
JSValue value = callFrame->argument(0);
if (value.isString() && WTF::equal(asString(value)->value(globalObject).impl(), "Test262:AsyncTestComplete")) {
if (value.isString() && WTF::equal(asString(value)->value(globalObject).impl(), "Test262:AsyncTestComplete"_s)) {
asyncTestPasses++;
return JSValue::encode(jsUndefined());
}
@@ -842,11 +842,11 @@ inline PreferredPrimitiveType toPreferredPrimitiveType(JSGlobalObject* globalObj
String hintString = asString(value)->value(globalObject);
RETURN_IF_EXCEPTION(scope, NoPreference);

if (WTF::equal(hintString, "default"))
if (WTF::equal(hintString, "default"_s))
return NoPreference;
if (WTF::equal(hintString, "number"))
if (WTF::equal(hintString, "number"_s))
return PreferNumber;
if (WTF::equal(hintString, "string"))
if (WTF::equal(hintString, "string"_s))
return PreferString;

throwTypeError(globalObject, scope, "Expected primitive hint to match one of 'default', 'number', 'string'."_s);
@@ -139,7 +139,7 @@ ALWAYS_INLINE bool isCanonicalNumericIndexString(const PropertyName& propertyNam
return false;
if (property->isSymbol())
return false;
if (equal(property, "-0"))
if (equal(property, "-0"_s))
return true;
double index = jsToNumber(property);
NumberToStringBuffer buffer;
@@ -171,7 +171,7 @@ static_assert(sizeof(AtomString) == sizeof(String), "AtomString and String must
inline bool operator==(const AtomString& a, const AtomString& b) { return a.impl() == b.impl(); }
bool operator==(const AtomString&, const LChar*);
inline bool operator==(const AtomString& a, const char* b) { return WTF::equal(a.impl(), reinterpret_cast<const LChar*>(b)); }
inline bool operator==(const AtomString& a, ASCIILiteral b) { return WTF::equal(a.impl(), reinterpret_cast<const LChar*>(b.characters())); }
inline bool operator==(const AtomString& a, ASCIILiteral b) { return WTF::equal(a.impl(), b); }
inline bool operator==(const AtomString& a, const Vector<UChar>& b) { return a.impl() && equal(a.impl(), b.data(), b.size()); }
inline bool operator==(const AtomString& a, const String& b) { return equal(a.impl(), b.impl()); }
inline bool operator==(const LChar* a, const AtomString& b) { return b == a; }
@@ -570,6 +570,7 @@ WTF_EXPORT_PRIVATE bool equal(const StringImpl*, const LChar*);
inline bool equal(const StringImpl* a, const char* b) { return equal(a, reinterpret_cast<const LChar*>(b)); }
WTF_EXPORT_PRIVATE bool equal(const StringImpl*, const LChar*, unsigned);
WTF_EXPORT_PRIVATE bool equal(const StringImpl*, const UChar*, unsigned);
ALWAYS_INLINE bool equal(const StringImpl* a, ASCIILiteral b) { return equal(a, b.characters8(), b.length()); }
inline bool equal(const StringImpl* a, const char* b, unsigned length) { return equal(a, reinterpret_cast<const LChar*>(b), length); }
inline bool equal(const LChar* a, StringImpl* b) { return equal(b, a); }
inline bool equal(const char* a, StringImpl* b) { return equal(b, reinterpret_cast<const LChar*>(a)); }
@@ -341,20 +341,20 @@ static_assert(sizeof(String) == sizeof(void*), "String should effectively be a p
inline bool operator==(const String& a, const String& b) { return equal(a.impl(), b.impl()); }
inline bool operator==(const String& a, const LChar* b) { return equal(a.impl(), b); }
inline bool operator==(const String& a, const char* b) { return equal(a.impl(), reinterpret_cast<const LChar*>(b)); }
inline bool operator==(const String& a, ASCIILiteral b) { return equal(a.impl(), reinterpret_cast<const LChar*>(b.characters())); }
inline bool operator==(const String& a, ASCIILiteral b) { return equal(a.impl(), b); }
inline bool operator==(const LChar* a, const String& b) { return equal(a, b.impl()); }
inline bool operator==(const char* a, const String& b) { return equal(reinterpret_cast<const LChar*>(a), b.impl()); }
inline bool operator==(ASCIILiteral a, const String& b) { return equal(reinterpret_cast<const LChar*>(a.characters()), b.impl()); }
inline bool operator==(ASCIILiteral a, const String& b) { return equal(b.impl(), a); }
template<size_t inlineCapacity> inline bool operator==(const Vector<char, inlineCapacity>& a, const String& b) { return equal(b.impl(), a.data(), a.size()); }
template<size_t inlineCapacity> inline bool operator==(const String& a, const Vector<char, inlineCapacity>& b) { return b == a; }

inline bool operator!=(const String& a, const String& b) { return !equal(a.impl(), b.impl()); }
inline bool operator!=(const String& a, const LChar* b) { return !equal(a.impl(), b); }
inline bool operator!=(const String& a, const char* b) { return !equal(a.impl(), reinterpret_cast<const LChar*>(b)); }
inline bool operator!=(const String& a, ASCIILiteral b) { return !equal(a.impl(), reinterpret_cast<const LChar*>(b.characters())); }
inline bool operator!=(const String& a, ASCIILiteral b) { return !equal(a.impl(), b); }
inline bool operator!=(const LChar* a, const String& b) { return !equal(a, b.impl()); }
inline bool operator!=(const char* a, const String& b) { return !equal(reinterpret_cast<const LChar*>(a), b.impl()); }
inline bool operator!=(ASCIILiteral a, const String& b) { return !equal(reinterpret_cast<const LChar*>(a.characters()), b.impl()); }
inline bool operator!=(ASCIILiteral a, const String& b) { return !equal(b.impl(), a); }
template<size_t inlineCapacity> inline bool operator!=(const Vector<char, inlineCapacity>& a, const String& b) { return !(a == b); }
template<size_t inlineCapacity> inline bool operator!=(const String& a, const Vector<char, inlineCapacity>& b) { return b != a; }

0 comments on commit a441398

Please sign in to comment.