Commit e827c1a7 authored by esprehn's avatar esprehn Committed by Commit bot

Use StringView for String and AtomicString operator==.

This allows the compiler to compute the length of literal strings that
are compared and then we can early abort comparing if the length is
different.

This does introduce a strlen call for non-literal strings, which we
have very few of today.

This patch fixes the web idl enum checking loop to use WTF::equal to
avoid the strlen since that seemed like an easy way to remove lots of strlen
calls. We can make other callers use a similar approach in the future if
if we see strlen on profiles.

By code auditing (with code search method finding) I noticed a few other
places will now call strlen (ex. iframe sandbox and permissions attribute
parsing) but this patch opts not to convert all places we now call strlen and
instead go for the simple path of fixing those if we run into problems. This
is a similar trade off to code elsewhere in Chromium that uses
base::StringPiece which also puts a strlen call for non-literal strings.

I also took this as an opportunity to remove the operator== methods which aren't
used for things like LChar* (strangely UChar* was missing) and Vector<char>.

BUG=678266

Review-Url: https://codereview.chromium.org/2614123002
Cr-Commit-Position: refs/heads/master@{#441914}
parent 555f939f
......@@ -869,7 +869,8 @@ bool isValidEnum(const String& value,
const String& enumName,
ExceptionState& exceptionState) {
for (size_t i = 0; i < length; ++i) {
if (value == validValues[i])
// Avoid the strlen inside String::operator== (because of the StringView).
if (WTF::equal(value.impl(), validValues[i]))
return true;
}
exceptionState.throwTypeError("The provided value '" + value +
......
......@@ -232,54 +232,34 @@ class WTF_EXPORT AtomicString {
inline bool operator==(const AtomicString& a, const AtomicString& b) {
return a.impl() == b.impl();
}
WTF_EXPORT bool operator==(const AtomicString&, const LChar*);
inline bool operator==(const AtomicString& a, const char* b) {
return WTF::equal(a.impl(), reinterpret_cast<const LChar*>(b));
}
inline bool operator==(const AtomicString& a, const Vector<UChar>& b) {
return a.impl() && equal(a.impl(), b.data(), b.size());
}
inline bool operator==(const AtomicString& a, const String& b) {
// We don't use equalStringView so we get the isAtomic() optimization inside
// WTF::equal.
return equal(a.impl(), b.impl());
}
inline bool operator==(const LChar* a, const AtomicString& b) {
return b == a;
}
inline bool operator==(const char* a, const AtomicString& b) {
inline bool operator==(const String& a, const AtomicString& b) {
return b == a;
}
inline bool operator==(const String& a, const AtomicString& b) {
return equal(a.impl(), b.impl());
inline bool operator==(const AtomicString& a, const char* b) {
return equalStringView(a, b);
}
inline bool operator==(const Vector<UChar>& a, const AtomicString& b) {
inline bool operator==(const char* a, const AtomicString& b) {
return b == a;
}
inline bool operator!=(const AtomicString& a, const AtomicString& b) {
return a.impl() != b.impl();
}
inline bool operator!=(const AtomicString& a, const LChar* b) {
inline bool operator!=(const AtomicString& a, const String& b) {
return !(a == b);
}
inline bool operator!=(const AtomicString& a, const char* b) {
inline bool operator!=(const String& a, const AtomicString& b) {
return !(a == b);
}
inline bool operator!=(const AtomicString& a, const String& b) {
return !equal(a.impl(), b.impl());
}
inline bool operator!=(const AtomicString& a, const Vector<UChar>& b) {
inline bool operator!=(const AtomicString& a, const char* b) {
return !(a == b);
}
inline bool operator!=(const LChar* a, const AtomicString& b) {
return !(b == a);
}
inline bool operator!=(const char* a, const AtomicString& b) {
return !(b == a);
}
inline bool operator!=(const String& a, const AtomicString& b) {
return !equal(a.impl(), b.impl());
}
inline bool operator!=(const Vector<UChar>& a, const AtomicString& b) {
return !(a == b);
}
......
......@@ -442,52 +442,26 @@ class WTF_EXPORT String {
#undef DISPATCH_CASE_OP
inline bool operator==(const String& a, const String& b) {
// We don't use equalStringView here since we want the isAtomic() fast path
// inside WTF::equal.
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 LChar* a, const String& b) {
return equal(a, b.impl());
return equalStringView(a, b);
}
inline bool operator==(const char* a, const String& b) {
return equal(reinterpret_cast<const LChar*>(a), b.impl());
}
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);
return !(a == b);
}
inline bool operator!=(const String& a, const char* b) {
return !equal(a.impl(), reinterpret_cast<const LChar*>(b));
}
inline bool operator!=(const LChar* a, const String& b) {
return !equal(a, b.impl());
return !(a == b);
}
inline bool operator!=(const char* a, const String& b) {
return !equal(reinterpret_cast<const LChar*>(a), b.impl());
}
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;
}
inline bool equalPossiblyIgnoringCase(const String& a,
const String& b,
......
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