Optimize join of large empty arrays
authortzagallo@apple.com <tzagallo@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 10 Jul 2019 08:24:52 +0000 (08:24 +0000)
committertzagallo@apple.com <tzagallo@apple.com@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Wed, 10 Jul 2019 08:24:52 +0000 (08:24 +0000)
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/JavaScriptCore:

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):

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@247296 268f45cc-cd09-0410-ab3c-d52691b4dbfc

JSTests/ChangeLog
JSTests/microbenchmarks/large-empty-array-join-resolve-rope.js [new file with mode: 0644]
JSTests/microbenchmarks/large-empty-array-join.js [new file with mode: 0644]
Source/JavaScriptCore/ChangeLog
Source/JavaScriptCore/runtime/ArrayPrototype.cpp

index decc266..132b78b 100644 (file)
@@ -1,3 +1,13 @@
+2019-07-10  Tadeu Zagallo  <tzagallo@apple.com>
+
+        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  <msaboff@apple.com>
 
         switch(String) needs to check for exceptions when resolving the string
diff --git a/JSTests/microbenchmarks/large-empty-array-join-resolve-rope.js b/JSTests/microbenchmarks/large-empty-array-join-resolve-rope.js
new file mode 100644 (file)
index 0000000..85cb8e9
--- /dev/null
@@ -0,0 +1,3 @@
+const array = [];
+for (let i = 0; i < 100; ++i)
+    array.push(new Array(1e4).join('—' + i).slice(1));
diff --git a/JSTests/microbenchmarks/large-empty-array-join.js b/JSTests/microbenchmarks/large-empty-array-join.js
new file mode 100644 (file)
index 0000000..1a2ae07
--- /dev/null
@@ -0,0 +1,3 @@
+const array = [];
+for (let i = 0; i < 100; ++i)
+    array.push(new Array(1e6).join('—' + i));
index 5db502c..3cc87d6 100644 (file)
@@ -1,3 +1,32 @@
+2019-07-10  Tadeu Zagallo  <tzagallo@apple.com>
+
+        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  <keith_miller@apple.com>
 
         Enable Intl.PluralRules and Intl.NumberFormatToParts by default
index c8a9cdd..0135202 100644 (file)
@@ -540,6 +540,25 @@ inline JSValue fastJoin(ExecState& state, JSObject* thisObject, StringView separ
             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, { });
+            }
         }
         }
     }