Title: [101147] trunk/Source/_javascript_Core
Revision
101147
Author
msab...@apple.com
Date
2011-11-24 22:23:00 -0800 (Thu, 24 Nov 2011)

Log Message

Tune JSStringBuilder for 8 bit Strings
https://bugs.webkit.org/show_bug.cgi?id=72683

Changed JSStringBuilder to use 8 bit buffers until 16 bit data is added.
When 16 bit data is to be added, the 8 bit buffer is converted to 16 bit
and building continues with a 16 bit buffer.

Reviewed by Filip Pizlo.

* runtime/JSStringBuilder.h:
(JSC::JSStringBuilder::JSStringBuilder):
(JSC::JSStringBuilder::append):
(JSC::JSStringBuilder::upConvert):
(JSC::JSStringBuilder::build):
* runtime/UString.h:
(JSC::UString::adopt):
* wtf/text/StringImpl.h:
(WTF::StringImpl::adopt):

Modified Paths

Diff

Modified: trunk/Source/_javascript_Core/ChangeLog (101146 => 101147)


--- trunk/Source/_javascript_Core/ChangeLog	2011-11-24 23:23:20 UTC (rev 101146)
+++ trunk/Source/_javascript_Core/ChangeLog	2011-11-25 06:23:00 UTC (rev 101147)
@@ -1,3 +1,24 @@
+2011-11-24  Michael Saboff  <msab...@apple.com>
+
+        Tune JSStringBuilder for 8 bit Strings
+        https://bugs.webkit.org/show_bug.cgi?id=72683
+
+        Changed JSStringBuilder to use 8 bit buffers until 16 bit data is added.
+        When 16 bit data is to be added, the 8 bit buffer is converted to 16 bit
+        and building continues with a 16 bit buffer.
+
+        Reviewed by Filip Pizlo.
+
+        * runtime/JSStringBuilder.h:
+        (JSC::JSStringBuilder::JSStringBuilder):
+        (JSC::JSStringBuilder::append):
+        (JSC::JSStringBuilder::upConvert):
+        (JSC::JSStringBuilder::build):
+        * runtime/UString.h:
+        (JSC::UString::adopt):
+        * wtf/text/StringImpl.h:
+        (WTF::StringImpl::adopt):
+
 2011-11-24  Zeno Albisser  <z...@webkit.org>
 
         [Qt]WK2][Mac] Use Mac port's IPC implementation instead of Unix sockets

Modified: trunk/Source/_javascript_Core/runtime/JSStringBuilder.h (101146 => 101147)


--- trunk/Source/_javascript_Core/runtime/JSStringBuilder.h	2011-11-24 23:23:20 UTC (rev 101146)
+++ trunk/Source/_javascript_Core/runtime/JSStringBuilder.h	2011-11-25 06:23:00 UTC (rev 101147)
@@ -37,12 +37,21 @@
 public:
     JSStringBuilder()
         : m_okay(true)
+        , m_is8Bit(true)
     {
     }
 
     void append(const UChar u)
     {
-        m_okay &= buffer.tryAppend(&u, 1);
+        if (m_is8Bit) {
+            if (u < 0xff) {
+                LChar c = u;
+                m_okay &= buffer8.tryAppend(&c, 1);
+                return;
+            }
+            upConvert();
+        }
+        m_okay &= buffer16.tryAppend(&u, 1);
     }
 
     void append(const char* str)
@@ -52,36 +61,74 @@
 
     void append(const char* str, size_t len)
     {
-        m_okay &= buffer.tryReserveCapacity(buffer.size() + len);
+        if (m_is8Bit) {
+            m_okay &= buffer8.tryAppend(reinterpret_cast<const LChar*>(str), len);
+            return;
+        }
+        m_okay &= buffer8.tryReserveCapacity(buffer16.size() + len);
         for (size_t i = 0; i < len; i++) {
             UChar u = static_cast<unsigned char>(str[i]);
-            m_okay &= buffer.tryAppend(&u, 1);
+            m_okay &= buffer16.tryAppend(&u, 1);
         }
     }
 
     void append(const UChar* str, size_t len)
     {
-        m_okay &= buffer.tryAppend(str, len);
+        if (m_is8Bit)
+            upConvert(); // FIXME: We could check character by character its size.
+        m_okay &= buffer16.tryAppend(str, len);
     }
 
     void append(const UString& str)
     {
-        m_okay &= buffer.tryAppend(str.characters(), str.length());
+        unsigned length = str.length();
+
+        if (!length)
+            return;
+
+        if (m_is8Bit) {
+            if (str.is8Bit()) {
+                m_okay &= buffer8.tryAppend(str.characters8(), length);
+                return;
+            }
+            upConvert();
+        }
+        m_okay &= buffer16.tryAppend(str.characters(), length);
     }
 
+    void upConvert()
+    {
+        ASSERT(m_is8Bit);
+        size_t len = buffer8.size();
+
+        for (size_t i = 0; i < len; i++)
+            buffer16.append(buffer8[i]);
+
+        buffer8.clear();
+        m_is8Bit = false;
+    }
+
     JSValue build(ExecState* exec)
     {
         if (!m_okay)
             return throwOutOfMemoryError(exec);
-        buffer.shrinkToFit();
-        if (!buffer.data())
+        if (m_is8Bit) {
+            buffer8.shrinkToFit();
+            if (!buffer8.data())
+                return throwOutOfMemoryError(exec);
+            return jsString(exec, UString::adopt(buffer8));
+        }
+        buffer16.shrinkToFit();
+        if (!buffer16.data())
             return throwOutOfMemoryError(exec);
-        return jsString(exec, UString::adopt(buffer));
+        return jsString(exec, UString::adopt(buffer16));
     }
 
 protected:
-    Vector<UChar, 64> buffer;
+    Vector<LChar, 64> buffer8;
+    Vector<UChar, 64> buffer16;
     bool m_okay;
+    bool m_is8Bit;
 };
 
 template<typename StringType1, typename StringType2>

Modified: trunk/Source/_javascript_Core/runtime/UString.h (101146 => 101147)


--- trunk/Source/_javascript_Core/runtime/UString.h	2011-11-24 23:23:20 UTC (rev 101146)
+++ trunk/Source/_javascript_Core/runtime/UString.h	2011-11-25 06:23:00 UTC (rev 101147)
@@ -56,8 +56,8 @@
 
     void swap(UString& o) { m_impl.swap(o.m_impl); }
 
-    template<size_t inlineCapacity>
-    static UString adopt(Vector<UChar, inlineCapacity>& vector) { return StringImpl::adopt(vector); }
+    template<typename CharType, size_t inlineCapacity>
+    static UString adopt(Vector<CharType, inlineCapacity>& vector) { return StringImpl::adopt(vector); }
 
     bool isNull() const { return !m_impl; }
     bool isEmpty() const { return !m_impl || !m_impl->length(); }

Modified: trunk/Source/_javascript_Core/wtf/text/StringImpl.h (101146 => 101147)


--- trunk/Source/_javascript_Core/wtf/text/StringImpl.h	2011-11-24 23:23:20 UTC (rev 101146)
+++ trunk/Source/_javascript_Core/wtf/text/StringImpl.h	2011-11-25 06:23:00 UTC (rev 101147)
@@ -263,8 +263,8 @@
     static unsigned dataOffset() { return OBJECT_OFFSETOF(StringImpl, m_data8); }
     static PassRefPtr<StringImpl> createWithTerminatingNullCharacter(const StringImpl&);
 
-    template<size_t inlineCapacity>
-    static PassRefPtr<StringImpl> adopt(Vector<UChar, inlineCapacity>& vector)
+    template<typename CharType, size_t inlineCapacity>
+    static PassRefPtr<StringImpl> adopt(Vector<CharType, inlineCapacity>& vector)
     {
         if (size_t size = vector.size()) {
             ASSERT(vector.data());
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
http://lists.webkit.org/mailman/listinfo.cgi/webkit-changes

Reply via email to