aboutsummaryrefslogtreecommitdiff
path: root/tools/js-optimizer.js
diff options
context:
space:
mode:
authorAlon Zakai <alonzakai@gmail.com>2013-06-06 21:12:20 -0700
committerAlon Zakai <alonzakai@gmail.com>2013-06-07 10:09:12 -0700
commit5f2ccad0a399276de4e8426fcd1449e256499815 (patch)
treeae98d4a32912a48f91b4f19885a98b5b8a980ebe /tools/js-optimizer.js
parent57ea5f4e7110310733e81194082bda168a463ae4 (diff)
track number of uses properly for loop variable removal
Diffstat (limited to 'tools/js-optimizer.js')
-rw-r--r--tools/js-optimizer.js9
1 files changed, 8 insertions, 1 deletions
diff --git a/tools/js-optimizer.js b/tools/js-optimizer.js
index a3a58f2b..67ce2b3d 100644
--- a/tools/js-optimizer.js
+++ b/tools/js-optimizer.js
@@ -1854,6 +1854,7 @@ function eliminate(ast, memSafe) {
// First, find the potentially eliminatable functions: that have one definition and one use
var definitions = {};
var uses = {};
+ var namings = {};
var values = {};
var locals = {};
var varsToRemove = {}; // variables being removed, that we can eliminate all 'var x;' of (this refers to 'var' nodes we should remove)
@@ -1896,6 +1897,8 @@ function eliminate(ast, memSafe) {
if (!values[name]) values[name] = node[3];
if (node[1] === true) { // not +=, -= etc., just =
uses[name]--; // because the name node will show up by itself in the previous case
+ if (!namings[name]) namings[name] = 0;
+ namings[name]++; // offset it here, this tracks the total times we are named
}
}
} else if (type == 'switch') {
@@ -1903,6 +1906,10 @@ function eliminate(ast, memSafe) {
}
});
+ for (var used in uses) {
+ namings[used] = (namings[used] || 0) + uses[used];
+ }
+
// we cannot eliminate variables if there is a switch
if (hasSwitch && !asm) return;
@@ -2372,7 +2379,7 @@ function eliminate(ast, memSafe) {
if (assign[1] === true && assign[2][0] == 'name' && assign[3][0] == 'name') {
var looper = assign[2][1];
var helper = assign[3][1];
- if (definitions[helper] == 1 && seenUses[looper] == uses[looper] + 1 && // +1, because uses does not count the definition
+ if (definitions[helper] == 1 && seenUses[looper] == namings[looper] &&
!helperReplacements[helper] && !helperReplacements[looper]) {
// the remaining issue is whether looper is used after the assignment to helper and before the last line (where we assign to it)
var found = -1;