Title: [206076] trunk/Source/WebCore
Revision
206076
Author
achristen...@apple.com
Date
2016-09-18 01:01:11 -0700 (Sun, 18 Sep 2016)

Log Message

Remove unnecessary String allocations in URLParser
https://bugs.webkit.org/show_bug.cgi?id=162089

Reviewed by Chris Dumez.

No change in behavior except a performance improvement.

* platform/URL.cpp:
(WebCore::assertProtocolIsGood):
(WebCore::URL::protocolIs):
(WebCore::protocolIs):
* platform/URL.h:
Added a new protocolIs for non-null-terminated strings from user input.
* platform/URLParser.cpp:
(WebCore::URLParser::parse):
Don't make a String to compare protocols.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (206075 => 206076)


--- trunk/Source/WebCore/ChangeLog	2016-09-18 05:38:24 UTC (rev 206075)
+++ trunk/Source/WebCore/ChangeLog	2016-09-18 08:01:11 UTC (rev 206076)
@@ -1,3 +1,22 @@
+2016-09-18  Alex Christensen  <achristen...@webkit.org>
+
+        Remove unnecessary String allocations in URLParser
+        https://bugs.webkit.org/show_bug.cgi?id=162089
+
+        Reviewed by Chris Dumez.
+
+        No change in behavior except a performance improvement.
+
+        * platform/URL.cpp:
+        (WebCore::assertProtocolIsGood):
+        (WebCore::URL::protocolIs):
+        (WebCore::protocolIs):
+        * platform/URL.h:
+        Added a new protocolIs for non-null-terminated strings from user input.
+        * platform/URLParser.cpp:
+        (WebCore::URLParser::parse):
+        Don't make a String to compare protocols.
+
 2016-09-17  Alex Christensen  <achristen...@webkit.org>
 
         Inline functions in URLParser

Modified: trunk/Source/WebCore/platform/URL.cpp (206075 => 206076)


--- trunk/Source/WebCore/platform/URL.cpp	2016-09-18 05:38:24 UTC (rev 206075)
+++ trunk/Source/WebCore/platform/URL.cpp	2016-09-18 08:01:11 UTC (rev 206076)
@@ -790,18 +790,17 @@
 
 #ifdef NDEBUG
 
-static inline void assertProtocolIsGood(const char*)
+static inline void assertProtocolIsGood(const char*, size_t)
 {
 }
 
 #else
 
-static void assertProtocolIsGood(const char* protocol)
+static void assertProtocolIsGood(const char* protocol, size_t length)
 {
-    const char* p = protocol;
-    while (*p) {
-        ASSERT(*p > ' ' && *p < 0x7F && !(*p >= 'A' && *p <= 'Z'));
-        ++p;
+    for (size_t i = 0; i < length; ++i) {
+        const char c = protocol[i];
+        ASSERT(c > ' ' && c < 0x7F && !(c >= 'A' && c <= 'Z'));
     }
 }
 
@@ -809,7 +808,7 @@
 
 bool URL::protocolIs(const char* protocol) const
 {
-    assertProtocolIsGood(protocol);
+    assertProtocolIsGood(protocol, strlen(protocol));
 
     // _javascript_ URLs are "valid" and should be executed even if URL decides they are invalid.
     // The free function protocolIsJavaScript() should be used instead. 
@@ -826,6 +825,24 @@
     return !protocol[m_schemeEnd]; // We should have consumed all characters in the argument.
 }
 
+bool URL::protocolIs(const LChar* protocol, size_t length) const
+{
+    assertProtocolIsGood(reinterpret_cast<const char*>(protocol), length);
+
+    if (!m_isValid)
+        return false;
+    
+    if (m_schemeEnd != length)
+        return false;
+
+    // Do the comparison without making a new string object.
+    for (unsigned i = 0; i < m_schemeEnd; ++i) {
+        if (!isSchemeCharacterMatchIgnoringCase(m_string[i], protocol[i]))
+            return false;
+    }
+    return true;
+}
+
 String URL::query() const
 {
     if (m_queryEnd == m_pathEnd)
@@ -1899,7 +1916,7 @@
 
 static bool protocolIs(StringView stringURL, const char* protocol)
 {
-    assertProtocolIsGood(protocol);
+    assertProtocolIsGood(protocol, strlen(protocol));
     unsigned length = stringURL.length();
     for (unsigned i = 0; i < length; ++i) {
         if (!protocol[i])
@@ -2122,10 +2139,11 @@
     copyASCII(m_string, buffer.data());
 }
 
+// FIXME: Why is this different than protocolIs(StringView, const char*)?
 bool protocolIs(const String& url, const char* protocol)
 {
     // Do the comparison without making a new string object.
-    assertProtocolIsGood(protocol);
+    assertProtocolIsGood(protocol, strlen(protocol));
     bool isLeading = true;
     for (unsigned i = 0, j = 0; url[i]; ++i) {
         // skip leading whitespace and control characters.

Modified: trunk/Source/WebCore/platform/URL.h (206075 => 206076)


--- trunk/Source/WebCore/platform/URL.h	2016-09-18 05:38:24 UTC (rev 206075)
+++ trunk/Source/WebCore/platform/URL.h	2016-09-18 08:01:11 UTC (rev 206076)
@@ -23,8 +23,7 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
  */
 
-#ifndef URL_h
-#define URL_h
+#pragma once
 
 #include "PlatformExportMacros.h"
 #include <wtf/Forward.h>
@@ -130,6 +129,7 @@
     // Returns true if the current URL's protocol is the same as the null-
     // terminated ASCII argument. The argument must be lower-case.
     WEBCORE_EXPORT bool protocolIs(const char*) const;
+    bool protocolIs(const LChar*, size_t) const;
     bool protocolIsBlob() const { return protocolIs("blob"); }
     bool protocolIsData() const { return protocolIs("data"); }
     bool protocolIsInHTTPFamily() const;
@@ -455,5 +455,3 @@
     };
 
 } // namespace WTF
-
-#endif // URL_h

Modified: trunk/Source/WebCore/platform/URLParser.cpp (206075 => 206076)


--- trunk/Source/WebCore/platform/URLParser.cpp	2016-09-18 05:38:24 UTC (rev 206075)
+++ trunk/Source/WebCore/platform/URLParser.cpp	2016-09-18 08:01:11 UTC (rev 206076)
@@ -949,9 +949,7 @@
                 m_asciiBuffer.append(':');
                 if (isSpecialScheme(urlScheme)) {
                     m_urlIsSpecial = true;
-                    // FIXME: This is unnecessarily allocating a String.
-                    // This should be easy to optimize once https://bugs.webkit.org/show_bug.cgi?id=162035 lands.
-                    if (base.protocol() == urlScheme)
+                    if (base.protocolIs(m_asciiBuffer.data(), m_asciiBuffer.size() - 1))
                         state = State::SpecialRelativeOrAuthority;
                     else
                         state = State::SpecialAuthoritySlashes;
@@ -1005,7 +1003,7 @@
                 ++c;
                 break;
             }
-            if (base.protocol() != "file") {
+            if (!base.protocolIs("file")) {
                 state = State::Relative;
                 break;
             }
@@ -1442,7 +1440,7 @@
         break;
     case State::File:
         LOG_FINAL_STATE("File");
-        if (!base.isNull() && base.protocol() == "file") {
+        if (!base.isNull() && base.protocolIs("file")) {
             copyURLPartsUntil(base, URLPart::QueryEnd);
             m_asciiBuffer.append(':');
         }
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to