Variables referenced before declaration prevent some Closure Compiler optimizations
I compile d3 together with my code with a compilation_level of ADVANCED_OPTIMIZATIONS. I discovered that certain functions of the compiled code break in unexpected ways, for example d3.scaleQuantile().domain([0, 6]).range([1, 2, 3]).quantiles() returns [NaN, NaN] instead of [2, 4]. The root cause is that the compiler can't correctly inline functions that use variables before declaration, e.g. n in quantile():
function quantile(array, p, f) {
if (!(n = array.length)) return;
if ((p = +p) <= 0 || n < 2) return +f(array[0], 0, array);
if (p >= 1) return +f(array[n - 1], n - 1, array);
var n,
h = (n - 1) * p,
i = Math.floor(h),
a = +f(array[i], i, array),
b = +f(array[i + 1], i + 1, array);
return a + (b - a) * (h - i);
}
I've filed a Google-internal bug against the compiler, but it would be also nice to update d3 to not use the referencing before declaration pattern. FWIW, the let keyword in ECMAScript 6 doesn't allow it either.
In v4.2.2 there are 52 occurrences:
third_party/javascript/d3/v4/d3.js:181: WARNING - Variable referenced before declaration: n
start = +start, stop = +stop, step = (n = arguments.length) < 2 ? (stop = start, start = 0, 1) : n < 3 ? 1 : +step;
^
third_party/javascript/d3/v4/d3.js:181: WARNING - Variable referenced before declaration: n
start = +start, stop = +stop, step = (n = arguments.length) < 2 ? (stop = start, start = 0, 1) : n < 3 ? 1 : +step;
^
third_party/javascript/d3/v4/d3.js:286: WARNING - Variable referenced before declaration: n
if (!(n = array.length)) return;
^
third_party/javascript/d3/v4/d3.js:287: WARNING - Variable referenced before declaration: n
if ((p = +p) <= 0 || n < 2) return +f(array[0], 0, array);
^
third_party/javascript/d3/v4/d3.js:288: WARNING - Variable referenced before declaration: n
if (p >= 1) return +f(array[n - 1], n - 1, array);
^
third_party/javascript/d3/v4/d3.js:288: WARNING - Variable referenced before declaration: n
if (p >= 1) return +f(array[n - 1], n - 1, array);
^
third_party/javascript/d3/v4/d3.js:414: WARNING - Variable referenced before declaration: n
if (!(n = array.length)) return;
^
third_party/javascript/d3/v4/d3.js:461: WARNING - Variable referenced before declaration: n
if (!(n = matrix.length)) return [];
^
third_party/javascript/d3/v4/d3.js:1010: WARNING - Variable referenced before declaration: n
if ((n = points.length) < 3) return null;
^
third_party/javascript/d3/v4/d3.js:1458: WARNING - Variable referenced before declaration: x
if (isNaN(x = +this._x.call(null, d)) || isNaN(y = +this._y.call(null, d))) return this; // ignore invalid points
^
third_party/javascript/d3/v4/d3.js:1458: WARNING - Variable referenced before declaration: y
if (isNaN(x = +this._x.call(null, d)) || isNaN(y = +this._y.call(null, d))) return this; // ignore invalid points
^
third_party/javascript/d3/v4/d3.js:3359: WARNING - Variable referenced before declaration: n
if (!((n = series.length) > 1)) return;
^
third_party/javascript/d3/v4/d3.js:3428: WARNING - Variable referenced before declaration: n
if (!((n = series.length) > 0)) return;
^
third_party/javascript/d3/v4/d3.js:3437: WARNING - Variable referenced before declaration: n
if (!((n = series.length) > 0)) return;
^
third_party/javascript/d3/v4/d3.js:3446: WARNING - Variable referenced before declaration: n
if (!((n = series.length) > 0) || !((m = (s0 = series[order[0]]).length) > 0)) return;
^
third_party/javascript/d3/v4/d3.js:3446: WARNING - Variable referenced before declaration: m
if (!((n = series.length) > 0) || !((m = (s0 = series[order[0]]).length) > 0)) return;
^
third_party/javascript/d3/v4/d3.js:3446: WARNING - Variable referenced before declaration: s0
if (!((n = series.length) > 0) || !((m = (s0 = series[order[0]]).length) > 0)) return;
^
third_party/javascript/d3/v4/d3.js:4574: WARNING - Variable referenced before declaration: n
if ((n = arguments.length - 2) > 0) for (var args = new Array(n), i = 0, n, t; i < n; ++i) args[i] = arguments[i + 2];
^
third_party/javascript/d3/v4/d3.js:4574: WARNING - Variable referenced before declaration: n
if ((n = arguments.length - 2) > 0) for (var args = new Array(n), i = 0, n, t; i < n; ++i) args[i] = arguments[i + 2];
^
third_party/javascript/d3/v4/d3.js:5393: WARNING - Variable referenced before declaration: i
if ((i = (x = p ? x.toExponential(p - 1) : x.toExponential()).indexOf("e")) < 0) return null; // NaN, �Infinity
^
third_party/javascript/d3/v4/d3.js:5492: WARNING - Variable referenced before declaration: match
if (!(match = re.exec(specifier))) throw new Error("invalid format: " + specifier);
^
third_party/javascript/d3/v4/d3.js:6707: WARNING - Variable referenced before declaration: i
if (r = v < u) i = u, u = v, v = i;
^
third_party/javascript/d3/v4/d3.js:6707: WARNING - Variable referenced before declaration: i
if (r = v < u) i = u, u = v, v = i;
^
third_party/javascript/d3/v4/d3.js:7522: WARNING - Variable referenced before declaration: data
data = new Array(this.size()), j = -1;
^
third_party/javascript/d3/v4/d3.js:7522: WARNING - Variable referenced before declaration: j
data = new Array(this.size()), j = -1;
^
third_party/javascript/d3/v4/d3.js:7524: WARNING - Variable referenced before declaration: data
return data;
^
third_party/javascript/d3/v4/d3.js:9454: WARNING - Variable referenced before declaration: n
if (!(n = circles.length)) return 0;
^
third_party/javascript/d3/v4/d3.js:9619: WARNING - Variable referenced before declaration: children
if (children = node.children) {
^
third_party/javascript/d3/v4/d3.js:10237: WARNING - Variable referenced before declaration: rows
if ((rows = parent._squarify) && (rows.ratio === ratio)) {
^
third_party/javascript/d3/v4/d3.js:10237: WARNING - Variable referenced before declaration: rows
if ((rows = parent._squarify) && (rows.ratio === ratio)) {
^
third_party/javascript/d3/v4/d3.js:12370: WARNING - Variable referenced before declaration: dp
dp = (dp = p1[0] - p0[0]) * dp + (dp = p1[1] - p0[1]) * dp,
^
third_party/javascript/d3/v4/d3.js:12370: WARNING - Variable referenced before declaration: dp
dp = (dp = p1[0] - p0[0]) * dp + (dp = p1[1] - p0[1]) * dp,
^
third_party/javascript/d3/v4/d3.js:12370: WARNING - Variable referenced before declaration: dp
dp = (dp = p1[0] - p0[0]) * dp + (dp = p1[1] - p0[1]) * dp,
^
third_party/javascript/d3/v4/d3.js:12370: WARNING - Variable referenced before declaration: dp
dp = (dp = p1[0] - p0[0]) * dp + (dp = p1[1] - p0[1]) * dp,
^
third_party/javascript/d3/v4/d3.js:12371: WARNING - Variable referenced before declaration: dl
dl = (dl = l1[0] - l0[0]) * dl + (dl = l1[1] - l0[1]) * dl;
^
third_party/javascript/d3/v4/d3.js:12371: WARNING - Variable referenced before declaration: dl
dl = (dl = l1[0] - l0[0]) * dl + (dl = l1[1] - l0[1]) * dl;
^
third_party/javascript/d3/v4/d3.js:12371: WARNING - Variable referenced before declaration: dl
dl = (dl = l1[0] - l0[0]) * dl + (dl = l1[1] - l0[1]) * dl;
^
third_party/javascript/d3/v4/d3.js:12371: WARNING - Variable referenced before declaration: dl
dl = (dl = l1[0] - l0[0]) * dl + (dl = l1[1] - l0[1]) * dl;
^
third_party/javascript/d3/v4/d3.js:13682: WARNING - Variable referenced before declaration: w
w = atan2(sqrt$1((w = y0 * z - z0 * y) * w + (w = z0 * x - x0 * z) * w + (w = x0 * y - y0 * x) * w), x0 * x + y0 * y + z0 * z);
^
third_party/javascript/d3/v4/d3.js:13682: WARNING - Variable referenced before declaration: w
w = atan2(sqrt$1((w = y0 * z - z0 * y) * w + (w = z0 * x - x0 * z) * w + (w = x0 * y - y0 * x) * w), x0 * x + y0 * y + z0 * z);
^
third_party/javascript/d3/v4/d3.js:13682: WARNING - Variable referenced before declaration: w
w = atan2(sqrt$1((w = y0 * z - z0 * y) * w + (w = z0 * x - x0 * z) * w + (w = x0 * y - y0 * x) * w), x0 * x + y0 * y + z0 * z);
^
third_party/javascript/d3/v4/d3.js:13682: WARNING - Variable referenced before declaration: w
w = atan2(sqrt$1((w = y0 * z - z0 * y) * w + (w = z0 * x - x0 * z) * w + (w = x0 * y - y0 * x) * w), x0 * x + y0 * y + z0 * z);
^
third_party/javascript/d3/v4/d3.js:13682: WARNING - Variable referenced before declaration: w
w = atan2(sqrt$1((w = y0 * z - z0 * y) * w + (w = z0 * x - x0 * z) * w + (w = x0 * y - y0 * x) * w), x0 * x + y0 * y + z0 * z);
^
third_party/javascript/d3/v4/d3.js:13682: WARNING - Variable referenced before declaration: w
w = atan2(sqrt$1((w = y0 * z - z0 * y) * w + (w = z0 * x - x0 * z) * w + (w = x0 * y - y0 * x) * w), x0 * x + y0 * y + z0 * z);
^
third_party/javascript/d3/v4/d3.js:14031: WARNING - Variable referenced before declaration: n
if ((n = segment.length - 1) <= 0) return;
^
third_party/javascript/d3/v4/d3.js:14098: WARNING - Variable referenced before declaration: n
if (!(n = array.length)) return;
^
third_party/javascript/d3/v4/d3.js:14822: WARNING - Variable referenced before declaration: m
if (!(m = (ring = polygon[i]).length)) continue;
^
third_party/javascript/d3/v4/d3.js:14822: WARNING - Variable referenced before declaration: ring
if (!(m = (ring = polygon[i]).length)) continue;
^
third_party/javascript/d3/v4/d3.js:14831: WARNING - Variable referenced before declaration: lambda1
for (var j = 0; j < m; ++j, lambda0 = lambda1, sinPhi0 = sinPhi1, cosPhi0 = cosPhi1, point0 = point1) {
^
third_party/javascript/d3/v4/d3.js:14831: WARNING - Variable referenced before declaration: sinPhi1
for (var j = 0; j < m; ++j, lambda0 = lambda1, sinPhi0 = sinPhi1, cosPhi0 = cosPhi1, point0 = point1) {
^
third_party/javascript/d3/v4/d3.js:14831: WARNING - Variable referenced before declaration: cosPhi1
for (var j = 0; j < m; ++j, lambda0 = lambda1, sinPhi0 = sinPhi1, cosPhi0 = cosPhi1, point0 = point1) {
^
third_party/javascript/d3/v4/d3.js:14831: WARNING - Variable referenced before declaration: point1
for (var j = 0; j < m; ++j, lambda0 = lambda1, sinPhi0 = sinPhi1, cosPhi0 = cosPhi1, point0 = point1) {
I appreciate that this pattern may be considered poor coding style, but var hoisting is a standard feature of JavaScript, and—with apologies—I am probably not willing to spend the necessary effort to rewrite the 50+ occurrences of this pattern to fix unsafe optimizations in the Closure Compiler. As I hope you understand, time is a precious resource even for open-source projects, and I’d rather be working on forward-looking enhancements like new features and bug fixes (that are not specific to a single tool).
I’d probably be more inclined to rewrite everything to adopt let and other ES2015 features, and then use Bublé to transpile it to ES5 for compatibility. Unfortunately, though, Bublé doesn’t handle this case correctly: it converts the let to a var and you don’t get the runtime ReferenceError. That makes me a little nervous and it might just be better to wait until I can require ES2015 at runtime.
Perhaps you can workaround this issue by passing the code through UglifyJS before giving it to the Closure Compiler? There are other tools for programmatic rewriting of code that might work, too… Or if this is a showstopper for using D3, I’d be willing to review pull requests to the affected modules.
The intended pattern for use with Closure Compiler would be to compile your code with a d3 externs file and then serve d3.min separately (or concat it + your minified code), not to compile d3 with your code.
Closure enforces a Java-y, or more weakly put, a particular, style that it is not expected every third-party JS author will have followed. That's where externs come into play, though, rather than patching the library (probably incompletely) ad-hoc.
(To be fair, I don't see a very comprehensive externs file anywhere out there.)
I've been experimenting with compiling d3 together with the caller code with promising results. After writing externs (http://pastebin.com/A4an42b2) and applying a small patch which modifies the bootstrapping and suppresses some warnings, I could enable all ADVANCED_OPTIMIZATIONS passes except for function inlining. Dead code removal works with 60% efficiency. Currently my blockers are the limited var hoisting support in Closure Compiler, and a compiler crashing bug triggered by the changes in v4.2.3.
I might be able to automate the fixes needed in d3. Working on that approach...
Sent the first PR: https://github.com/d3/d3-array/pull/44
If that is accepted then I'll continue fixing and sending PRs.
To be clear, https://github.com/d3/d3-array/pull/44 was not accepted so we fixed the compiler to do the right thing instead, which is really a more correct fix anyway.
Thank you!