[SCM] WebKit Debian packaging branch, webkit-1.1, updated. upstream/1.1.15.1-1414-gc69ee75

eric at webkit.org eric at webkit.org
Thu Oct 29 20:42:37 UTC 2009


The following commit has been merged in the webkit-1.1 branch:
commit 37b42e42c863d44384f3f0b96d0513179a8421db
Author: eric at webkit.org <eric at webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Date:   Fri Oct 9 21:24:28 2009 +0000

    2009-10-09  Jens Alfke  <snej at chromium.org>
    
            Reviewed by Darin Adler.
    
            Optimization: Many StringImpl transformations are no-ops and should just return 'this'
            https://bugs.webkit.org/show_bug.cgi?id=30186
    
            Optimized StringImpl methods lower(), stripWhiteSpace() and simplifyWhiteSpace() to
            detect no-ops and return this instead of creating a new instance.
            Empirical testing shows that the majority of calls to these methods are no-ops, making
            this worthwhile even if (in the case of lower()) the non-no-op case is slightly slowed.
            Upper() is very rarely a no-op, so it wasn't worthwhile to optimize it.
    
            * platform/text/StringImpl.cpp:
            (WebCore::StringImpl::lower):
            (WebCore::StringImpl::upper): Just add a comment explaining why this wasn't optimized
            (WebCore::StringImpl::stripWhiteSpace):
            (WebCore::StringImpl::simplifyWhiteSpace):
    
    git-svn-id: http://svn.webkit.org/repository/webkit/trunk@49403 268f45cc-cd09-0410-ab3c-d52691b4dbfc

diff --git a/WebCore/ChangeLog b/WebCore/ChangeLog
index 703fdc2..b4fdd65 100644
--- a/WebCore/ChangeLog
+++ b/WebCore/ChangeLog
@@ -1,3 +1,22 @@
+2009-10-09  Jens Alfke  <snej at chromium.org>
+
+        Reviewed by Darin Adler.
+        
+        Optimization: Many StringImpl transformations are no-ops and should just return 'this'
+        https://bugs.webkit.org/show_bug.cgi?id=30186
+
+        Optimized StringImpl methods lower(), stripWhiteSpace() and simplifyWhiteSpace() to
+        detect no-ops and return this instead of creating a new instance.
+        Empirical testing shows that the majority of calls to these methods are no-ops, making
+        this worthwhile even if (in the case of lower()) the non-no-op case is slightly slowed.
+        Upper() is very rarely a no-op, so it wasn't worthwhile to optimize it.
+
+        * platform/text/StringImpl.cpp:
+        (WebCore::StringImpl::lower):
+        (WebCore::StringImpl::upper): Just add a comment explaining why this wasn't optimized
+        (WebCore::StringImpl::stripWhiteSpace):
+        (WebCore::StringImpl::simplifyWhiteSpace):
+
 2009-10-09  Dirk Schulze  <krit at webkit.org>
 
         Reviewed by Oliver Hunt.
diff --git a/WebCore/platform/text/StringImpl.cpp b/WebCore/platform/text/StringImpl.cpp
index dffae98..c3f03de 100644
--- a/WebCore/platform/text/StringImpl.cpp
+++ b/WebCore/platform/text/StringImpl.cpp
@@ -175,20 +175,32 @@ bool StringImpl::isLower()
 
 PassRefPtr<StringImpl> StringImpl::lower()
 {
-    UChar* data;
-    PassRefPtr<StringImpl> newImpl = createUninitialized(m_length, data);
+    // First scan the string for uppercase and non-ASCII characters:
     int32_t length = m_length;
-
-    // Do a faster loop for the case where all the characters are ASCII.
     UChar ored = 0;
+    bool noUpper = true;
     for (int i = 0; i < length; i++) {
         UChar c = m_data[i];
         ored |= c;
-        data[i] = toASCIILower(c);
+        noUpper = noUpper && !isASCIIUpper(c);
     }
-    if (!(ored & ~0x7F))
-        return newImpl;
+    
+    // Nothing to do if the string is all ASCII with no uppercase.
+    if (noUpper && !(ored & ~0x7F))
+        return this;
 
+    UChar* data;
+    RefPtr<StringImpl> newImpl = createUninitialized(m_length, data);
+
+    if (!(ored & ~0x7F)) {
+        // Do a faster loop for the case where all the characters are ASCII.
+        for (int i = 0; i < length; i++) {
+            UChar c = m_data[i];
+            data[i] = toASCIILower(c);
+        }
+        return newImpl;
+    }
+    
     // Do a slower implementation for cases that include non-ASCII characters.
     bool error;
     int32_t realLength = Unicode::toLower(data, length, m_data, m_length, &error);
@@ -203,6 +215,9 @@ PassRefPtr<StringImpl> StringImpl::lower()
 
 PassRefPtr<StringImpl> StringImpl::upper()
 {
+    // This function could be optimized for no-op cases the way lower() is,
+    // but in empirical testing, few actual calls to upper() are no-ops, so
+    // it wouldn't be worth the extra time for pre-scanning.
     UChar* data;
     PassRefPtr<StringImpl> newImpl = createUninitialized(m_length, data);
     int32_t length = m_length;
@@ -287,6 +302,8 @@ PassRefPtr<StringImpl> StringImpl::stripWhiteSpace()
     while (end && isSpaceOrNewline(m_data[end]))
         end--;
 
+    if (!start && end == m_length - 1)
+        return this;
     return create(m_data + start, end + 1 - start);
 }
 
@@ -329,12 +346,16 @@ PassRefPtr<StringImpl> StringImpl::simplifyWhiteSpace()
     const UChar* from = m_data;
     const UChar* fromend = from + m_length;
     int outc = 0;
+    bool changedToSpace = false;
     
     UChar* to = data.characters();
     
     while (true) {
-        while (from != fromend && isSpaceOrNewline(*from))
+        while (from != fromend && isSpaceOrNewline(*from)) {
+            if (*from != ' ')
+                changedToSpace = true;
             from++;
+        }
         while (from != fromend && !isSpaceOrNewline(*from))
             to[outc++] = *from++;
         if (from != fromend)
@@ -346,6 +367,9 @@ PassRefPtr<StringImpl> StringImpl::simplifyWhiteSpace()
     if (outc > 0 && to[outc - 1] == ' ')
         outc--;
     
+    if (static_cast<unsigned>(outc) == m_length && !changedToSpace)
+        return this;
+    
     data.shrink(outc);
     
     return adopt(data);

-- 
WebKit Debian packaging



More information about the Pkg-webkit-commits mailing list