Open NTillmann opened 7 years ago
This optimization will require computing a fixed point in the ResidualHeapVisitor.
Consider the following:
(function() {
function f() {
let obj = {
g1: function() {},
g2: function() {} };
return function() { return obj.g2; }
}
global_g2 = f();
})();
Be careful: Different declarative bindings might point to the same value.
(function() {
function f(obj) {
return function() { return obj.g2; }
}
let obj = {
g1: function() {},
g2: function() {} };
global_g2_1 = f(obj);
global_g2_2 = f(obj);
})();
Note that we need to traverse all residual functions to discover if we can apply this transformation at all:
(function() {
function f(obj) { return obj.p; }
function g(obj) { Object.assign(obj, { p: 42 }); }
let obj = { p: 23 };
global.f = f;
global.g = g;
})();
Prepacking
currently yields the following:
The object allocation for
_1
is silly. #494 suggests delaying the allocation, but in this example, it could be completely eliminated. Prepack could generate the following code:Or even better:
If instead of
42
the fieldobj.f
may either hold an object reference or may be mutated by the residual code, then another variable similar to_1
would be needed.