Title: [247296] trunk
Revision
247296
Author
[email protected]
Date
2019-07-10 01:24:52 -0700 (Wed, 10 Jul 2019)

Log Message

Optimize join of large empty arrays
https://bugs.webkit.org/show_bug.cgi?id=199636

Reviewed by Mark Lam.

JSTests:

* microbenchmarks/large-empty-array-join.js: Added.
* microbenchmarks/large-empty-array-join-resolve-rope.js: Added.

Source/_javascript_Core:

Replicate the behavior of `str.repeat(count)` when performing `new Array(count + 1).join(str)`.
I added two new microbenchmarks:
- large-empty-array-join, which does not use the result of the join and runs ~44x faster and uses ~18x less memory.
- large-empty-array-join-resolve-rope, which uses the result of the join and runs 2x faster.

                                            baseline                    diff
large-empty-array-join                2713.9698+-72.7621    ^     61.2335+-10.4836       ^ definitely 44.3217x faster
large-empty-array-join-resolve-string   26.5517+-0.3995     ^     12.9309+-0.5516        ^ definitely 2.0533x faster

large-empty-array-join memory usage with baseline (dirty):
    733012 kB current_mem
    756824 kB lifetime_peak

large-empty-array-join memory usage with diff (dirty):
    41904 kB current_mem
    41972 kB lifetime_peak

Additionally, I ran JetStream2, sunspider and v8-spider and all were neutral.

* runtime/ArrayPrototype.cpp:
(JSC::fastJoin):

Modified Paths

Added Paths

Diff

Modified: trunk/JSTests/ChangeLog (247295 => 247296)


--- trunk/JSTests/ChangeLog	2019-07-10 06:33:57 UTC (rev 247295)
+++ trunk/JSTests/ChangeLog	2019-07-10 08:24:52 UTC (rev 247296)
@@ -1,3 +1,13 @@
+2019-07-10  Tadeu Zagallo  <[email protected]>
+
+        Optimize join of large empty arrays
+        https://bugs.webkit.org/show_bug.cgi?id=199636
+
+        Reviewed by Mark Lam.
+
+        * microbenchmarks/large-empty-array-join.js: Added.
+        * microbenchmarks/large-empty-array-join-resolve-rope.js: Added.
+
 2019-07-06  Michael Saboff  <[email protected]>
 
         switch(String) needs to check for exceptions when resolving the string

Added: trunk/JSTests/microbenchmarks/large-empty-array-join-resolve-rope.js (0 => 247296)


--- trunk/JSTests/microbenchmarks/large-empty-array-join-resolve-rope.js	                        (rev 0)
+++ trunk/JSTests/microbenchmarks/large-empty-array-join-resolve-rope.js	2019-07-10 08:24:52 UTC (rev 247296)
@@ -0,0 +1,3 @@
+const array = [];
+for (let i = 0; i < 100; ++i)
+    array.push(new Array(1e4).join('—' + i).slice(1));

Added: trunk/JSTests/microbenchmarks/large-empty-array-join.js (0 => 247296)


--- trunk/JSTests/microbenchmarks/large-empty-array-join.js	                        (rev 0)
+++ trunk/JSTests/microbenchmarks/large-empty-array-join.js	2019-07-10 08:24:52 UTC (rev 247296)
@@ -0,0 +1,3 @@
+const array = [];
+for (let i = 0; i < 100; ++i)
+    array.push(new Array(1e6).join('—' + i));

Modified: trunk/Source/_javascript_Core/ChangeLog (247295 => 247296)


--- trunk/Source/_javascript_Core/ChangeLog	2019-07-10 06:33:57 UTC (rev 247295)
+++ trunk/Source/_javascript_Core/ChangeLog	2019-07-10 08:24:52 UTC (rev 247296)
@@ -1,3 +1,32 @@
+2019-07-10  Tadeu Zagallo  <[email protected]>
+
+        Optimize join of large empty arrays
+        https://bugs.webkit.org/show_bug.cgi?id=199636
+
+        Reviewed by Mark Lam.
+
+        Replicate the behavior of `str.repeat(count)` when performing `new Array(count + 1).join(str)`.
+        I added two new microbenchmarks:
+        - large-empty-array-join, which does not use the result of the join and runs ~44x faster and uses ~18x less memory.
+        - large-empty-array-join-resolve-rope, which uses the result of the join and runs 2x faster.
+
+                                                    baseline                    diff
+        large-empty-array-join                2713.9698+-72.7621    ^     61.2335+-10.4836       ^ definitely 44.3217x faster
+        large-empty-array-join-resolve-string   26.5517+-0.3995     ^     12.9309+-0.5516        ^ definitely 2.0533x faster
+
+        large-empty-array-join memory usage with baseline (dirty):
+            733012 kB current_mem
+            756824 kB lifetime_peak
+
+        large-empty-array-join memory usage with diff (dirty):
+            41904 kB current_mem
+            41972 kB lifetime_peak
+
+        Additionally, I ran JetStream2, sunspider and v8-spider and all were neutral.
+
+        * runtime/ArrayPrototype.cpp:
+        (JSC::fastJoin):
+
 2019-07-08  Keith Miller  <[email protected]>
 
         Enable Intl.PluralRules and Intl.NumberFormatToParts by default

Modified: trunk/Source/_javascript_Core/runtime/ArrayPrototype.cpp (247295 => 247296)


--- trunk/Source/_javascript_Core/runtime/ArrayPrototype.cpp	2019-07-10 06:33:57 UTC (rev 247295)
+++ trunk/Source/_javascript_Core/runtime/ArrayPrototype.cpp	2019-07-10 08:24:52 UTC (rev 247296)
@@ -540,6 +540,25 @@
             if (separator.is8Bit())
                 RELEASE_AND_RETURN(scope, repeatCharacter(state, separator.characters8()[0], length - 1));
             RELEASE_AND_RETURN(scope, repeatCharacter(state, separator.characters16()[0], length - 1));
+        default:
+            JSString* result = jsEmptyString(&state);
+            if (length <= 1)
+                return result;
+
+            JSString* operand = jsString(&vm, separator.toString());
+            RETURN_IF_EXCEPTION(scope, { });
+            unsigned count = length - 1;
+            for (;;) {
+                if (count & 1) {
+                    result = jsString(&state, result, operand);
+                    RETURN_IF_EXCEPTION(scope, { });
+                }
+                count >>= 1;
+                if (!count)
+                    return result;
+                operand = jsString(&state, operand, operand);
+                RETURN_IF_EXCEPTION(scope, { });
+            }
         }
         }
     }
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to