Title: [115132] trunk/Source
Revision
115132
Author
[email protected]
Date
2012-04-24 16:41:32 -0700 (Tue, 24 Apr 2012)

Log Message

Generalize the single character optimization of r114072
https://bugs.webkit.org/show_bug.cgi?id=83961

Patch by Benjamin Poulain <[email protected]> on 2012-04-24
Reviewed by Eric Seidel.

Source/_javascript_Core: 

Use the regular String::find(StringImpl*) in all cases now that it has been made faster.

* runtime/StringPrototype.cpp:
(JSC::replaceUsingStringSearch):

Source/WTF: 

This patch makes some improvment over String::find() in the case of lookup for a single
character.

The two function find(UChar|LChar) are replaced by a template.

The case of searching a UChar in a 8bit string has a shortcut if the UChar cannot
possibly match any character in the range of the type LChar.

The slow case StringImpl::find(StringImpl*) is modified to
-Do not allocate in the fast case if only one string is 8bit.
-Use the shortcut for searching UChar in LChar.

This speed up the function by about 7% when avoiding string conversion.

* wtf/text/StringImpl.cpp:
(WTF::StringImpl::find):
* wtf/text/StringImpl.h:
(StringImpl):
(WTF):
(WTF::find):
(WTF::StringImpl::find):

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (115131 => 115132)


--- trunk/Source/_javascript_Core/ChangeLog	2012-04-24 23:33:40 UTC (rev 115131)
+++ trunk/Source/_javascript_Core/ChangeLog	2012-04-24 23:41:32 UTC (rev 115132)
@@ -1,3 +1,15 @@
+2012-04-24  Benjamin Poulain  <[email protected]>
+
+        Generalize the single character optimization of r114072
+        https://bugs.webkit.org/show_bug.cgi?id=83961
+
+        Reviewed by Eric Seidel.
+
+        Use the regular String::find(StringImpl*) in all cases now that it has been made faster.
+
+        * runtime/StringPrototype.cpp:
+        (JSC::replaceUsingStringSearch):
+
 2012-04-24  Filip Pizlo  <[email protected]>
 
         Unreviewed, 32-bit build fix.

Modified: trunk/Source/_javascript_Core/runtime/StringPrototype.cpp (115131 => 115132)


--- trunk/Source/_javascript_Core/runtime/StringPrototype.cpp	2012-04-24 23:33:40 UTC (rev 115131)
+++ trunk/Source/_javascript_Core/runtime/StringPrototype.cpp	2012-04-24 23:41:32 UTC (rev 115132)
@@ -633,21 +633,8 @@
     if (exec->hadException())
         return JSValue::encode(jsUndefined());
 
-    size_t searchStringLength = searchString.length();
+    size_t matchStart = string.find(searchString);
 
-    size_t matchStart;
-    if (searchStringLength == 1) {
-        StringImpl* searchStringImpl = searchString.impl();
-        UChar searchCharacter;
-        if (searchStringImpl->is8Bit())
-            searchCharacter = searchStringImpl->characters8()[0];
-        else
-            searchCharacter = searchStringImpl->characters16()[0];
-
-        matchStart = string.find(searchCharacter);
-    } else
-        matchStart = string.find(searchString);
-
     if (matchStart == notFound)
         return JSValue::encode(jsString);
 
@@ -656,7 +643,7 @@
     CallType callType = getCallData(replaceValue, callData);
     if (callType != CallTypeNone) {
         MarkedArgumentBuffer args;
-        args.append(jsSubstring(exec, string, matchStart, searchStringLength));
+        args.append(jsSubstring(exec, string, matchStart, searchString.impl()->length()));
         args.append(jsNumber(matchStart));
         args.append(jsString);
         replaceValue = call(exec, replaceValue, callType, callData, jsUndefined(), args);
@@ -671,7 +658,7 @@
     StringImpl* stringImpl = string.impl();
     UString leftPart(StringImpl::create(stringImpl, 0, matchStart));
 
-    size_t matchEnd = matchStart + searchStringLength;
+    size_t matchEnd = matchStart + searchString.impl()->length();
     int ovector[2] = { matchStart,  matchEnd};
     UString middlePart = substituteBackreferences(replaceString, string, ovector, 0);
 

Modified: trunk/Source/WTF/ChangeLog (115131 => 115132)


--- trunk/Source/WTF/ChangeLog	2012-04-24 23:33:40 UTC (rev 115131)
+++ trunk/Source/WTF/ChangeLog	2012-04-24 23:41:32 UTC (rev 115132)
@@ -1,3 +1,32 @@
+2012-04-24  Benjamin Poulain  <[email protected]>
+
+        Generalize the single character optimization of r114072
+        https://bugs.webkit.org/show_bug.cgi?id=83961
+
+        Reviewed by Eric Seidel.
+
+        This patch makes some improvment over String::find() in the case of lookup for a single
+        character.
+
+        The two function find(UChar|LChar) are replaced by a template.
+
+        The case of searching a UChar in a 8bit string has a shortcut if the UChar cannot
+        possibly match any character in the range of the type LChar.
+
+        The slow case StringImpl::find(StringImpl*) is modified to
+        -Do not allocate in the fast case if only one string is 8bit.
+        -Use the shortcut for searching UChar in LChar.
+
+        This speed up the function by about 7% when avoiding string conversion.
+
+        * wtf/text/StringImpl.cpp:
+        (WTF::StringImpl::find):
+        * wtf/text/StringImpl.h:
+        (StringImpl):
+        (WTF):
+        (WTF::find):
+        (WTF::StringImpl::find):
+
 2012-04-24  Darin Adler  <[email protected]>
 
         Iterating a HashMap<String, X> involves a string equality comparison to check for the empty value

Modified: trunk/Source/WTF/wtf/text/StringImpl.cpp (115131 => 115132)


--- trunk/Source/WTF/wtf/text/StringImpl.cpp	2012-04-24 23:33:40 UTC (rev 115131)
+++ trunk/Source/WTF/wtf/text/StringImpl.cpp	2012-04-24 23:41:32 UTC (rev 115132)
@@ -905,9 +905,14 @@
 
     // Optimization 1: fast case for strings of length 1.
     if (matchLength == 1) {
-        if (is8Bit() && matchString->is8Bit())
-            return WTF::find(characters8(), length(), matchString->characters8()[0], index);
-        return WTF::find(characters(), length(), matchString->characters()[0], index);
+        if (is8Bit()) {
+            if (matchString->is8Bit())
+                return WTF::find(characters8(), length(), matchString->characters8()[0], index);
+            return WTF::find(characters8(), length(), matchString->characters16()[0], index);
+        }
+        if (matchString->is8Bit())
+            return WTF::find(characters16(), length(), matchString->characters8()[0], index);
+        return WTF::find(characters16(), length(), matchString->characters16()[0], index);
     }
 
     // Check index & matchLength are in range.

Modified: trunk/Source/WTF/wtf/text/StringImpl.h (115131 => 115132)


--- trunk/Source/WTF/wtf/text/StringImpl.h	2012-04-24 23:33:40 UTC (rev 115131)
+++ trunk/Source/WTF/wtf/text/StringImpl.h	2012-04-24 23:41:32 UTC (rev 115132)
@@ -481,6 +481,8 @@
     template <typename CharType>
     ALWAYS_INLINE PassRefPtr<StringImpl> removeCharacters(const CharType* characters, CharacterMatchFunctionPtr);
 
+    size_t find(LChar character, unsigned start = 0);
+    size_t find(char character, unsigned start = 0);
     size_t find(UChar character, unsigned start = 0);
     WTF_EXPORT_PRIVATE size_t find(CharacterMatchFunctionPtr, unsigned index = 0);
     size_t find(const LChar*, unsigned index = 0);
@@ -735,7 +737,8 @@
 
 WTF_EXPORT_PRIVATE bool equalIgnoringNullity(StringImpl*, StringImpl*);
 
-inline size_t find(const LChar* characters, unsigned length, LChar matchCharacter, unsigned index = 0)
+template<typename CharacterType>
+inline size_t find(const CharacterType* characters, unsigned length, CharacterType matchCharacter, unsigned index = 0)
 {
     while (index < length) {
         if (characters[index] == matchCharacter)
@@ -745,16 +748,18 @@
     return notFound;
 }
 
-inline size_t find(const UChar* characters, unsigned length, UChar matchCharacter, unsigned index = 0)
+ALWAYS_INLINE size_t find(const UChar* characters, unsigned length, LChar matchCharacter, unsigned index = 0)
 {
-    while (index < length) {
-        if (characters[index] == matchCharacter)
-            return index;
-        ++index;
-    }
-    return notFound;
+    return find(characters, length, static_cast<UChar>(matchCharacter), index);
 }
 
+inline size_t find(const LChar* characters, unsigned length, UChar matchCharacter, unsigned index = 0)
+{
+    if (matchCharacter & ~0xFF)
+        return notFound;
+    return find(characters, length, static_cast<LChar>(matchCharacter), index);
+}
+
 inline size_t find(const LChar* characters, unsigned length, CharacterMatchFunctionPtr matchFunction, unsigned index = 0)
 {
     while (index < length) {
@@ -801,6 +806,18 @@
     return index;
 }
 
+inline size_t StringImpl::find(LChar character, unsigned start)
+{
+    if (is8Bit())
+        return WTF::find(characters8(), m_length, character, start);
+    return WTF::find(characters16(), m_length, character, start);
+}
+
+ALWAYS_INLINE size_t StringImpl::find(char character, unsigned start)
+{
+    return find(static_cast<LChar>(character), start);
+}
+
 inline size_t StringImpl::find(UChar character, unsigned start)
 {
     if (is8Bit())
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to