facebookarchive/prepack

Eliminate unnecessary object allocations for residual functions

Open

#495 建立於 2017年5月2日

在 GitHub 查看
 (3 留言) (0 反應) (1 負責人)JavaScript (520 fork)batch import
enhancementhelp wantedserializer

倉庫指標

Star
 (14,268 star)
PR 合併指標
 (30 天內沒有已合併 PR)

描述

Prepacking

function() {
    function f() {
        let obj = { f: 42 };
        return function() { return obj.f; }
    }
    g = f();
})();

currently yields the following:

(function () {
    var _1 = {
        f: 42
    };

    function _0() {
        return _1.f;
    }

    g = _0;
})();

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:

(function () {
    let _1_f = 42;
    function _0() {
        return _1_f;
    }

    g = _0;
})();

Or even better:

(function () {
    function _0() {
        return 42;
    }

    g = _0;
})();

If instead of 42 the field obj.f may either hold an object reference or may be mutated by the residual code, then another variable similar to _1 would be needed.

貢獻者指南