Title: [232302] trunk/Source
Revision
232302
Author
[email protected]
Date
2018-05-30 12:21:34 -0700 (Wed, 30 May 2018)

Log Message

Reduce String allocations
https://bugs.webkit.org/show_bug.cgi?id=186059

Patch by Alex Christensen <[email protected]> on 2018-05-30
Reviewed by Darin Adler.

Source/WebCore:

Don't allocate Strings just to convert it to another form.
Based mostly on Darin's feedback on bug 185986.

No change in behavior.

* Modules/websockets/WebSocketHandshake.cpp:
(WebCore::hostName):
(WebCore::WebSocketHandshake::host const):
* css/parser/CSSSelectorParser.cpp:
(WebCore::CSSSelectorParser::consumePseudo):
(WebCore::CSSSelectorParser::consumeANPlusB):
* loader/mac/LoaderNSURLExtras.mm:
(suggestedFilenameWithMIMEType):
* page/SecurityOriginData.h:
(WebCore::SecurityOriginData::fromURL):
* page/csp/ContentSecurityPolicySource.cpp:
(WebCore::wildcardMatches):
(WebCore::ContentSecurityPolicySource::hostMatches const):
* platform/URL.cpp:
(WebCore::URL::hostAndPort const):
* platform/network/cf/SocketStreamHandleImplCFNet.cpp:
(WebCore::SocketStreamHandleImpl::createStreams):

Source/WTF:

* wtf/text/StringView.cpp:
(WTF::convertASCIICase):
(WTF::StringView::convertToASCIILowercase const):
(WTF::StringView::convertToASCIIUppercase const):
* wtf/text/StringView.h:
* wtf/text/cf/StringViewCF.cpp:
(WTF::StringView::createCFString const):

Modified Paths

Diff

Modified: trunk/Source/WTF/ChangeLog (232301 => 232302)


--- trunk/Source/WTF/ChangeLog	2018-05-30 18:00:27 UTC (rev 232301)
+++ trunk/Source/WTF/ChangeLog	2018-05-30 19:21:34 UTC (rev 232302)
@@ -1,3 +1,18 @@
+2018-05-30  Alex Christensen  <[email protected]>
+
+        Reduce String allocations
+        https://bugs.webkit.org/show_bug.cgi?id=186059
+
+        Reviewed by Darin Adler.
+
+        * wtf/text/StringView.cpp:
+        (WTF::convertASCIICase):
+        (WTF::StringView::convertToASCIILowercase const):
+        (WTF::StringView::convertToASCIIUppercase const):
+        * wtf/text/StringView.h:
+        * wtf/text/cf/StringViewCF.cpp:
+        (WTF::StringView::createCFString const):
+
 2018-05-30  Dominik Infuehr  <[email protected]>
 
         [MIPS] Fix build on MIPS32r1

Modified: trunk/Source/WTF/wtf/text/StringView.cpp (232301 => 232302)


--- trunk/Source/WTF/wtf/text/StringView.cpp	2018-05-30 18:00:27 UTC (rev 232301)
+++ trunk/Source/WTF/wtf/text/StringView.cpp	2018-05-30 19:21:34 UTC (rev 232302)
@@ -33,6 +33,7 @@
 #include <wtf/Lock.h>
 #include <wtf/NeverDestroyed.h>
 #include <wtf/Optional.h>
+#include <wtf/text/StringBuffer.h>
 #include <wtf/text/TextBreakIterator.h>
 #include <wtf/unicode/UTF8.h>
 
@@ -206,6 +207,35 @@
     return !(*this == other);
 }
 
+enum class ASCIICase { Lower, Upper };
+
+template<ASCIICase type, typename CharacterType>
+String convertASCIICase(const CharacterType* input, unsigned length)
+{
+    if (!input)
+        return { };
+
+    StringBuffer<CharacterType> buffer(length);
+    CharacterType* characters = buffer.characters();
+    for (unsigned i = 0; i < length; ++i)
+        characters[i] = type == ASCIICase::Lower ? toASCIILower(input[i]) : toASCIIUpper(input[i]);
+    return String::adopt(WTFMove(buffer));
+}
+
+String StringView::convertToASCIILowercase() const
+{
+    if (m_is8Bit)
+        return convertASCIICase<ASCIICase::Lower>(static_cast<const LChar*>(m_characters), m_length);
+    return convertASCIICase<ASCIICase::Lower>(static_cast<const UChar*>(m_characters), m_length);
+}
+
+String StringView::convertToASCIIUppercase() const
+{
+    if (m_is8Bit)
+        return convertASCIICase<ASCIICase::Upper>(static_cast<const LChar*>(m_characters), m_length);
+    return convertASCIICase<ASCIICase::Upper>(static_cast<const UChar*>(m_characters), m_length);
+}
+
 #if CHECK_STRINGVIEW_LIFETIME
 
 // Manage reference count manually so UnderlyingString does not need to be defined in the header.

Modified: trunk/Source/WTF/wtf/text/StringView.h (232301 => 232302)


--- trunk/Source/WTF/wtf/text/StringView.h	2018-05-30 18:00:27 UTC (rev 232301)
+++ trunk/Source/WTF/wtf/text/StringView.h	2018-05-30 19:21:34 UTC (rev 232302)
@@ -98,6 +98,7 @@
 
 #if USE(CF)
     // This function converts null strings to empty strings.
+    WTF_EXPORT_PRIVATE RetainPtr<CFStringRef> createCFString() const;
     WTF_EXPORT_PRIVATE RetainPtr<CFStringRef> createCFStringWithoutCopying() const;
 #endif
 
@@ -136,6 +137,9 @@
     WTF_EXPORT_PRIVATE size_t findIgnoringASCIICase(const StringView&) const;
     WTF_EXPORT_PRIVATE size_t findIgnoringASCIICase(const StringView&, unsigned startOffset) const;
 
+    WTF_EXPORT_PRIVATE String convertToASCIILowercase() const;
+    WTF_EXPORT_PRIVATE String convertToASCIIUppercase() const;
+
     bool contains(UChar) const;
     WTF_EXPORT_PRIVATE bool containsIgnoringASCIICase(const StringView&) const;
     WTF_EXPORT_PRIVATE bool containsIgnoringASCIICase(const StringView&, unsigned startOffset) const;

Modified: trunk/Source/WTF/wtf/text/cf/StringViewCF.cpp (232301 => 232302)


--- trunk/Source/WTF/wtf/text/cf/StringViewCF.cpp	2018-05-30 18:00:27 UTC (rev 232301)
+++ trunk/Source/WTF/wtf/text/cf/StringViewCF.cpp	2018-05-30 19:21:34 UTC (rev 232302)
@@ -33,6 +33,14 @@
 
 namespace WTF {
 
+RetainPtr<CFStringRef> StringView::createCFString() const
+{
+    if (is8Bit())
+        return adoptCF(CFStringCreateWithBytes(kCFAllocatorDefault, characters8(), length(), kCFStringEncodingISOLatin1, false));
+    
+    return adoptCF(CFStringCreateWithCharacters(kCFAllocatorDefault, reinterpret_cast<const UniChar*>(characters16()), length()));
+}
+
 RetainPtr<CFStringRef> StringView::createCFStringWithoutCopying() const
 {
     if (is8Bit())

Modified: trunk/Source/WebCore/ChangeLog (232301 => 232302)


--- trunk/Source/WebCore/ChangeLog	2018-05-30 18:00:27 UTC (rev 232301)
+++ trunk/Source/WebCore/ChangeLog	2018-05-30 19:21:34 UTC (rev 232302)
@@ -1,3 +1,33 @@
+2018-05-30  Alex Christensen  <[email protected]>
+
+        Reduce String allocations
+        https://bugs.webkit.org/show_bug.cgi?id=186059
+
+        Reviewed by Darin Adler.
+
+        Don't allocate Strings just to convert it to another form.
+        Based mostly on Darin's feedback on bug 185986.
+
+        No change in behavior.
+
+        * Modules/websockets/WebSocketHandshake.cpp:
+        (WebCore::hostName):
+        (WebCore::WebSocketHandshake::host const):
+        * css/parser/CSSSelectorParser.cpp:
+        (WebCore::CSSSelectorParser::consumePseudo):
+        (WebCore::CSSSelectorParser::consumeANPlusB):
+        * loader/mac/LoaderNSURLExtras.mm:
+        (suggestedFilenameWithMIMEType):
+        * page/SecurityOriginData.h:
+        (WebCore::SecurityOriginData::fromURL):
+        * page/csp/ContentSecurityPolicySource.cpp:
+        (WebCore::wildcardMatches):
+        (WebCore::ContentSecurityPolicySource::hostMatches const):
+        * platform/URL.cpp:
+        (WebCore::URL::hostAndPort const):
+        * platform/network/cf/SocketStreamHandleImplCFNet.cpp:
+        (WebCore::SocketStreamHandleImpl::createStreams):
+
 2018-05-30  Jer Noble  <[email protected]>
 
         Media elements outside fullscreen should not be considered main content.

Modified: trunk/Source/WebCore/Modules/websockets/WebSocketHandshake.cpp (232301 => 232302)


--- trunk/Source/WebCore/Modules/websockets/WebSocketHandshake.cpp	2018-05-30 18:00:27 UTC (rev 232301)
+++ trunk/Source/WebCore/Modules/websockets/WebSocketHandshake.cpp	2018-05-30 19:21:34 UTC (rev 232302)
@@ -82,7 +82,7 @@
 {
     ASSERT(url.protocolIs("wss") == secure);
     StringBuilder builder;
-    builder.append(url.host().toString().convertToASCIILowercase());
+    builder.append(url.host().convertToASCIILowercase());
     if (url.port() && ((!secure && url.port().value() != 80) || (secure && url.port().value() != 443))) {
         builder.append(':');
         builder.appendNumber(url.port().value());
@@ -146,7 +146,7 @@
 // FIXME: Return type should just be String, not const String.
 const String WebSocketHandshake::host() const
 {
-    return m_url.host().toString().convertToASCIILowercase();
+    return m_url.host().convertToASCIILowercase();
 }
 
 const String& WebSocketHandshake::clientProtocol() const

Modified: trunk/Source/WebCore/css/parser/CSSSelectorParser.cpp (232301 => 232302)


--- trunk/Source/WebCore/css/parser/CSSSelectorParser.cpp	2018-05-30 18:00:27 UTC (rev 232301)
+++ trunk/Source/WebCore/css/parser/CSSSelectorParser.cpp	2018-05-30 19:21:34 UTC (rev 232302)
@@ -494,7 +494,7 @@
 
     std::unique_ptr<CSSParserSelector> selector;
     
-    auto lowercasedValue = token.value().toString().convertToASCIILowercase();
+    auto lowercasedValue = token.value().convertToASCIILowercase();
     auto value = StringView { lowercasedValue };
 
     if (colons == 1)
@@ -735,7 +735,7 @@
     } else if (token.type() == IdentToken) {
         if (token.value()[0] == '-') {
             result.first = -1;
-            nString = token.value().toString().substring(1);
+            nString = token.value().substring(1).toString();
         } else {
             result.first = 1;
             nString = token.value().toString();

Modified: trunk/Source/WebCore/loader/mac/LoaderNSURLExtras.mm (232301 => 232302)


--- trunk/Source/WebCore/loader/mac/LoaderNSURLExtras.mm	2018-05-30 18:00:27 UTC (rev 232301)
+++ trunk/Source/WebCore/loader/mac/LoaderNSURLExtras.mm	2018-05-30 19:21:34 UTC (rev 232302)
@@ -48,8 +48,8 @@
 
     if ([filename length] == 0 || [lastPathComponent isEqualToString:@"/"]) {
         // lastPathComponent is no good, try the host.
-        NSString *host = URL(url).host().toString();
-        filename = filenameByFixingIllegalCharacters(host);
+        auto host = URL(url).host().createNSString();
+        filename = filenameByFixingIllegalCharacters(host.get());
         if ([filename length] == 0) {
             // Can't make a filename using this URL, use "unknown".
             filename = copyImageUnknownFileLabel();

Modified: trunk/Source/WebCore/page/SecurityOriginData.h (232301 => 232302)


--- trunk/Source/WebCore/page/SecurityOriginData.h	2018-05-30 18:00:27 UTC (rev 232301)
+++ trunk/Source/WebCore/page/SecurityOriginData.h	2018-05-30 19:21:34 UTC (rev 232302)
@@ -49,8 +49,8 @@
     static SecurityOriginData fromURL(const URL& url)
     {
         return SecurityOriginData {
-            url.protocol().isNull() ? emptyString() : url.protocol().toString().convertToASCIILowercase(),
-            url.host().isNull() ? emptyString() : url.host().toString().convertToASCIILowercase(),
+            url.protocol().isNull() ? emptyString() : url.protocol().convertToASCIILowercase(),
+            url.host().isNull() ? emptyString() : url.host().convertToASCIILowercase(),
             url.port()
         };
     }

Modified: trunk/Source/WebCore/page/csp/ContentSecurityPolicySource.cpp (232301 => 232302)


--- trunk/Source/WebCore/page/csp/ContentSecurityPolicySource.cpp	2018-05-30 18:00:27 UTC (rev 232301)
+++ trunk/Source/WebCore/page/csp/ContentSecurityPolicySource.cpp	2018-05-30 19:21:34 UTC (rev 232302)
@@ -62,11 +62,19 @@
     return equalIgnoringASCIICase(url.protocol(), m_scheme);
 }
 
+static bool wildcardMatches(StringView host, const String& hostWithWildcard)
+{
+    auto hostLength = host.length();
+    auto hostWithWildcardLength = hostWithWildcard.length();
+    return host.endsWithIgnoringASCIICase(hostWithWildcard)
+        && hostLength > hostWithWildcardLength
+        && host[hostLength - hostWithWildcardLength - 1] == '.';
+}
+
 bool ContentSecurityPolicySource::hostMatches(const URL& url) const
 {
     auto host = url.host();
-    return equalIgnoringASCIICase(host, m_host) || (m_hostHasWildcard && host.endsWithIgnoringASCIICase(makeString(".", m_host)));
-
+    return equalIgnoringASCIICase(host, m_host) || (m_hostHasWildcard && wildcardMatches(host, m_host));
 }
 
 bool ContentSecurityPolicySource::pathMatches(const URL& url) const

Modified: trunk/Source/WebCore/platform/URL.cpp (232301 => 232302)


--- trunk/Source/WebCore/platform/URL.cpp	2018-05-30 18:00:27 UTC (rev 232301)
+++ trunk/Source/WebCore/platform/URL.cpp	2018-05-30 19:21:34 UTC (rev 232302)
@@ -39,6 +39,7 @@
 #include <wtf/UUID.h>
 #include <wtf/text/CString.h>
 #include <wtf/text/StringBuilder.h>
+#include <wtf/text/StringConcatenateNumbers.h>
 #include <wtf/text/StringHash.h>
 #include <wtf/text/TextStream.h>
 
@@ -180,7 +181,7 @@
 String URL::hostAndPort() const
 {
     if (auto port = this->port())
-        return makeString(host(), ':', String::number(port.value()));
+        return makeString(host(), ':', static_cast<unsigned>(port.value()));
     return host().toString();
 }
 

Modified: trunk/Source/WebCore/platform/network/cf/SocketStreamHandleImplCFNet.cpp (232301 => 232302)


--- trunk/Source/WebCore/platform/network/cf/SocketStreamHandleImplCFNet.cpp	2018-05-30 18:00:27 UTC (rev 232301)
+++ trunk/Source/WebCore/platform/network/cf/SocketStreamHandleImplCFNet.cpp	2018-05-30 19:21:34 UTC (rev 232302)
@@ -312,7 +312,7 @@
     if (m_connectionType == Unknown)
         return;
 
-    RetainPtr<CFStringRef> host = m_url.host().toString().createCFString();
+    RetainPtr<CFStringRef> host = m_url.host().createCFString();
 
     // Creating streams to final destination, not to proxy.
     CFReadStreamRef readStream = 0;
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to