[Pkg-javascript-commits] [uglifyjs] 273/491: extend `reduce_funcs` to cover cross-scope substitutions (#2469)

Jonas Smedegaard dr at jones.dk
Wed Feb 14 19:51:43 UTC 2018


This is an automated email from the git hooks/post-receive script.

js pushed a commit to annotated tag debian/3.3.10-1
in repository uglifyjs.

commit cda27b09703a0dfc8f84612a3d157a517f8ea4a9
Author: Alex Lam S.L <alexlamsl at gmail.com>
Date:   Sat Nov 11 15:30:17 2017 +0800

    extend `reduce_funcs` to cover cross-scope substitutions (#2469)
    
    fixes #2468
---
 lib/compress.js              |   5 +-
 test/compress/reduce_vars.js | 209 ++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 208 insertions(+), 6 deletions(-)

diff --git a/lib/compress.js b/lib/compress.js
index 2b38ed4..fdf3a2b 100644
--- a/lib/compress.js
+++ b/lib/compress.js
@@ -4256,10 +4256,11 @@ merge(Compressor.prototype, {
             var fixed = self.fixed_value();
             if (fixed instanceof AST_Defun) {
                 d.fixed = fixed = make_node(AST_Function, fixed, fixed);
-                if (!compressor.option("reduce_funcs")) d.single_use = false;
             }
             if (d.single_use && fixed instanceof AST_Function) {
-                if (d.escaped && d.scope !== self.scope || recursive_ref(compressor, d)) {
+                if (!compressor.option("reduce_funcs") && d.scope !== self.scope) {
+                    d.single_use = false;
+                } else if (d.escaped && d.scope !== self.scope || recursive_ref(compressor, d)) {
                     d.single_use = false;
                 } else if (d.scope !== self.scope || d.orig[0] instanceof AST_SymbolFunarg) {
                     d.single_use = fixed.is_constant_expression(self.scope);
diff --git a/test/compress/reduce_vars.js b/test/compress/reduce_vars.js
index 76ed460..e718949 100644
--- a/test/compress/reduce_vars.js
+++ b/test/compress/reduce_vars.js
@@ -4208,7 +4208,6 @@ issue_2449: {
 
 perf_1: {
     options = {
-        passes: 10,
         reduce_funcs: true,
         reduce_vars: true,
         toplevel: true,
@@ -4223,7 +4222,7 @@ perf_1: {
         }
         var sum = 0;
         for (var i = 0; i < 100; ++i) {
-            sum += indirect_foo(i, i+1, i*3);
+            sum += indirect_foo(i, i + 1, 3 * i);
         }
         console.log(sum);
     }
@@ -4243,7 +4242,6 @@ perf_1: {
 
 perf_2: {
     options = {
-        passes: 10,
         reduce_funcs: false,
         reduce_vars: true,
         toplevel: true,
@@ -4258,7 +4256,7 @@ perf_2: {
         }
         var sum = 0;
         for (var i = 0; i < 100; ++i) {
-            sum += indirect_foo(i, i+1, i*3);
+            sum += indirect_foo(i, i + 1, 3 * i);
         }
         console.log(sum);
     }
@@ -4276,3 +4274,206 @@ perf_2: {
     }
     expect_stdout: "348150"
 }
+
+perf_3: {
+    options = {
+        reduce_funcs: true,
+        reduce_vars: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        var foo = function(x, y, z) {
+            return x < y ? x * y + z : x * z - y;
+        }
+        var indirect_foo = function(x, y, z) {
+            return foo(x, y, z);
+        }
+        var sum = 0;
+        for (var i = 0; i < 100; ++i)
+            sum += indirect_foo(i, i + 1, 3 * i);
+        console.log(sum);
+    }
+    expect: {
+        var indirect_foo = function(x, y, z) {
+            return function(x, y, z) {
+                return x < y ? x * y + z : x * z - y;
+            }(x, y, z);
+        }
+        var sum = 0;
+        for (var i = 0; i < 100; ++i)
+            sum += indirect_foo(i, i + 1, 3 * i);
+        console.log(sum);
+    }
+    expect_stdout: "348150"
+}
+
+perf_4: {
+    options = {
+        reduce_funcs: false,
+        reduce_vars: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        var foo = function(x, y, z) {
+            return x < y ? x * y + z : x * z - y;
+        }
+        var indirect_foo = function(x, y, z) {
+            return foo(x, y, z);
+        }
+        var sum = 0;
+        for (var i = 0; i < 100; ++i)
+            sum += indirect_foo(i, i + 1, 3 * i);
+        console.log(sum);
+    }
+    expect: {
+        var foo = function(x, y, z) {
+            return x < y ? x * y + z : x * z - y;
+        }
+        var indirect_foo = function(x, y, z) {
+            return foo(x, y, z);
+        }
+        var sum = 0;
+        for (var i = 0; i < 100; ++i)
+            sum += indirect_foo(i, i + 1, 3 * i);
+        console.log(sum);
+    }
+    expect_stdout: "348150"
+}
+
+perf_5: {
+    options = {
+        passes: 10,
+        reduce_funcs: true,
+        reduce_vars: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        function indirect_foo(x, y, z) {
+            function foo(x, y, z) {
+                return x < y ? x * y + z : x * z - y;
+            }
+            return foo(x, y, z);
+        }
+        var sum = 0;
+        for (var i = 0; i < 100; ++i) {
+            sum += indirect_foo(i, i + 1, 3 * i);
+        }
+        console.log(sum);
+    }
+    expect: {
+        function indirect_foo(x, y, z) {
+            return function(x, y, z) {
+                return x < y ? x * y + z : x * z - y;
+            }(x, y, z);
+        }
+        var sum = 0;
+        for (var i = 0; i < 100; ++i)
+            sum += indirect_foo(i, i + 1, 3 * i);
+        console.log(sum);
+    }
+    expect_stdout: "348150"
+}
+
+perf_6: {
+    options = {
+        passes: 10,
+        reduce_funcs: false,
+        reduce_vars: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        function indirect_foo(x, y, z) {
+            function foo(x, y, z) {
+                return x < y ? x * y + z : x * z - y;
+            }
+            return foo(x, y, z);
+        }
+        var sum = 0;
+        for (var i = 0; i < 100; ++i) {
+            sum += indirect_foo(i, i + 1, 3 * i);
+        }
+        console.log(sum);
+    }
+    expect: {
+        function indirect_foo(x, y, z) {
+            return function(x, y, z) {
+                return x < y ? x * y + z : x * z - y;
+            }(x, y, z);
+        }
+        var sum = 0;
+        for (var i = 0; i < 100; ++i)
+            sum += indirect_foo(i, i + 1, 3 * i);
+        console.log(sum);
+    }
+    expect_stdout: "348150"
+}
+
+perf_7: {
+    options = {
+        reduce_funcs: true,
+        reduce_vars: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        var indirect_foo = function(x, y, z) {
+            var foo = function(x, y, z) {
+                return x < y ? x * y + z : x * z - y;
+            }
+            return foo(x, y, z);
+        }
+        var sum = 0;
+        for (var i = 0; i < 100; ++i)
+            sum += indirect_foo(i, i + 1, 3 * i);
+        console.log(sum);
+    }
+    expect: {
+        var indirect_foo = function(x, y, z) {
+            return function(x, y, z) {
+                return x < y ? x * y + z : x * z - y;
+            }(x, y, z);
+        }
+        var sum = 0;
+        for (var i = 0; i < 100; ++i)
+            sum += indirect_foo(i, i + 1, 3 * i);
+        console.log(sum);
+    }
+    expect_stdout: "348150"
+}
+
+perf_8: {
+    options = {
+        reduce_funcs: false,
+        reduce_vars: true,
+        toplevel: true,
+        unused: true,
+    }
+    input: {
+        var indirect_foo = function(x, y, z) {
+            var foo = function(x, y, z) {
+                return x < y ? x * y + z : x * z - y;
+            }
+            return foo(x, y, z);
+        }
+        var sum = 0;
+        for (var i = 0; i < 100; ++i)
+            sum += indirect_foo(i, i + 1, 3 * i);
+        console.log(sum);
+    }
+    expect: {
+        var indirect_foo = function(x, y, z) {
+            return function(x, y, z) {
+                return x < y ? x * y + z : x * z - y;
+            }(x, y, z);
+        }
+        var sum = 0;
+        for (var i = 0; i < 100; ++i)
+            sum += indirect_foo(i, i + 1, 3 * i);
+        console.log(sum);
+    }
+    expect_stdout: "348150"
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-javascript/uglifyjs.git



More information about the Pkg-javascript-commits mailing list