facebookarchive/prepack

factorifyObjects inefficiency

Open

#914 创建于 2017年8月22日

在 GitHub 查看
 (3 评论) (0 反应) (1 负责人)JavaScript (520 fork)batch import
enhancementhelp wantedserializer

仓库指标

Star
 (14,268 star)
PR 合并指标
 (30 天内没有已合并 PR)

描述

The factorifyObjects feature in Prepack reduces code size by creating factory functions for common object creation patterns. For example,

// Copies of x: 2
// ^ When present in a serializer test case, this comment will make the test case fail if the prepacked code only mentions x once, instead of twice
(function() {
    a = { x: 0, y: 1 };
    b = { x: 0, y: 1 };
    c = { x: 0, y: 1 };
    d = { x: 0, y: 1 };
    e = { x: 0, y: 1 };
    A = { x: 0, y: 2 };
    B = { x: 0, y: 2 };
    C = { x: 0, y: 2 };
    D = { x: 0, y: 2 };
    E = { x: 0, y: 2 };
})();

prepacks to

(function () {
  function $_2() {
    return $_0(0, 2);
  }
  function $_1() {
    return $_0(0, 1);
  }
  function $_0(__0, __1) {
    return {
      x: __0,
      y: __1
    };
  }
  var _0 = $_1();
  a = _0;
  var _1 = $_1();
  b = _1;
  var _2 = $_1();
  c = _2;
  var _3 = $_1();
  d = _3;
  var _4 = $_1();
  e = _4;
  var _5 = $_2();
  A = _5;
  var _6 = $_2();
  B = _6;
  var _7 = $_2();
  C = _7;
  var _8 = $_2();
  D = _8;
  var _9 = $_2();
  E = _9;
})();

While correct, the two levels of factory functions seems a bit excessive. We should consider simply inlining $_0. This would save a good thousand function calls in a medium FB internal JavaScript program.

贡献者指南