Title: [209173] trunk/Source
Revision
209173
Author
[email protected]
Date
2016-11-30 20:32:50 -0800 (Wed, 30 Nov 2016)

Log Message

Roll out StringBuilder changes from the previous patch.
They were a slowdown on a Kraken JSON test.

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (209172 => 209173)


--- trunk/Source/_javascript_Core/ChangeLog	2016-12-01 03:56:12 UTC (rev 209172)
+++ trunk/Source/_javascript_Core/ChangeLog	2016-12-01 04:32:50 UTC (rev 209173)
@@ -1,3 +1,11 @@
+2016-11-30  Darin Adler  <[email protected]>
+
+        Roll out StringBuilder changes from the previous patch.
+        They were a slowdown on a Kraken JSON test.
+
+        * runtime/JSONObject.cpp:
+        Roll out changes from below.
+
 2016-11-30  Yusuke Suzuki  <[email protected]>
 
         [JSC] Specifying same module entry point multiple times cause TypeError

Modified: trunk/Source/_javascript_Core/runtime/JSONObject.cpp (209172 => 209173)


--- trunk/Source/_javascript_Core/runtime/JSONObject.cpp	2016-12-01 03:56:12 UTC (rev 209172)
+++ trunk/Source/_javascript_Core/runtime/JSONObject.cpp	2016-12-01 04:32:50 UTC (rev 209173)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009-2016 Apple Inc. All rights reserved.
+ * Copyright (C) 2009, 2016 Apple Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
  * modification, are permitted provided that the following conditions
@@ -354,7 +354,7 @@
     }
 
     if (value.isString()) {
-        builder.appendQuotedJSONString(asString(value)->viewWithUnderlyingString(*m_exec).view);
+        builder.appendQuotedJSONString(asString(value)->value(m_exec));
         return StringifySucceeded;
     }
 

Modified: trunk/Source/WTF/ChangeLog (209172 => 209173)


--- trunk/Source/WTF/ChangeLog	2016-12-01 03:56:12 UTC (rev 209172)
+++ trunk/Source/WTF/ChangeLog	2016-12-01 04:32:50 UTC (rev 209173)
@@ -1,5 +1,14 @@
 2016-11-30  Darin Adler  <[email protected]>
 
+        Roll out StringBuilder changes from the previous patch.
+        They were a slowdown on a Kraken JSON test.
+
+        * wtf/text/StringBuilder.cpp:
+        * wtf/text/StringBuilder.h:
+        Roll out changes from below.
+
+2016-11-30  Darin Adler  <[email protected]>
+
         Streamline and speed up tokenizer and segmented string classes
         https://bugs.webkit.org/show_bug.cgi?id=165003
 

Modified: trunk/Source/WTF/wtf/text/StringBuilder.cpp (209172 => 209173)


--- trunk/Source/WTF/wtf/text/StringBuilder.cpp	2016-12-01 03:56:12 UTC (rev 209172)
+++ trunk/Source/WTF/wtf/text/StringBuilder.cpp	2016-12-01 04:32:50 UTC (rev 209173)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2010-2016 Apple Inc. All rights reserved.
+ * Copyright (C) 2010, 2013, 2016 Apple Inc. All rights reserved.
  * Copyright (C) 2012 Google Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -29,6 +29,7 @@
 
 #include "IntegerToStringConversion.h"
 #include "MathExtras.h"
+#include "WTFString.h"
 #include <wtf/dtoa.h>
 
 namespace WTF {
@@ -39,18 +40,6 @@
     return std::max(requiredLength, std::max(minimumCapacity, capacity * 2));
 }
 
-template<> ALWAYS_INLINE LChar* StringBuilder::bufferCharacters<LChar>()
-{
-    ASSERT(m_is8Bit);
-    return m_bufferCharacters8;
-}
-
-template<> ALWAYS_INLINE UChar* StringBuilder::bufferCharacters<UChar>()
-{
-    ASSERT(!m_is8Bit);
-    return m_bufferCharacters16;
-}
-
 void StringBuilder::reifyString() const
 {
     // Check if the string already exists.
@@ -108,7 +97,6 @@
 void StringBuilder::allocateBuffer(const LChar* currentCharacters, unsigned requiredLength)
 {
     ASSERT(m_is8Bit);
-
     // Copy the existing data into a new buffer, set result to point to the end of the existing data.
     auto buffer = StringImpl::createUninitialized(requiredLength, m_bufferCharacters8);
     memcpy(m_bufferCharacters8, currentCharacters, static_cast<size_t>(m_length) * sizeof(LChar)); // This can't overflow.
@@ -124,7 +112,6 @@
 void StringBuilder::allocateBuffer(const UChar* currentCharacters, unsigned requiredLength)
 {
     ASSERT(!m_is8Bit);
-
     // Copy the existing data into a new buffer, set result to point to the end of the existing data.
     auto buffer = StringImpl::createUninitialized(requiredLength, m_bufferCharacters16);
     memcpy(m_bufferCharacters16, currentCharacters, static_cast<size_t>(m_length) * sizeof(UChar)); // This can't overflow.
@@ -137,11 +124,10 @@
 
 // Allocate a new 16 bit buffer, copying in currentCharacters (which is 8 bit and may come
 // from either m_string or m_buffer, neither will be reassigned until the copy has completed).
-void StringBuilder::allocateBufferUpconvert(const LChar* currentCharacters, unsigned requiredLength)
+void StringBuilder::allocateBufferUpConvert(const LChar* currentCharacters, unsigned requiredLength)
 {
     ASSERT(m_is8Bit);
     ASSERT(requiredLength >= m_length);
-
     // Copy the existing data into a new buffer, set result to point to the end of the existing data.
     auto buffer = StringImpl::createUninitialized(requiredLength, m_bufferCharacters16);
     for (unsigned i = 0; i < m_length; ++i)
@@ -155,7 +141,8 @@
     ASSERT(m_buffer->length() == requiredLength);
 }
 
-template<> void StringBuilder::reallocateBuffer<LChar>(unsigned requiredLength)
+template <>
+void StringBuilder::reallocateBuffer<LChar>(unsigned requiredLength)
 {
     // If the buffer has only one ref (by this StringBuilder), reallocate it,
     // otherwise fall back to "allocate and copy" method.
@@ -171,7 +158,8 @@
     ASSERT(m_buffer->length() == requiredLength);
 }
 
-template<> void StringBuilder::reallocateBuffer<UChar>(unsigned requiredLength)
+template <>
+void StringBuilder::reallocateBuffer<UChar>(unsigned requiredLength)
 {
     // If the buffer has only one ref (by this StringBuilder), reallocate it,
     // otherwise fall back to "allocate and copy" method.
@@ -178,7 +166,7 @@
     m_string = String();
     
     if (m_buffer->is8Bit())
-        allocateBufferUpconvert(m_buffer->characters8(), requiredLength);
+        allocateBufferUpConvert(m_buffer->characters8(), requiredLength);
     else if (m_buffer->hasOneRef())
         m_buffer = StringImpl::reallocate(m_buffer.releaseNonNull(), requiredLength, m_bufferCharacters16);
     else
@@ -200,7 +188,7 @@
         // Grow the string, if necessary.
         if (newCapacity > m_length) {
             if (!m_length) {
-                LChar* nullPlaceholder = nullptr;
+                LChar* nullPlaceholder = 0;
                 allocateBuffer(nullPlaceholder, newCapacity);
             } else if (m_string.is8Bit())
                 allocateBuffer(m_string.characters8(), newCapacity);
@@ -213,7 +201,8 @@
 
 // Make 'length' additional capacity be available in m_buffer, update m_string & m_length,
 // return a pointer to the newly allocated storage.
-template<typename CharacterType> ALWAYS_INLINE CharacterType* StringBuilder::appendUninitialized(unsigned length)
+template <typename CharType>
+ALWAYS_INLINE CharType* StringBuilder::appendUninitialized(unsigned length)
 {
     ASSERT(length);
 
@@ -228,15 +217,16 @@
         unsigned currentLength = m_length;
         m_string = String();
         m_length = requiredLength;
-        return bufferCharacters<CharacterType>() + currentLength;
+        return getBufferCharacters<CharType>() + currentLength;
     }
-
-    return appendUninitializedSlow<CharacterType>(requiredLength);
+    
+    return appendUninitializedSlow<CharType>(requiredLength);
 }
 
 // Make 'length' additional capacity be available in m_buffer, update m_string & m_length,
 // return a pointer to the newly allocated storage.
-template<typename CharacterType> CharacterType* StringBuilder::appendUninitializedSlow(unsigned requiredLength)
+template <typename CharType>
+CharType* StringBuilder::appendUninitializedSlow(unsigned requiredLength)
 {
     ASSERT(requiredLength);
 
@@ -243,38 +233,19 @@
     if (m_buffer) {
         // If the buffer is valid it must be at least as long as the current builder contents!
         ASSERT(m_buffer->length() >= m_length);
-        reallocateBuffer<CharacterType>(expandedCapacity(capacity(), requiredLength));
+        
+        reallocateBuffer<CharType>(expandedCapacity(capacity(), requiredLength));
     } else {
         ASSERT(m_string.length() == m_length);
-        allocateBuffer(m_length ? m_string.characters<CharacterType>() : nullptr, expandedCapacity(capacity(), requiredLength));
+        allocateBuffer(m_length ? m_string.characters<CharType>() : 0, expandedCapacity(capacity(), requiredLength));
     }
     
-    auto* result = bufferCharacters<CharacterType>() + m_length;
+    CharType* result = getBufferCharacters<CharType>() + m_length;
     m_length = requiredLength;
     ASSERT(m_buffer->length() >= m_length);
     return result;
 }
 
-inline UChar* StringBuilder::appendUninitializedUpconvert(unsigned length)
-{
-    unsigned requiredLength = length + m_length;
-    if (requiredLength < length)
-        CRASH();
-
-    if (m_buffer) {
-        // If the buffer is valid it must be at least as long as the current builder contents!
-        ASSERT(m_buffer->length() >= m_length);
-        allocateBufferUpconvert(m_buffer->characters8(), expandedCapacity(capacity(), requiredLength));
-    } else {
-        ASSERT(m_string.length() == m_length);
-        allocateBufferUpconvert(m_string.isNull() ? nullptr : m_string.characters8(), expandedCapacity(capacity(), requiredLength));
-    }
-
-    auto* result = m_bufferCharacters16 + m_length;
-    m_length = requiredLength;
-    return result;
-}
-
 void StringBuilder::append(const UChar* characters, unsigned length)
 {
     if (!length)
@@ -283,16 +254,32 @@
     ASSERT(characters);
 
     if (m_is8Bit) {
-        if (length == 1 && !(*characters & ~0xFF)) {
+        if (length == 1 && !(*characters & ~0xff)) {
             // Append as 8 bit character
             LChar lChar = static_cast<LChar>(*characters);
             append(&lChar, 1);
             return;
         }
-        memcpy(appendUninitializedUpconvert(length), characters, static_cast<size_t>(length) * sizeof(UChar));
+
+        // Calculate the new size of the builder after appending.
+        unsigned requiredLength = length + m_length;
+        if (requiredLength < length)
+            CRASH();
+        
+        if (m_buffer) {
+            // If the buffer is valid it must be at least as long as the current builder contents!
+            ASSERT(m_buffer->length() >= m_length);
+            
+            allocateBufferUpConvert(m_buffer->characters8(), expandedCapacity(capacity(), requiredLength));
+        } else {
+            ASSERT(m_string.length() == m_length);
+            allocateBufferUpConvert(m_string.isNull() ? 0 : m_string.characters8(), expandedCapacity(capacity(), requiredLength));
+        }
+
+        memcpy(m_bufferCharacters16 + m_length, characters, static_cast<size_t>(length) * sizeof(UChar));
+        m_length = requiredLength;
     } else
         memcpy(appendUninitialized<UChar>(length), characters, static_cast<size_t>(length) * sizeof(UChar));
-
     ASSERT(m_buffer->length() >= m_length);
 }
 
@@ -303,22 +290,19 @@
     ASSERT(characters);
 
     if (m_is8Bit) {
-        auto* destination = appendUninitialized<LChar>(length);
-        // FIXME: How did we determine a threshold of 8 here was the right one?
-        // Also, this kind of optimization could be useful anywhere else we have a
-        // performance-sensitive code path that calls memcpy.
+        LChar* dest = appendUninitialized<LChar>(length);
         if (length > 8)
-            memcpy(destination, characters, length);
+            memcpy(dest, characters, static_cast<size_t>(length) * sizeof(LChar));
         else {
             const LChar* end = characters + length;
             while (characters < end)
-                *destination++ = *characters++;
+                *(dest++) = *(characters++);
         }
     } else {
-        auto* destination = appendUninitialized<UChar>(length);
+        UChar* dest = appendUninitialized<UChar>(length);
         const LChar* end = characters + length;
         while (characters < end)
-            *destination++ = *characters++;
+            *(dest++) = *(characters++);
     }
 }
 
@@ -401,58 +385,17 @@
     }
 }
 
-template<typename LengthType, typename CharacterType> static LengthType quotedJSONStringLength(const CharacterType* input, unsigned length)
+template <typename OutputCharacterType, typename InputCharacterType>
+static void appendQuotedJSONStringInternal(OutputCharacterType*& output, const InputCharacterType* input, unsigned length)
 {
-    LengthType quotedLength = 2;
-    for (unsigned i = 0; i < length; ++i) {
-        auto character = input[i];
-        if (LIKELY(character > 0x1F)) {
-            switch (character) {
-            case '"':
-            case '\\':
-                quotedLength += 2;
-                break;
-            default:
-                ++quotedLength;
-                break;
-            }
-        } else {
-            switch (character) {
-            case '\t':
-            case '\r':
-            case '\n':
-            case '\f':
-            case '\b':
-                quotedLength += 2;
-                break;
-            default:
-                quotedLength += 6;
-            }
-        }
-    }
-    return quotedLength;
-}
-
-template<typename CharacterType> static inline unsigned quotedJSONStringLength(const CharacterType* input, unsigned length)
-{
-    constexpr auto maxSafeLength = (std::numeric_limits<unsigned>::max() - 2) / 6;
-    if (length <= maxSafeLength)
-        return quotedJSONStringLength<unsigned>(input, length);
-    return quotedJSONStringLength<Checked<unsigned>>(input, length).unsafeGet();
-}
-
-template<typename OutputCharacterType, typename InputCharacterType> static inline void appendQuotedJSONStringInternal(OutputCharacterType* output, const InputCharacterType* input, unsigned length)
-{
-    *output++ = '"';
-    for (unsigned i = 0; i < length; ++i) {
-        auto character = input[i];
-        if (LIKELY(character > 0x1F)) {
-            if (UNLIKELY(character == '"' || character == '\\'))
+    for (const InputCharacterType* end = input + length; input != end; ++input) {
+        if (LIKELY(*input > 0x1F)) {
+            if (*input == '"' || *input == '\\')
                 *output++ = '\\';
-            *output++ = character;
+            *output++ = *input;
             continue;
         }
-        switch (character) {
+        switch (*input) {
         case '\t':
             *output++ = '\\';
             *output++ = 't';
@@ -474,35 +417,56 @@
             *output++ = 'b';
             break;
         default:
-            ASSERT(!(character & ~0xFF));
+            ASSERT((*input & 0xFF00) == 0);
+            static const char hexDigits[] = "0123456789abcdef";
             *output++ = '\\';
             *output++ = 'u';
             *output++ = '0';
             *output++ = '0';
-            *output++ = upperNibbleToLowercaseASCIIHexDigit(character);
-            *output++ = lowerNibbleToLowercaseASCIIHexDigit(character);
+            *output++ = static_cast<LChar>(hexDigits[(*input >> 4) & 0xF]);
+            *output++ = static_cast<LChar>(hexDigits[*input & 0xF]);
             break;
         }
     }
-    *output = '"';
 }
 
-void StringBuilder::appendQuotedJSONString(StringView string)
+void StringBuilder::appendQuotedJSONString(const String& string)
 {
-    unsigned length = string.length();
-    if (string.is8Bit()) {
-        auto* characters = string.characters8();
-        if (m_is8Bit)
-            appendQuotedJSONStringInternal(appendUninitialized<LChar>(quotedJSONStringLength(characters, length)), characters, length);
-        else
-            appendQuotedJSONStringInternal(appendUninitialized<UChar>(quotedJSONStringLength(characters, length)), characters, length);
+    // Make sure we have enough buffer space to append this string without having
+    // to worry about reallocating in the middle.
+    // The 2 is for the '"' quotes on each end.
+    // The 6 is for characters that need to be \uNNNN encoded.
+    Checked<unsigned> stringLength = string.length();
+    Checked<unsigned> maximumCapacityRequired = length();
+    maximumCapacityRequired += 2 + stringLength * 6;
+    unsigned allocationSize = maximumCapacityRequired.unsafeGet();
+    // This max() is here to allow us to allocate sizes between the range [2^31, 2^32 - 2] because roundUpToPowerOfTwo(1<<31 + some int smaller than 1<<31) == 0.
+    allocationSize = std::max(allocationSize, roundUpToPowerOfTwo(allocationSize));
+
+    if (is8Bit() && !string.is8Bit())
+        allocateBufferUpConvert(m_bufferCharacters8, allocationSize);
+    else
+        reserveCapacity(allocationSize);
+    ASSERT(m_buffer->length() >= allocationSize);
+
+    if (is8Bit()) {
+        ASSERT(string.is8Bit());
+        LChar* output = m_bufferCharacters8 + m_length;
+        *output++ = '"';
+        appendQuotedJSONStringInternal(output, string.characters8(), string.length());
+        *output++ = '"';
+        m_length = output - m_bufferCharacters8;
     } else {
-        auto* characters = string.characters16();
-        if (m_is8Bit)
-            appendQuotedJSONStringInternal(appendUninitializedUpconvert(quotedJSONStringLength(characters, length)), characters, length);
+        UChar* output = m_bufferCharacters16 + m_length;
+        *output++ = '"';
+        if (string.is8Bit())
+            appendQuotedJSONStringInternal(output, string.characters8(), string.length());
         else
-            appendQuotedJSONStringInternal(appendUninitialized<UChar>(quotedJSONStringLength(characters, length)), characters, length);
+            appendQuotedJSONStringInternal(output, string.characters16(), string.length());
+        *output++ = '"';
+        m_length = output - m_bufferCharacters16;
     }
+    ASSERT(m_buffer->length() >= m_length);
 }
 
 } // namespace WTF

Modified: trunk/Source/WTF/wtf/text/StringBuilder.h (209172 => 209173)


--- trunk/Source/WTF/wtf/text/StringBuilder.h	2016-12-01 03:56:12 UTC (rev 209172)
+++ trunk/Source/WTF/wtf/text/StringBuilder.h	2016-12-01 04:32:50 UTC (rev 209173)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009-2016 Apple Inc. All rights reserved.
+ * Copyright (C) 2009-2010, 2012-2013, 2016 Apple Inc. All rights reserved.
  * Copyright (C) 2012 Google Inc. All rights reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -24,18 +24,26 @@
  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
  */
 
-#pragma once
+#ifndef StringBuilder_h
+#define StringBuilder_h
 
+#include <wtf/text/AtomicString.h>
 #include <wtf/text/StringView.h>
+#include <wtf/text/WTFString.h>
 
 namespace WTF {
 
 class StringBuilder {
-    // Disallow copying since it's expensive and we don't want anyone to do it by accident.
+    // Disallow copying since it's expensive and we don't want code to do it by accident.
     WTF_MAKE_NONCOPYABLE(StringBuilder);
 
 public:
-    StringBuilder() = default;
+    StringBuilder()
+        : m_length(0)
+        , m_is8Bit(true)
+        , m_bufferCharacters8(0)
+    {
+    }
 
     WTF_EXPORT_PRIVATE void append(const UChar*, unsigned);
     WTF_EXPORT_PRIVATE void append(const LChar*, unsigned);
@@ -42,27 +50,29 @@
 
     ALWAYS_INLINE void append(const char* characters, unsigned length) { append(reinterpret_cast<const LChar*>(characters), length); }
 
-    void append(const AtomicString& atomicString) { append(atomicString.string()); }
+    void append(const AtomicString& atomicString)
+    {
+        append(atomicString.string());
+    }
 
     void append(const String& string)
     {
-        unsigned length = string.length();
-        if (!length)
+        if (!string.length())
             return;
 
-        // If we're appending to an empty string, and there is not a buffer
-        // (reserveCapacity has not been called) then just retain the string.
+        // If we're appending to an empty string, and there is not a buffer (reserveCapacity has not been called)
+        // then just retain the string.
         if (!m_length && !m_buffer) {
             m_string = string;
-            m_length = length;
-            m_is8Bit = string.is8Bit();
+            m_length = string.length();
+            m_is8Bit = m_string.is8Bit();
             return;
         }
 
         if (string.is8Bit())
-            append(string.characters8(), length);
+            append(string.characters8(), string.length());
         else
-            append(string.characters16(), length);
+            append(string.characters16(), string.length());
     }
 
     void append(const StringBuilder& other)
@@ -70,12 +80,11 @@
         if (!other.m_length)
             return;
 
-        // If we're appending to an empty string, and there is not a buffer
-        // (reserveCapacity has not been called) then just retain the string.
+        // If we're appending to an empty string, and there is not a buffer (reserveCapacity has not been called)
+        // then just retain the string.
         if (!m_length && !m_buffer && !other.m_string.isNull()) {
             m_string = other.m_string;
             m_length = other.m_length;
-            m_is8Bit = other.m_is8Bit;
             return;
         }
 
@@ -96,7 +105,6 @@
 #if USE(CF)
     WTF_EXPORT_PRIVATE void append(CFStringRef);
 #endif
-
 #if USE(CF) && defined(__OBJC__)
     void append(NSString *string) { append((__bridge CFStringRef)string); }
 #endif
@@ -103,20 +111,11 @@
     
     void append(const String& string, unsigned offset, unsigned length)
     {
-        ASSERT(offset <= string.length());
-        ASSERT(offset + length <= string.length());
-
-        if (!length)
+        if (!string.length())
             return;
 
-        // If we're appending to an empty string, and there is not a buffer
-        // (reserveCapacity has not been called) then just retain the string.
-        if (!offset && !m_length && !m_buffer && length == string.length()) {
-            m_string = string;
-            m_length = length;
-            m_is8Bit = string.is8Bit();
+        if ((offset + length) > string.length())
             return;
-        }
 
         if (string.is8Bit())
             append(string.characters8() + offset, length);
@@ -130,33 +129,37 @@
             append(characters, strlen(characters));
     }
 
-    void append(UChar character)
+    void append(UChar c)
     {
         if (m_buffer && m_length < m_buffer->length() && m_string.isNull()) {
             if (!m_is8Bit) {
-                m_bufferCharacters16[m_length++] = character;
+                m_bufferCharacters16[m_length++] = c;
                 return;
             }
-            if (!(character & ~0xFF)) {
-                m_bufferCharacters8[m_length++] = static_cast<LChar>(character);
+
+            if (!(c & ~0xff)) {
+                m_bufferCharacters8[m_length++] = static_cast<LChar>(c);
                 return;
             }
         }
-        append(&character, 1);
+        append(&c, 1);
     }
 
-    void append(LChar character)
+    void append(LChar c)
     {
         if (m_buffer && m_length < m_buffer->length() && m_string.isNull()) {
             if (m_is8Bit)
-                m_bufferCharacters8[m_length++] = character;
+                m_bufferCharacters8[m_length++] = c;
             else
-                m_bufferCharacters16[m_length++] = character;
+                m_bufferCharacters16[m_length++] = c;
         } else
-            append(&character, 1);
+            append(&c, 1);
     }
 
-    void append(char character) { append(static_cast<LChar>(character)); }
+    void append(char c)
+    {
+        append(static_cast<LChar>(c));
+    }
 
     void append(UChar32 c)
     {
@@ -168,9 +171,10 @@
         append(U16_TRAIL(c));
     }
 
-    WTF_EXPORT_PRIVATE void appendQuotedJSONString(StringView);
+    WTF_EXPORT_PRIVATE void appendQuotedJSONString(const String&);
 
-    template<unsigned charactersCount> ALWAYS_INLINE void appendLiteral(const char (&characters)[charactersCount]) { append(characters, charactersCount - 1); }
+    template<unsigned charactersCount>
+    ALWAYS_INLINE void appendLiteral(const char (&characters)[charactersCount]) { append(characters, charactersCount - 1); }
 
     WTF_EXPORT_PRIVATE void appendNumber(int);
     WTF_EXPORT_PRIVATE void appendNumber(unsigned int);
@@ -216,15 +220,24 @@
         return AtomicString(m_buffer.get(), 0, m_length);
     }
 
-    unsigned length() const { return m_length; }
+    unsigned length() const
+    {
+        return m_length;
+    }
+
     bool isEmpty() const { return !m_length; }
 
     WTF_EXPORT_PRIVATE void reserveCapacity(unsigned newCapacity);
 
-    unsigned capacity() const { return m_buffer ? m_buffer->length() : m_length; }
+    unsigned capacity() const
+    {
+        return m_buffer ? m_buffer->length() : m_length;
+    }
 
     WTF_EXPORT_PRIVATE void resize(unsigned newSize);
+
     WTF_EXPORT_PRIVATE bool canShrink() const;
+
     WTF_EXPORT_PRIVATE void shrinkToFit();
 
     UChar operator[](unsigned i) const
@@ -281,36 +294,43 @@
 private:
     void allocateBuffer(const LChar* currentCharacters, unsigned requiredLength);
     void allocateBuffer(const UChar* currentCharacters, unsigned requiredLength);
-    void allocateBufferUpconvert(const LChar* currentCharacters, unsigned requiredLength);
-    template<typename CharacterType> void reallocateBuffer(unsigned requiredLength);
-    UChar* appendUninitializedUpconvert(unsigned length);
-    template<typename CharacterType> CharacterType* appendUninitialized(unsigned length);
-    template<typename CharacterType> CharacterType* appendUninitializedSlow(unsigned length);
-    template<typename CharacterType> CharacterType* bufferCharacters();
+    void allocateBufferUpConvert(const LChar* currentCharacters, unsigned requiredLength);
+    template <typename CharType>
+    void reallocateBuffer(unsigned requiredLength);
+    template <typename CharType>
+    ALWAYS_INLINE CharType* appendUninitialized(unsigned length);
+    template <typename CharType>
+    CharType* appendUninitializedSlow(unsigned length);
+    template <typename CharType>
+    ALWAYS_INLINE CharType * getBufferCharacters();
     WTF_EXPORT_PRIVATE void reifyString() const;
 
-    unsigned m_length { 0 };
+    unsigned m_length;
     mutable String m_string;
     RefPtr<StringImpl> m_buffer;
-    bool m_is8Bit { true };
+    bool m_is8Bit;
     union {
-        LChar* m_bufferCharacters8 { nullptr };
+        LChar* m_bufferCharacters8;
         UChar* m_bufferCharacters16;
     };
 };
 
-template<typename StringType> bool equal(const StringBuilder&, const StringType&);
-bool equal(const StringBuilder&, const String&); // Only needed because is8Bit dereferences nullptr when the string is null.
-template<typename CharacterType> bool equal(const StringBuilder&, const CharacterType*, unsigned length);
+template <>
+ALWAYS_INLINE LChar* StringBuilder::getBufferCharacters<LChar>()
+{
+    ASSERT(m_is8Bit);
+    return m_bufferCharacters8;
+}
 
-bool operator==(const StringBuilder&, const StringBuilder&);
-bool operator!=(const StringBuilder&, const StringBuilder&);
-bool operator==(const StringBuilder&, const String&);
-bool operator!=(const StringBuilder&, const String&);
-bool operator==(const String&, const StringBuilder&);
-bool operator!=(const String&, const StringBuilder&);
+template <>
+ALWAYS_INLINE UChar* StringBuilder::getBufferCharacters<UChar>()
+{
+    ASSERT(!m_is8Bit);
+    return m_bufferCharacters16;
+}    
 
-template<typename CharacterType> inline bool equal(const StringBuilder& s, const CharacterType* buffer, unsigned length)
+template <typename CharType>
+bool equal(const StringBuilder& s, const CharType* buffer, unsigned length)
 {
     if (s.length() != length)
         return false;
@@ -321,18 +341,24 @@
     return equal(s.characters16(), buffer, length);
 }
 
-template<typename StringType> inline bool equal(const StringBuilder& a, const StringType& b)
+template <typename StringType>
+bool equal(const StringBuilder& a, const StringType& b)
 {
-    return equalCommon(a, b);
-}
+    if (a.length() != b.length())
+        return false;
 
-inline bool equal(const StringBuilder& a, const String& b)
-{
-    // FIXME: This preserves historic behavior where an empty StringBuilder compares as equal
-    // to a null String. This does not seem like desirable behavior since a null String and
-    // an empty String do not compare as equal, but we have regression tests expecting it,
-    // and there is a slim chance we also have code depending on it.
-    return b.isNull() ? a.isEmpty() : equalCommon(a, b);
+    if (!a.length())
+        return true;
+
+    if (a.is8Bit()) {
+        if (b.is8Bit())
+            return equal(a.characters8(), b.characters8(), a.length());
+        return equal(a.characters8(), b.characters16(), a.length());
+    }
+
+    if (b.is8Bit())
+        return equal(a.characters16(), b.characters8(), a.length());
+    return equal(a.characters16(), b.characters16(), a.length());
 }
 
 inline bool operator==(const StringBuilder& a, const StringBuilder& b) { return equal(a, b); }
@@ -345,3 +371,5 @@
 } // namespace WTF
 
 using WTF::StringBuilder;
+
+#endif // StringBuilder_h
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to