<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[52026] trunk/JavaScriptCore</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta">
<dt>Revision</dt> <dd><a href="http://trac.webkit.org/projects/webkit/changeset/52026">52026</a></dd>
<dt>Author</dt> <dd>barraclough@apple.com</dd>
<dt>Date</dt> <dd>2009-12-11 14:57:39 -0800 (Fri, 11 Dec 2009)</dd>
</dl>

<h3>Log Message</h3>
<pre>https://bugs.webkit.org/show_bug.cgi?id=32400
Switch remaining cases of string addition to use ropes.

Reviewed by Oliver Hunt.

Re-landing <a href="http://trac.webkit.org/projects/webkit/changeset/51975">r51975</a> - added toPrimitiveString method,
performs toPrimitive then subsequent toString operations.

~1% progression on Sunspidey.

* jit/JITStubs.cpp:
(JSC::DEFINE_STUB_FUNCTION):
* runtime/JSString.h:
(JSC::JSString::JSString):
(JSC::JSString::appendStringInConstruct):
* runtime/Operations.cpp:
(JSC::jsAddSlowCase):
* runtime/Operations.h:
(JSC::jsString):
(JSC::jsAdd):</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkJavaScriptCoreChangeLog">trunk/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkJavaScriptCorejitJITStubscpp">trunk/JavaScriptCore/jit/JITStubs.cpp</a></li>
<li><a href="#trunkJavaScriptCoreruntimeJSStringh">trunk/JavaScriptCore/runtime/JSString.h</a></li>
<li><a href="#trunkJavaScriptCoreruntimeJSValueh">trunk/JavaScriptCore/runtime/JSValue.h</a></li>
<li><a href="#trunkJavaScriptCoreruntimeOperationscpp">trunk/JavaScriptCore/runtime/Operations.cpp</a></li>
<li><a href="#trunkJavaScriptCoreruntimeOperationsh">trunk/JavaScriptCore/runtime/Operations.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/JavaScriptCore/ChangeLog (52025 => 52026)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JavaScriptCore/ChangeLog        2009-12-11 22:39:29 UTC (rev 52025)
+++ trunk/JavaScriptCore/ChangeLog        2009-12-11 22:57:39 UTC (rev 52026)
</span><span class="lines">@@ -1,3 +1,26 @@
</span><ins>+2009-12-10  Gavin Barraclough  &lt;barraclough@apple.com&gt;
+
+        Reviewed by Oliver Hunt.
+
+        https://bugs.webkit.org/show_bug.cgi?id=32400
+        Switch remaining cases of string addition to use ropes.
+
+        Re-landing r51975 - added toPrimitiveString method,
+        performs toPrimitive then subsequent toString operations.
+
+        ~1% progression on Sunspidey.
+
+        * jit/JITStubs.cpp:
+        (JSC::DEFINE_STUB_FUNCTION):
+        * runtime/JSString.h:
+        (JSC::JSString::JSString):
+        (JSC::JSString::appendStringInConstruct):
+        * runtime/Operations.cpp:
+        (JSC::jsAddSlowCase):
+        * runtime/Operations.h:
+        (JSC::jsString):
+        (JSC::jsAdd):
+
</ins><span class="cx"> 2009-12-11  Adam Roben  &lt;aroben@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         Windows build fix
</span></span></pre></div>
<a id="trunkJavaScriptCorejitJITStubscpp"></a>
<div class="modfile"><h4>Modified: trunk/JavaScriptCore/jit/JITStubs.cpp (52025 => 52026)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JavaScriptCore/jit/JITStubs.cpp        2009-12-11 22:39:29 UTC (rev 52025)
+++ trunk/JavaScriptCore/jit/JITStubs.cpp        2009-12-11 22:57:39 UTC (rev 52026)
</span><span class="lines">@@ -1033,35 +1033,20 @@
</span><span class="cx"> 
</span><span class="cx">     JSValue v1 = stackFrame.args[0].jsValue();
</span><span class="cx">     JSValue v2 = stackFrame.args[1].jsValue();
</span><del>-
-    double left;
-    double right = 0.0;
-
-    bool rightIsNumber = v2.getNumber(right);
-    if (rightIsNumber &amp;&amp; v1.getNumber(left))
-        return JSValue::encode(jsNumber(stackFrame.globalData, left + right));
-    
</del><span class="cx">     CallFrame* callFrame = stackFrame.callFrame;
</span><span class="cx"> 
</span><del>-    bool leftIsString = v1.isString();
-    if (leftIsString &amp;&amp; v2.isString()) {
-        JSValue result = jsString(callFrame, asString(v1), asString(v2));
</del><ins>+    if (v1.isString()) {
+        JSValue result = v2.isString()
+            ? jsString(callFrame, asString(v1), asString(v2))
+            : jsString(callFrame, asString(v1), v2.toPrimitiveString(callFrame));
</ins><span class="cx">         CHECK_FOR_EXCEPTION_AT_END();
</span><span class="cx">         return JSValue::encode(result);
</span><span class="cx">     }
</span><span class="cx"> 
</span><del>-    if (rightIsNumber &amp; leftIsString) {
-        RefPtr&lt;UString::Rep&gt; value = v2.isInt32() ?
-            concatenate(asString(v1)-&gt;value(callFrame).rep(), v2.asInt32()) :
-            concatenate(asString(v1)-&gt;value(callFrame).rep(), right);
</del><ins>+    double left = 0.0, right;
+    if (v1.getNumber(left) &amp;&amp; v2.getNumber(right))
+        return JSValue::encode(jsNumber(stackFrame.globalData, left + right));
</ins><span class="cx"> 
</span><del>-        if (UNLIKELY(!value)) {
-            throwOutOfMemoryError(callFrame);
-            VM_THROW_EXCEPTION();
-        }
-        return JSValue::encode(jsString(stackFrame.globalData, value.release()));
-    }
-
</del><span class="cx">     // All other cases are pretty uncommon
</span><span class="cx">     JSValue result = jsAddSlowCase(callFrame, v1, v2);
</span><span class="cx">     CHECK_FOR_EXCEPTION_AT_END();
</span></span></pre></div>
<a id="trunkJavaScriptCoreruntimeJSStringh"></a>
<div class="modfile"><h4>Modified: trunk/JavaScriptCore/runtime/JSString.h (52025 => 52026)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JavaScriptCore/runtime/JSString.h        2009-12-11 22:39:29 UTC (rev 52025)
+++ trunk/JavaScriptCore/runtime/JSString.h        2009-12-11 22:57:39 UTC (rev 52026)
</span><span class="lines">@@ -203,6 +203,32 @@
</span><span class="cx">             appendStringInConstruct(index, s2);
</span><span class="cx">             ASSERT(ropeLength == index);
</span><span class="cx">         }
</span><ins>+        // This constructor constructs a new string by concatenating s1 &amp; s2.
+        // This should only be called with ropeLength &lt;= 3.
+        JSString(JSGlobalData* globalData, unsigned ropeLength, JSString* s1, const UString&amp; u2)
+            : JSCell(globalData-&gt;stringStructure.get())
+            , m_stringLength(s1-&gt;length() + u2.size())
+            , m_ropeLength(ropeLength)
+        {
+            ASSERT(ropeLength &lt;= s_maxInternalRopeLength);
+            unsigned index = 0;
+            appendStringInConstruct(index, s1);
+            appendStringInConstruct(index, u2);
+            ASSERT(ropeLength == index);
+        }
+        // This constructor constructs a new string by concatenating s1 &amp; s2.
+        // This should only be called with ropeLength &lt;= 3.
+        JSString(JSGlobalData* globalData, unsigned ropeLength, const UString&amp; u1, JSString* s2)
+            : JSCell(globalData-&gt;stringStructure.get())
+            , m_stringLength(u1.size() + s2-&gt;length())
+            , m_ropeLength(ropeLength)
+        {
+            ASSERT(ropeLength &lt;= s_maxInternalRopeLength);
+            unsigned index = 0;
+            appendStringInConstruct(index, u1);
+            appendStringInConstruct(index, s2);
+            ASSERT(ropeLength == index);
+        }
</ins><span class="cx">         // This constructor constructs a new string by concatenating v1, v2 &amp; v3.
</span><span class="cx">         // This should only be called with ropeLength &lt;= 3 ... which since every
</span><span class="cx">         // value must require a ropeLength of at least one implies that the length
</span><span class="lines">@@ -258,13 +284,18 @@
</span><span class="cx"> 
</span><span class="cx">         void resolveRope(ExecState*) const;
</span><span class="cx"> 
</span><ins>+        void appendStringInConstruct(unsigned&amp; index, const UString&amp; string)
+        {
+            m_fibers[index++] = Rope::Fiber(string.rep()-&gt;ref());
+        }
+
</ins><span class="cx">         void appendStringInConstruct(unsigned&amp; index, JSString* jsString)
</span><span class="cx">         {
</span><span class="cx">             if (jsString-&gt;isRope()) {
</span><span class="cx">                 for (unsigned i = 0; i &lt; jsString-&gt;m_ropeLength; ++i)
</span><span class="cx">                     m_fibers[index++] = jsString-&gt;m_fibers[i].ref();
</span><span class="cx">             } else
</span><del>-                m_fibers[index++] = Rope::Fiber(jsString-&gt;string().rep()-&gt;ref());
</del><ins>+                appendStringInConstruct(index, jsString-&gt;string());
</ins><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         void appendValueInConstructAndIncrementLength(ExecState* exec, unsigned&amp; index, JSValue v)
</span><span class="lines">@@ -311,6 +342,8 @@
</span><span class="cx">         unsigned ropeLength() { return m_ropeLength ? m_ropeLength : 1; }
</span><span class="cx"> 
</span><span class="cx">         friend JSValue jsString(ExecState* exec, JSString* s1, JSString* s2);
</span><ins>+        friend JSValue jsString(ExecState* exec, const UString&amp; u1, JSString* s2);
+        friend JSValue jsString(ExecState* exec, JSString* s1, const UString&amp; u2);
</ins><span class="cx">         friend JSValue jsString(ExecState* exec, Register* strings, unsigned count);
</span><span class="cx">     };
</span><span class="cx"> 
</span><span class="lines">@@ -469,6 +502,26 @@
</span><span class="cx">         return asCell()-&gt;toString(exec);
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    inline UString JSValue::toPrimitiveString(ExecState* exec) const
+    {
+        if (isString())
+            return static_cast&lt;JSString*&gt;(asCell())-&gt;value(exec);
+        if (isInt32())
+            return exec-&gt;globalData().numericStrings.add(asInt32());
+        if (isDouble())
+            return exec-&gt;globalData().numericStrings.add(asDouble());
+        if (isTrue())
+            return &quot;true&quot;;
+        if (isFalse())
+            return &quot;false&quot;;
+        if (isNull())
+            return &quot;null&quot;;
+        if (isUndefined())
+            return &quot;undefined&quot;;
+        ASSERT(isCell());
+        return asCell()-&gt;toPrimitive(exec, NoPreference).toString(exec);
+    }
+
</ins><span class="cx"> } // namespace JSC
</span><span class="cx"> 
</span><span class="cx"> #endif // JSString_h
</span></span></pre></div>
<a id="trunkJavaScriptCoreruntimeJSValueh"></a>
<div class="modfile"><h4>Modified: trunk/JavaScriptCore/runtime/JSValue.h (52025 => 52026)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JavaScriptCore/runtime/JSValue.h        2009-12-11 22:39:29 UTC (rev 52025)
+++ trunk/JavaScriptCore/runtime/JSValue.h        2009-12-11 22:57:39 UTC (rev 52026)
</span><span class="lines">@@ -158,6 +158,7 @@
</span><span class="cx">         double toNumber(ExecState*) const;
</span><span class="cx">         JSValue toJSNumber(ExecState*) const; // Fast path for when you expect that the value is an immediate number.
</span><span class="cx">         UString toString(ExecState*) const;
</span><ins>+        UString toPrimitiveString(ExecState*) const;
</ins><span class="cx">         JSObject* toObject(ExecState*) const;
</span><span class="cx"> 
</span><span class="cx">         // Integer conversions.
</span></span></pre></div>
<a id="trunkJavaScriptCoreruntimeOperationscpp"></a>
<div class="modfile"><h4>Modified: trunk/JavaScriptCore/runtime/Operations.cpp (52025 => 52026)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JavaScriptCore/runtime/Operations.cpp        2009-12-11 22:39:29 UTC (rev 52025)
+++ trunk/JavaScriptCore/runtime/Operations.cpp        2009-12-11 22:57:39 UTC (rev 52026)
</span><span class="lines">@@ -54,12 +54,13 @@
</span><span class="cx">     JSValue p1 = v1.toPrimitive(callFrame);
</span><span class="cx">     JSValue p2 = v2.toPrimitive(callFrame);
</span><span class="cx"> 
</span><del>-    if (p1.isString() || p2.isString()) {
-        RefPtr&lt;UString::Rep&gt; value = concatenate(p1.toString(callFrame).rep(), p2.toString(callFrame).rep());
-        if (!value)
-            return throwOutOfMemoryError(callFrame);
-        return jsString(callFrame, value.release());
</del><ins>+    if (p1.isString()) {
+        return p2.isString()
+            ? jsString(callFrame, asString(p1), asString(p2))
+            : jsString(callFrame, asString(p1), p2.toString(callFrame));
</ins><span class="cx">     }
</span><ins>+    if (p2.isString())
+        return jsString(callFrame, p1.toString(callFrame), asString(p2));
</ins><span class="cx"> 
</span><span class="cx">     return jsNumber(callFrame, p1.toNumber(callFrame) + p2.toNumber(callFrame));
</span><span class="cx"> }
</span></span></pre></div>
<a id="trunkJavaScriptCoreruntimeOperationsh"></a>
<div class="modfile"><h4>Modified: trunk/JavaScriptCore/runtime/Operations.h (52025 => 52026)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/JavaScriptCore/runtime/Operations.h        2009-12-11 22:39:29 UTC (rev 52025)
+++ trunk/JavaScriptCore/runtime/Operations.h        2009-12-11 22:57:39 UTC (rev 52026)
</span><span class="lines">@@ -37,6 +37,11 @@
</span><span class="cx"> 
</span><span class="cx">     ALWAYS_INLINE JSValue jsString(ExecState* exec, JSString* s1, JSString* s2)
</span><span class="cx">     {
</span><ins>+        if (!s1-&gt;length())
+            return s2;
+        if (!s2-&gt;length())
+            return s1;
+
</ins><span class="cx">         unsigned ropeLength = s1-&gt;ropeLength() + s2-&gt;ropeLength();
</span><span class="cx">         JSGlobalData* globalData = &amp;exec-&gt;globalData();
</span><span class="cx"> 
</span><span class="lines">@@ -53,6 +58,42 @@
</span><span class="cx">         return new (globalData) JSString(globalData, rope.release());
</span><span class="cx">     }
</span><span class="cx"> 
</span><ins>+    ALWAYS_INLINE JSValue jsString(ExecState* exec, const UString&amp; u1, JSString* s2)
+    {
+        unsigned ropeLength = 1 + s2-&gt;ropeLength();
+        JSGlobalData* globalData = &amp;exec-&gt;globalData();
+
+        if (ropeLength &lt;= JSString::s_maxInternalRopeLength)
+            return new (globalData) JSString(globalData, ropeLength, u1, s2);
+
+        unsigned index = 0;
+        RefPtr&lt;JSString::Rope&gt; rope = JSString::Rope::createOrNull(ropeLength);
+        if (UNLIKELY(!rope))
+            return throwOutOfMemoryError(exec);
+        rope-&gt;append(index, u1);
+        rope-&gt;append(index, s2);
+        ASSERT(index == ropeLength);
+        return new (globalData) JSString(globalData, rope.release());
+    }
+
+    ALWAYS_INLINE JSValue jsString(ExecState* exec, JSString* s1, const UString&amp; u2)
+    {
+        unsigned ropeLength = s1-&gt;ropeLength() + 1;
+        JSGlobalData* globalData = &amp;exec-&gt;globalData();
+
+        if (ropeLength &lt;= JSString::s_maxInternalRopeLength)
+            return new (globalData) JSString(globalData, ropeLength, s1, u2);
+
+        unsigned index = 0;
+        RefPtr&lt;JSString::Rope&gt; rope = JSString::Rope::createOrNull(ropeLength);
+        if (UNLIKELY(!rope))
+            return throwOutOfMemoryError(exec);
+        rope-&gt;append(index, s1);
+        rope-&gt;append(index, u2);
+        ASSERT(index == ropeLength);
+        return new (globalData) JSString(globalData, rope.release());
+    }
+
</ins><span class="cx">     ALWAYS_INLINE JSValue jsString(ExecState* exec, Register* strings, unsigned count)
</span><span class="cx">     {
</span><span class="cx">         ASSERT(count &gt;= 3);
</span><span class="lines">@@ -247,32 +288,16 @@
</span><span class="cx"> 
</span><span class="cx">     ALWAYS_INLINE JSValue jsAdd(CallFrame* callFrame, JSValue v1, JSValue v2)
</span><span class="cx">     {
</span><del>-        double left;
-        double right = 0.0;
-
-        bool rightIsNumber = v2.getNumber(right);
-        if (rightIsNumber &amp;&amp; v1.getNumber(left))
</del><ins>+        double left = 0.0, right;
+        if (v1.getNumber(left), v2.getNumber(right))
</ins><span class="cx">             return jsNumber(callFrame, left + right);
</span><span class="cx">         
</span><del>-        bool leftIsString = v1.isString();
-        if (leftIsString &amp;&amp; v2.isString()) {
-            if (!asString(v1)-&gt;length())
-                return asString(v2);
-            if (!asString(v2)-&gt;length())
-                return asString(v1);
-            return jsString(callFrame, asString(v1), asString(v2));
</del><ins>+        if (v1.isString()) {
+            return v2.isString()
+                ? jsString(callFrame, asString(v1), asString(v2))
+                : jsString(callFrame, asString(v1), v2.toPrimitiveString(callFrame));
</ins><span class="cx">         }
</span><span class="cx"> 
</span><del>-        if (rightIsNumber &amp; leftIsString) {
-            RefPtr&lt;UString::Rep&gt; value = v2.isInt32() ?
-                concatenate(asString(v1)-&gt;value(callFrame).rep(), v2.asInt32()) :
-                concatenate(asString(v1)-&gt;value(callFrame).rep(), right);
-
-            if (!value)
-                return throwOutOfMemoryError(callFrame);
-            return jsString(callFrame, value.release());
-        }
-
</del><span class="cx">         // All other cases are pretty uncommon
</span><span class="cx">         return jsAddSlowCase(callFrame, v1, v2);
</span><span class="cx">     }
</span></span></pre>
</div>
</div>

</body>
</html>