Regression(r116408): Ctrl-A (select all) on large text file hangs the tab with high...
authortony@chromium.org <tony@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 20 Jun 2012 19:33:02 +0000 (19:33 +0000)
committertony@chromium.org <tony@chromium.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 20 Jun 2012 19:33:02 +0000 (19:33 +0000)
https://bugs.webkit.org/show_bug.cgi?id=89562

Reviewed by Ryosuke Niwa.

Rather than replace the newlines in-place (in O(n^2)), build a new string using StringBuilder, which takes O(n).

No new tests, this is a perf improvement.

* platform/chromium/ClipboardUtilitiesChromium.cpp:
(WebCore::replaceNewlinesWithWindowsStyleNewlines):
* platform/win/ClipboardUtilitiesWin.cpp:
(WebCore::replaceNewlinesWithWindowsStyleNewlines):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@120850 268f45cc-cd09-0410-ab3c-d52691b4dbfc

Source/WebCore/ChangeLog
Source/WebCore/platform/chromium/ClipboardUtilitiesChromium.cpp
Source/WebCore/platform/win/ClipboardUtilitiesWin.cpp

index 6bef3d7..57128cd 100644 (file)
@@ -1,3 +1,19 @@
+2012-06-20  Tony Chang  <tony@chromium.org>
+
+        Regression(r116408): Ctrl-A (select all) on large text file hangs the tab with high CPU usage
+        https://bugs.webkit.org/show_bug.cgi?id=89562
+
+        Reviewed by Ryosuke Niwa.
+
+        Rather than replace the newlines in-place (in O(n^2)), build a new string using StringBuilder, which takes O(n).
+
+        No new tests, this is a perf improvement.
+
+        * platform/chromium/ClipboardUtilitiesChromium.cpp:
+        (WebCore::replaceNewlinesWithWindowsStyleNewlines):
+        * platform/win/ClipboardUtilitiesWin.cpp:
+        (WebCore::replaceNewlinesWithWindowsStyleNewlines):
+
 2012-06-20  Elliott Sprehn  <esprehn@gmail.com>
 
         Add support for fit-content etc
index f00c80a..e2ec9f1 100644 (file)
@@ -36,6 +36,7 @@
 #include "PlatformString.h"
 
 #include <public/WebClipboard.h>
+#include <wtf/text/StringBuilder.h>
 
 namespace WebCore {
 
@@ -50,19 +51,14 @@ WebKit::WebClipboard::Buffer currentPasteboardBuffer()
 void replaceNewlinesWithWindowsStyleNewlines(String& str)
 {
     DEFINE_STATIC_LOCAL(String, windowsNewline, ("\r\n"));
-    const static unsigned windowsNewlineLength = windowsNewline.length();
-
-    unsigned index = 0;
-    unsigned strLength = str.length();
-    while (index < strLength) {
-        if (str[index] != '\n' || (index > 0 && str[index - 1] == '\r')) {
-            ++index;
-            continue;
-        }
-        str.replace(index, 1, windowsNewline);
-        strLength = str.length();
-        index += windowsNewlineLength;
+    StringBuilder result;
+    for (unsigned index = 0; index < str.length(); ++index) {
+        if (str[index] != '\n' || (index > 0 && str[index - 1] == '\r'))
+            result.append(str[index]);
+        else
+            result.append(windowsNewline);
     }
+    str = result.toString();
 }
 #endif
 
index aa66d13..61e3621 100644 (file)
@@ -35,6 +35,7 @@
 #include <wininet.h> // for INTERNET_MAX_URL_LENGTH
 #include <wtf/StringExtras.h>
 #include <wtf/text/CString.h>
+#include <wtf/text/StringBuilder.h>
 #include <wtf/text/WTFString.h>
 
 #if USE(CF)
@@ -299,19 +300,14 @@ void markupToCFHTML(const String& markup, const String& srcURL, Vector<char>& re
 void replaceNewlinesWithWindowsStyleNewlines(String& str)
 {
     DEFINE_STATIC_LOCAL(String, windowsNewline, ("\r\n"));
-    const static unsigned windowsNewlineLength = windowsNewline.length();
-
-    unsigned index = 0;
-    unsigned strLength = str.length();
-    while (index < strLength) {
-        if (str[index] != '\n' || (index > 0 && str[index - 1] == '\r')) {
-            ++index;
-            continue;
-        }
-        str.replace(index, 1, windowsNewline);
-        strLength = str.length();
-        index += windowsNewlineLength;
+    StringBuilder result;
+    for (unsigned index = 0; index < str.length(); ++index) {
+        if (str[index] != '\n' || (index > 0 && str[index - 1] == '\r'))
+            result.append(str[index]);
+        else
+            result.append(windowsNewline);
     }
+    str = result.toString();
 }
 
 void replaceNBSPWithSpace(String& str)