Bug 1247359 - micro-optimize the common case of String{Begins,End}With; r=erahm
StringBeginsWith (resp. StringEndsWith) takes a defaulted nsStringComparator object for doing comparisons. The flexibility this affords is great, but the cost is not: nsStringComparator has virtual methods, so initializing that defaulted object (at every callsite) requires a temporary object whose vtable must be initialized. Since the overwhemingly common case is to use the default comparator anyway, we should not use defaulted arguments and instead provide the default comparator/user-provided comparator cases as separate overloads. This change eliminates the virtual call for the majority of callsites and reduces codesize as well.
This commit is contained in:
@@ -1026,6 +1026,17 @@ CountCharInReadable(const nsACString& aStr, char aChar)
|
||||
return count;
|
||||
}
|
||||
|
||||
bool
|
||||
StringBeginsWith(const nsAString& aSource, const nsAString& aSubstring)
|
||||
{
|
||||
nsAString::size_type src_len = aSource.Length(),
|
||||
sub_len = aSubstring.Length();
|
||||
if (sub_len > src_len) {
|
||||
return false;
|
||||
}
|
||||
return Substring(aSource, 0, sub_len).Equals(aSubstring);
|
||||
}
|
||||
|
||||
bool
|
||||
StringBeginsWith(const nsAString& aSource, const nsAString& aSubstring,
|
||||
const nsStringComparator& aComparator)
|
||||
@@ -1038,6 +1049,17 @@ StringBeginsWith(const nsAString& aSource, const nsAString& aSubstring,
|
||||
return Substring(aSource, 0, sub_len).Equals(aSubstring, aComparator);
|
||||
}
|
||||
|
||||
bool
|
||||
StringBeginsWith(const nsACString& aSource, const nsACString& aSubstring)
|
||||
{
|
||||
nsACString::size_type src_len = aSource.Length(),
|
||||
sub_len = aSubstring.Length();
|
||||
if (sub_len > src_len) {
|
||||
return false;
|
||||
}
|
||||
return Substring(aSource, 0, sub_len).Equals(aSubstring);
|
||||
}
|
||||
|
||||
bool
|
||||
StringBeginsWith(const nsACString& aSource, const nsACString& aSubstring,
|
||||
const nsCStringComparator& aComparator)
|
||||
@@ -1050,6 +1072,17 @@ StringBeginsWith(const nsACString& aSource, const nsACString& aSubstring,
|
||||
return Substring(aSource, 0, sub_len).Equals(aSubstring, aComparator);
|
||||
}
|
||||
|
||||
bool
|
||||
StringEndsWith(const nsAString& aSource, const nsAString& aSubstring)
|
||||
{
|
||||
nsAString::size_type src_len = aSource.Length(),
|
||||
sub_len = aSubstring.Length();
|
||||
if (sub_len > src_len) {
|
||||
return false;
|
||||
}
|
||||
return Substring(aSource, src_len - sub_len, sub_len).Equals(aSubstring);
|
||||
}
|
||||
|
||||
bool
|
||||
StringEndsWith(const nsAString& aSource, const nsAString& aSubstring,
|
||||
const nsStringComparator& aComparator)
|
||||
@@ -1063,6 +1096,17 @@ StringEndsWith(const nsAString& aSource, const nsAString& aSubstring,
|
||||
aComparator);
|
||||
}
|
||||
|
||||
bool
|
||||
StringEndsWith(const nsACString& aSource, const nsACString& aSubstring)
|
||||
{
|
||||
nsACString::size_type src_len = aSource.Length(),
|
||||
sub_len = aSubstring.Length();
|
||||
if (sub_len > src_len) {
|
||||
return false;
|
||||
}
|
||||
return Substring(aSource, src_len - sub_len, sub_len).Equals(aSubstring);
|
||||
}
|
||||
|
||||
bool
|
||||
StringEndsWith(const nsACString& aSource, const nsACString& aSubstring,
|
||||
const nsCStringComparator& aComparator)
|
||||
|
||||
Reference in New Issue
Block a user