Title: [91913] trunk/Source/_javascript_Core
Revision
91913
Author
[email protected]
Date
2011-07-28 00:38:23 -0700 (Thu, 28 Jul 2011)

Log Message

Improve StringImpl::stripWhiteSpace() and simplifyWhiteSpace().
https://bugs.webkit.org/show_bug.cgi?id=65300

Reviewed by Darin Adler.

r91837 had performance regression of StringImpl::stripWhiteSpace()
and simplifyWhiteSpace(). This changes the code so that compilers
generates code equivalent to r91836 or piror.

* wtf/text/StringImpl.cpp:
(WTF::StringImpl::stripMatchedCharacters):
A template member function for stripWhiteSpace(). This function takes a functor.
(WTF::UCharPredicate):
A functor for generic predicate for single UChar argument.
(WTF::SpaceOrNewlinePredicate):
A special functor for isSpaceOrNewline().
(WTF::StringImpl::stripWhiteSpace):
Use stripmatchedCharacters().
(WTF::StringImpl::simplifyMatchedCharactersToSpace):
A template member function for simplifyWhiteSpace().
(WTF::StringImpl::simplifyWhiteSpace):
Use simplifyMatchedCharactersToSpace().
* wtf/text/StringImpl.h:

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (91912 => 91913)


--- trunk/Source/_javascript_Core/ChangeLog	2011-07-28 06:46:33 UTC (rev 91912)
+++ trunk/Source/_javascript_Core/ChangeLog	2011-07-28 07:38:23 UTC (rev 91913)
@@ -1,3 +1,29 @@
+2011-07-28  Kent Tamura  <[email protected]>
+
+        Improve StringImpl::stripWhiteSpace() and simplifyWhiteSpace().
+        https://bugs.webkit.org/show_bug.cgi?id=65300
+
+        Reviewed by Darin Adler.
+
+        r91837 had performance regression of StringImpl::stripWhiteSpace()
+        and simplifyWhiteSpace(). This changes the code so that compilers
+        generates code equivalent to r91836 or piror.
+
+        * wtf/text/StringImpl.cpp:
+        (WTF::StringImpl::stripMatchedCharacters):
+        A template member function for stripWhiteSpace(). This function takes a functor.
+        (WTF::UCharPredicate):
+        A functor for generic predicate for single UChar argument.
+        (WTF::SpaceOrNewlinePredicate):
+        A special functor for isSpaceOrNewline().
+        (WTF::StringImpl::stripWhiteSpace):
+        Use stripmatchedCharacters().
+        (WTF::StringImpl::simplifyMatchedCharactersToSpace):
+        A template member function for simplifyWhiteSpace().
+        (WTF::StringImpl::simplifyWhiteSpace):
+        Use simplifyMatchedCharactersToSpace().
+        * wtf/text/StringImpl.h:
+
 2011-07-27  Dmitry Lomov  <[email protected]>
 
         [chromium] Turn on WTF_MULTIPLE_THREADS.

Modified: trunk/Source/_javascript_Core/wtf/text/StringImpl.cpp (91912 => 91913)


--- trunk/Source/_javascript_Core/wtf/text/StringImpl.cpp	2011-07-28 06:46:33 UTC (rev 91912)
+++ trunk/Source/_javascript_Core/wtf/text/StringImpl.cpp	2011-07-28 07:38:23 UTC (rev 91913)
@@ -313,13 +313,9 @@
     return newImpl.release();
 }
 
-PassRefPtr<StringImpl> StringImpl::stripWhiteSpace()
+template <class UCharPredicate>
+inline PassRefPtr<StringImpl> StringImpl::stripMatchedCharacters(UCharPredicate predicate)
 {
-    return stripWhiteSpace(isSpaceOrNewline);
-}
-
-PassRefPtr<StringImpl> StringImpl::stripWhiteSpace(IsWhiteSpaceFunctionPtr isWhiteSpace)
-{
     if (!m_length)
         return empty();
 
@@ -327,7 +323,7 @@
     unsigned end = m_length - 1;
     
     // skip white space from start
-    while (start <= end && isWhiteSpace(m_data[start]))
+    while (start <= end && predicate(m_data[start]))
         start++;
     
     // only white space
@@ -335,7 +331,7 @@
         return empty();
 
     // skip white space from end
-    while (end && isWhiteSpace(m_data[end]))
+    while (end && predicate(m_data[end]))
         end--;
 
     if (!start && end == m_length - 1)
@@ -343,6 +339,37 @@
     return create(m_data + start, end + 1 - start);
 }
 
+class UCharPredicate {
+public:
+    inline UCharPredicate(CharacterMatchFunctionPtr function): m_function(function) { }
+
+    inline bool operator()(UChar ch) const
+    {
+        return m_function(ch);
+    }
+
+private:
+    const CharacterMatchFunctionPtr m_function;
+};
+
+class SpaceOrNewlinePredicate {
+public:
+    inline bool operator()(UChar ch) const
+    {
+        return isSpaceOrNewline(ch);
+    }
+};
+
+PassRefPtr<StringImpl> StringImpl::stripWhiteSpace()
+{
+    return stripMatchedCharacters(SpaceOrNewlinePredicate());
+}
+
+PassRefPtr<StringImpl> StringImpl::stripWhiteSpace(IsWhiteSpaceFunctionPtr isWhiteSpace)
+{
+    return stripMatchedCharacters(UCharPredicate(isWhiteSpace));
+}
+
 PassRefPtr<StringImpl> StringImpl::removeCharacters(CharacterMatchFunctionPtr findMatch)
 {
     const UChar* from = m_data;
@@ -375,13 +402,9 @@
     return adopt(data);
 }
 
-PassRefPtr<StringImpl> StringImpl::simplifyWhiteSpace()
+template <class UCharPredicate>
+inline PassRefPtr<StringImpl> StringImpl::simplifyMatchedCharactersToSpace(UCharPredicate predicate)
 {
-    return StringImpl::simplifyWhiteSpace(isSpaceOrNewline);
-}
-
-PassRefPtr<StringImpl> StringImpl::simplifyWhiteSpace(IsWhiteSpaceFunctionPtr isWhiteSpace)
-{
     StringBuffer data(m_length);
 
     const UChar* from = m_data;
@@ -392,12 +415,12 @@
     UChar* to = data.characters();
     
     while (true) {
-        while (from != fromend && isWhiteSpace(*from)) {
+        while (from != fromend && predicate(*from)) {
             if (*from != ' ')
                 changedToSpace = true;
             from++;
         }
-        while (from != fromend && !isWhiteSpace(*from))
+        while (from != fromend && !predicate(*from))
             to[outc++] = *from++;
         if (from != fromend)
             to[outc++] = ' ';
@@ -416,6 +439,16 @@
     return adopt(data);
 }
 
+PassRefPtr<StringImpl> StringImpl::simplifyWhiteSpace()
+{
+    return StringImpl::simplifyMatchedCharactersToSpace(SpaceOrNewlinePredicate());
+}
+
+PassRefPtr<StringImpl> StringImpl::simplifyWhiteSpace(IsWhiteSpaceFunctionPtr isWhiteSpace)
+{
+    return StringImpl::simplifyMatchedCharactersToSpace(UCharPredicate(isWhiteSpace));
+}
+
 int StringImpl::toIntStrict(bool* ok, int base)
 {
     return charactersToIntStrict(m_data, m_length, ok, base);

Modified: trunk/Source/_javascript_Core/wtf/text/StringImpl.h (91912 => 91913)


--- trunk/Source/_javascript_Core/wtf/text/StringImpl.h	2011-07-28 06:46:33 UTC (rev 91912)
+++ trunk/Source/_javascript_Core/wtf/text/StringImpl.h	2011-07-28 07:38:23 UTC (rev 91913)
@@ -331,6 +331,9 @@
     
     BufferOwnership bufferOwnership() const { return static_cast<BufferOwnership>(m_refCountAndFlags & s_refCountMaskBufferOwnership); }
     bool isStatic() const { return m_refCountAndFlags & s_refCountFlagStatic; }
+    template <class UCharPredicate> PassRefPtr<StringImpl> stripMatchedCharacters(UCharPredicate);
+    template <class UCharPredicate> PassRefPtr<StringImpl> simplifyMatchedCharactersToSpace(UCharPredicate);
+
     const UChar* m_data;
     union {
         void* m_buffer;
_______________________________________________
webkit-changes mailing list
[email protected]
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to