<!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>[185942] trunk/Source/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/185942">185942</a></dd>
<dt>Author</dt> <dd>darin@apple.com</dd>
<dt>Date</dt> <dd>2015-06-24 21:32:24 -0700 (Wed, 24 Jun 2015)</dd>
</dl>

<h3>Log Message</h3>
<pre>Optimize Array.join and Array.reverse for high speed array types
https://bugs.webkit.org/show_bug.cgi?id=146275

Reviewed by Mark Lam.

This seems to yield another 17% speed improvement in the array
test from the Peacekeeper benchmark.

* runtime/ArrayPrototype.cpp:
(JSC::isHole): Added. Helper to check for holes.
(JSC::containsHole): Ditto.
(JSC::arrayProtoFuncJoin): Added special cases for the various types
of arrays that could be in a butterfly.
(JSC::arrayProtoFuncReverse): Ditto.

* runtime/JSStringJoiner.h: Made appendEmptyString public so we can
call it from the new parts of Array.join.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkSourceJavaScriptCoreChangeLog">trunk/Source/JavaScriptCore/ChangeLog</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeArrayPrototypecpp">trunk/Source/JavaScriptCore/runtime/ArrayPrototype.cpp</a></li>
<li><a href="#trunkSourceJavaScriptCoreruntimeJSStringJoinerh">trunk/Source/JavaScriptCore/runtime/JSStringJoiner.h</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkSourceJavaScriptCoreChangeLog"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/ChangeLog (185941 => 185942)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/ChangeLog        2015-06-25 04:06:08 UTC (rev 185941)
+++ trunk/Source/JavaScriptCore/ChangeLog        2015-06-25 04:32:24 UTC (rev 185942)
</span><span class="lines">@@ -1,3 +1,23 @@
</span><ins>+2015-06-24  Darin Adler  &lt;darin@apple.com&gt;
+
+        Optimize Array.join and Array.reverse for high speed array types
+        https://bugs.webkit.org/show_bug.cgi?id=146275
+
+        Reviewed by Mark Lam.
+
+        This seems to yield another 17% speed improvement in the array
+        test from the Peacekeeper benchmark.
+
+        * runtime/ArrayPrototype.cpp:
+        (JSC::isHole): Added. Helper to check for holes.
+        (JSC::containsHole): Ditto.
+        (JSC::arrayProtoFuncJoin): Added special cases for the various types
+        of arrays that could be in a butterfly.
+        (JSC::arrayProtoFuncReverse): Ditto.
+
+        * runtime/JSStringJoiner.h: Made appendEmptyString public so we can
+        call it from the new parts of Array.join.
+
</ins><span class="cx"> 2015-06-24  Filip Pizlo  &lt;fpizlo@apple.com&gt;
</span><span class="cx"> 
</span><span class="cx">         DFG::SpeculativeJIT shouldn't use filter==Contradiction when it meant isClear
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeArrayPrototypecpp"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/ArrayPrototype.cpp (185941 => 185942)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/ArrayPrototype.cpp        2015-06-25 04:06:08 UTC (rev 185941)
+++ trunk/Source/JavaScriptCore/runtime/ArrayPrototype.cpp        2015-06-25 04:32:24 UTC (rev 185942)
</span><span class="lines">@@ -360,50 +360,137 @@
</span><span class="cx">     return JSValue::encode(stringJoiner.join(*exec));
</span><span class="cx"> }
</span><span class="cx"> 
</span><ins>+static inline bool isHole(double value)
+{
+    return std::isnan(value);
+}
+
+static inline bool isHole(const WriteBarrier&lt;Unknown&gt;&amp; value)
+{
+    return !value;
+}
+
+template&lt;typename T&gt; static inline bool containsHole(T* data, unsigned length)
+{
+    for (unsigned i = 0; i &lt; length; ++i) {
+        if (isHole(data[i]))
+            return true;
+    }
+    return false;
+}
+
</ins><span class="cx"> EncodedJSValue JSC_HOST_CALL arrayProtoFuncJoin(ExecState* exec)
</span><span class="cx"> {
</span><del>-    JSObject* thisObj = exec-&gt;thisValue().toThis(exec, StrictMode).toObject(exec);
-    unsigned length = getLength(exec, thisObj);
</del><ins>+    JSObject* thisObject = exec-&gt;thisValue().toThis(exec, StrictMode).toObject(exec);
+
+    unsigned length = getLength(exec, thisObject);
</ins><span class="cx">     if (exec-&gt;hadException())
</span><span class="cx">         return JSValue::encode(jsUndefined());
</span><span class="cx"> 
</span><del>-    StringRecursionChecker checker(exec, thisObj);
</del><ins>+    StringRecursionChecker checker(exec, thisObject);
</ins><span class="cx">     if (JSValue earlyReturnValue = checker.earlyReturnValue())
</span><span class="cx">         return JSValue::encode(earlyReturnValue);
</span><span class="cx"> 
</span><del>-    JSString* separator = nullptr;
-    if (!exec-&gt;argument(0).isUndefined()) {
-        separator = exec-&gt;argument(0).toString(exec);
</del><ins>+    auto&amp; vm = exec-&gt;vm();
+
+    Strong&lt;JSString&gt; separatorOnHeap;
+    StringView separator;
+    if (exec-&gt;argument(0).isUndefined()) {
+        static const LChar comma = ',';
+        separator = { &amp;comma, 1 };
+    } else {
+        separatorOnHeap.set(vm, exec-&gt;argument(0).toString(exec));
</ins><span class="cx">         if (exec-&gt;hadException())
</span><span class="cx">             return JSValue::encode(jsUndefined());
</span><ins>+        separator = separatorOnHeap-&gt;view(exec);
</ins><span class="cx">     }
</span><span class="cx"> 
</span><del>-    const LChar comma = ',';
-    JSStringJoiner stringJoiner(*exec, separator ? separator-&gt;view(exec) : StringView{ &amp;comma, 1 }, length);
-    if (exec-&gt;hadException())
-        return JSValue::encode(jsUndefined());
-
-    unsigned i = 0;
-
-    if (isJSArray(thisObj)) {
-        JSArray* array = asArray(thisObj);
-        for (; i &lt; length &amp;&amp; array-&gt;canGetIndexQuickly(i); ++i) {
-            stringJoiner.append(*exec, array-&gt;getIndexQuickly(i));
-            if (exec-&gt;hadException())
-                return JSValue::encode(jsUndefined());
</del><ins>+    switch (thisObject-&gt;indexingType()) {
+    case ALL_CONTIGUOUS_INDEXING_TYPES:
+    case ALL_INT32_INDEXING_TYPES: {
+        auto&amp; butterfly = *thisObject-&gt;butterfly();
+        if (length &gt; butterfly.publicLength())
+            break;
+        JSStringJoiner joiner(*exec, separator, length);
+        if (exec-&gt;hadException())
+            return JSValue::encode(jsUndefined());
+        auto data = butterfly.contiguous().data();
+        bool holesKnownToBeOK = false;
+        for (unsigned i = 0; i &lt; length; ++i) {
+            if (JSValue value = data[i].get()) {
+                joiner.append(*exec, value);
+                if (exec-&gt;hadException())
+                    return JSValue::encode(jsUndefined());
+            } else {
+                if (!holesKnownToBeOK) {
+                    if (thisObject-&gt;structure(vm)-&gt;holesMustForwardToPrototype(vm))
+                        goto generalCase;
+                    holesKnownToBeOK = true;
+                }
+                joiner.appendEmptyString();
+            }
</ins><span class="cx">         }
</span><ins>+        return JSValue::encode(joiner.join(*exec));
</ins><span class="cx">     }
</span><ins>+    case ALL_DOUBLE_INDEXING_TYPES: {
+        auto&amp; butterfly = *thisObject-&gt;butterfly();
+        if (length &gt; butterfly.publicLength())
+            break;
+        JSStringJoiner joiner(*exec, separator, length);
+        if (exec-&gt;hadException())
+            return JSValue::encode(jsUndefined());
+        auto data = butterfly.contiguousDouble().data();
+        bool holesKnownToBeOK = false;
+        for (unsigned i = 0; i &lt; length; ++i) {
+            double value = data[i];
+            if (!isHole(value))
+                joiner.append(*exec, jsDoubleNumber(value));
+            else {
+                if (!holesKnownToBeOK) {
+                    if (thisObject-&gt;structure(vm)-&gt;holesMustForwardToPrototype(vm))
+                        goto generalCase;
+                    holesKnownToBeOK = true;
+                }
+                joiner.appendEmptyString();
+            }
+        }
+        return JSValue::encode(joiner.join(*exec));
+    }
+    case ALL_ARRAY_STORAGE_INDEXING_TYPES: {
+        auto&amp; storage = *thisObject-&gt;butterfly()-&gt;arrayStorage();
+        if (length &gt; storage.vectorLength())
+            break;
+        if (storage.hasHoles() &amp;&amp; thisObject-&gt;structure(vm)-&gt;holesMustForwardToPrototype(vm))
+            break;
+        JSStringJoiner joiner(*exec, separator, length);
+        if (exec-&gt;hadException())
+            return JSValue::encode(jsUndefined());
+        auto data = storage.vector().data();
+        for (unsigned i = 0; i &lt; length; ++i) {
+            if (JSValue value = data[i].get()) {
+                joiner.append(*exec, value);
+                if (exec-&gt;hadException())
+                    return JSValue::encode(jsUndefined());
+            } else
+                joiner.appendEmptyString();
+        }
+        return JSValue::encode(joiner.join(*exec));
+    }
+    }
</ins><span class="cx"> 
</span><del>-    for (; i &lt; length; ++i) {
-        JSValue element = thisObj-&gt;get(exec, i);
</del><ins>+generalCase:
+    JSStringJoiner joiner(*exec, separator, length);
+    if (exec-&gt;hadException())
+        return JSValue::encode(jsUndefined());
+    for (unsigned i = 0; i &lt; length; ++i) {
+        JSValue element = thisObject-&gt;getIndex(exec, i);
</ins><span class="cx">         if (exec-&gt;hadException())
</span><span class="cx">             return JSValue::encode(jsUndefined());
</span><del>-        stringJoiner.append(*exec, element);
</del><ins>+        joiner.append(*exec, element);
</ins><span class="cx">         if (exec-&gt;hadException())
</span><span class="cx">             return JSValue::encode(jsUndefined());
</span><span class="cx">     }
</span><del>-
-    return JSValue::encode(stringJoiner.join(*exec));
</del><ins>+    return JSValue::encode(joiner.join(*exec));
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> EncodedJSValue JSC_HOST_CALL arrayProtoFuncConcat(ExecState* exec)
</span><span class="lines">@@ -525,40 +612,77 @@
</span><span class="cx"> 
</span><span class="cx"> EncodedJSValue JSC_HOST_CALL arrayProtoFuncReverse(ExecState* exec)
</span><span class="cx"> {
</span><del>-    JSObject* thisObj = exec-&gt;thisValue().toThis(exec, StrictMode).toObject(exec);
-    unsigned length = getLength(exec, thisObj);
</del><ins>+    JSObject* thisObject = exec-&gt;thisValue().toThis(exec, StrictMode).toObject(exec);
+
+    unsigned length = getLength(exec, thisObject);
</ins><span class="cx">     if (exec-&gt;hadException())
</span><span class="cx">         return JSValue::encode(jsUndefined());
</span><span class="cx"> 
</span><ins>+    auto&amp; vm = exec-&gt;vm();
+
+    switch (thisObject-&gt;indexingType()) {
+    case ALL_CONTIGUOUS_INDEXING_TYPES:
+    case ALL_INT32_INDEXING_TYPES: {
+        auto&amp; butterfly = *thisObject-&gt;butterfly();
+        if (length &gt; butterfly.publicLength())
+            break;
+        auto data = butterfly.contiguous().data();
+        if (containsHole(data, length) &amp;&amp; thisObject-&gt;structure(vm)-&gt;holesMustForwardToPrototype(vm))
+            break;
+        std::reverse(data, data + length);
+        return JSValue::encode(thisObject);
+    }
+    case ALL_DOUBLE_INDEXING_TYPES: {
+        auto&amp; butterfly = *thisObject-&gt;butterfly();
+        if (length &gt; butterfly.publicLength())
+            break;
+        auto data = butterfly.contiguousDouble().data();
+        if (containsHole(data, length) &amp;&amp; thisObject-&gt;structure(vm)-&gt;holesMustForwardToPrototype(vm))
+            break;
+        std::reverse(data, data + length);
+        return JSValue::encode(thisObject);
+    }
+    case ALL_ARRAY_STORAGE_INDEXING_TYPES: {
+        auto&amp; storage = *thisObject-&gt;butterfly()-&gt;arrayStorage();
+        if (length &gt; storage.vectorLength())
+            break;
+        if (storage.hasHoles() &amp;&amp; thisObject-&gt;structure(vm)-&gt;holesMustForwardToPrototype(vm))
+            break;
+        auto data = storage.vector().data();
+        std::reverse(data, data + length);
+        return JSValue::encode(thisObject);
+    }
+    }
+
</ins><span class="cx">     unsigned middle = length / 2;
</span><span class="cx">     for (unsigned k = 0; k &lt; middle; k++) {
</span><span class="cx">         unsigned lk1 = length - k - 1;
</span><del>-        JSValue obj2 = getProperty(exec, thisObj, lk1);
</del><ins>+        JSValue obj2 = getProperty(exec, thisObject, lk1);
</ins><span class="cx">         if (exec-&gt;hadException())
</span><span class="cx">             return JSValue::encode(jsUndefined());
</span><del>-        JSValue obj = getProperty(exec, thisObj, k);
</del><ins>+        JSValue obj = getProperty(exec, thisObject, k);
</ins><span class="cx">         if (exec-&gt;hadException())
</span><span class="cx">             return JSValue::encode(jsUndefined());
</span><span class="cx"> 
</span><span class="cx">         if (obj2) {
</span><del>-            thisObj-&gt;putByIndexInline(exec, k, obj2, true);
</del><ins>+            thisObject-&gt;putByIndexInline(exec, k, obj2, true);
</ins><span class="cx">             if (exec-&gt;hadException())
</span><span class="cx">                 return JSValue::encode(jsUndefined());
</span><del>-        } else if (!thisObj-&gt;methodTable(exec-&gt;vm())-&gt;deletePropertyByIndex(thisObj, exec, k)) {
</del><ins>+        } else if (!thisObject-&gt;methodTable(exec-&gt;vm())-&gt;deletePropertyByIndex(thisObject, exec, k)) {
</ins><span class="cx">             throwTypeError(exec, ASCIILiteral(&quot;Unable to delete property.&quot;));
</span><span class="cx">             return JSValue::encode(jsUndefined());
</span><span class="cx">         }
</span><span class="cx"> 
</span><span class="cx">         if (obj) {
</span><del>-            thisObj-&gt;putByIndexInline(exec, lk1, obj, true);
</del><ins>+            thisObject-&gt;putByIndexInline(exec, lk1, obj, true);
</ins><span class="cx">             if (exec-&gt;hadException())
</span><span class="cx">                 return JSValue::encode(jsUndefined());
</span><del>-        } else if (!thisObj-&gt;methodTable(exec-&gt;vm())-&gt;deletePropertyByIndex(thisObj, exec, lk1)) {
</del><ins>+        } else if (!thisObject-&gt;methodTable(exec-&gt;vm())-&gt;deletePropertyByIndex(thisObject, exec, lk1)) {
</ins><span class="cx">             throwTypeError(exec, ASCIILiteral(&quot;Unable to delete property.&quot;));
</span><span class="cx">             return JSValue::encode(jsUndefined());
</span><span class="cx">         }
</span><span class="cx">     }
</span><del>-    return JSValue::encode(thisObj);
</del><ins>+    return JSValue::encode(thisObject);
</ins><span class="cx"> }
</span><span class="cx"> 
</span><span class="cx"> EncodedJSValue JSC_HOST_CALL arrayProtoFuncShift(ExecState* exec)
</span></span></pre></div>
<a id="trunkSourceJavaScriptCoreruntimeJSStringJoinerh"></a>
<div class="modfile"><h4>Modified: trunk/Source/JavaScriptCore/runtime/JSStringJoiner.h (185941 => 185942)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/Source/JavaScriptCore/runtime/JSStringJoiner.h        2015-06-25 04:06:08 UTC (rev 185941)
+++ trunk/Source/JavaScriptCore/runtime/JSStringJoiner.h        2015-06-25 04:32:24 UTC (rev 185942)
</span><span class="lines">@@ -37,6 +37,7 @@
</span><span class="cx">     JSStringJoiner(ExecState&amp;, StringView separator, unsigned stringCount);
</span><span class="cx"> 
</span><span class="cx">     void append(ExecState&amp;, JSValue);
</span><ins>+    void appendEmptyString();
</ins><span class="cx"> 
</span><span class="cx">     JSValue join(ExecState&amp;);
</span><span class="cx"> 
</span><span class="lines">@@ -44,7 +45,6 @@
</span><span class="cx">     void append(StringViewWithUnderlyingString&amp;&amp;);
</span><span class="cx">     void append8Bit(const String&amp;);
</span><span class="cx">     void appendLiteral(const Identifier&amp;);
</span><del>-    void appendEmptyString();
</del><span class="cx">     unsigned joinedLength(ExecState&amp;) const;
</span><span class="cx"> 
</span><span class="cx">     LChar m_singleCharacterSeparator;
</span></span></pre>
</div>
</div>

</body>
</html>