Upgrade to Pro — share decks privately, control downloads, hide ads and more …

V8 javascript engine for フロントエンドデベロッパー

V8 javascript engine for フロントエンドデベロッパー

V8 javascript engineがjavascriptをどのように処理しているかの解説。

More Decks by Taketoshi Aono(青野健利 a.k.a brn)

Other Decks in Programming

Transcript

  1. V8הכ V8ך暴䗙 •  넝鸞 •  Hidden Classהְֲⰻ鿇㘗ءأذي •  Inline Caching

    •  ⚅➿ⴽך؝ٖٝؕٝز٥؎ًؙٝٔٝةٕGC •  غ؎ز؝٦س؎ٝة٦فٔة׾ⰻ詿2016䎃劣 •  㼎䘔CPUכMipsծArmծX64ծX86٥7
  2. Abstract Syntax Tree IF CONDITION THEN BLOCK EXPRESSION STATEMENT ASSIGN

    VAR PROXY (X) LITERAL (100) if  (x)  {  x = 100 
  3. AST Rewriter javascriptך圓俑דծSpreadؔلٖ٦ة٦הְֲ圓俑ָ֮׶ת ׅկ var x = [1,2,3]; var y

    = […x]; V8כֿך圓俑׾AltJSךزٓٝأػ؎ٓך圫חⰋֻⴽך圓俑ח 剅ֹ䳔ִג׃תְתׅկ
  4. AST Rewriter do { $R = []; for ($i of

    x) %AppendElement($R, $i); $R } // src/parsing/parser.cc ֿך״ֲחծ瘝⣣ךdo圓俑הfor-of圓俑ח剅ֹ䳔ִ ג׃תְתׅկ
  5. // 擬似的なjavascriptコード ! var Bytecodes = [0,1,2,3] var index =

    0; function dispatch(next) { BytecodeHandlers[next](); } const BytecodeHandlers = { ['0']() {...; dispatch(Bytecodes[index++])}, ['1']() {...; dispatch(Bytecodes[index++])}, ['2']() {...; dispatch(Bytecodes[index++])}, ['3']() {...; dispatch(Bytecodes[index++])} } const firstBytecode = Bytecodes[index++]; BytecodeHandlers[firstBytecode](firstBytecode);
  6. Igniton Dispatch Table 00 01 02 04 08 0f 10

    10 Node Node Node MachineCode MachineCode IGNITION_HANDLER Entry Function(Machine Code) グラフからコードを生成 生成したコードを、 DispatchTableの対応する バイトコードのインデックスへ 登録
  7. Visitor Pattern class Visitor { void Visit(Ast* ast) { ast->Accept(this);

    } void VisitXXX(XXX* ast) { ast->property->accept(this); } } class Ast(XXX) { void Accept(Visitor* visitor) { visitor->VisitXXX(this) } }
  8. IGNITION_HANDLER(JumpIfToBooleanFalse, InterpreterAssembler) {! Node* value = GetAccumulator();! // Accumulatorの値を取得! Node*

    relative_jump = BytecodeOperandUImmWord(0);! // 引数のoperandを取得! Label if_true(this), if_false(this);! BranchIfToBooleanIsTrue(value, &if_true, &if_false);! // valueがtrueならif_true、falseならif_false! Bind(&if_true);! Dispatch();! Bind(&if_false);! // operandのbytecodeまでjump! Jump(relative_jump);! }!
  9. IGNITION_HANDLER(ForInContinue, InterpreterAssembler) {! ...! Branch(WordEqual(index, cache_length), &if_true, &if_false);! Bind(&if_true);! {!

    SetAccumulator(BooleanConstant(false));! Goto(&end);! }! Bind(&if_false);! {! SetAccumulator(BooleanConstant(true));! Goto(&end);! }! ...! }!
  10. #define __ ACCESS_MASM(masm)! ! static void Generate_StackOverflowCheck(...) {! ! __

    LoadRoot(kScratchRegister, Heap::kRealStackLimitRootIndex);! __ movp(scratch, rsp);! ! __ subp(scratch, kScratchRegister);! __ sarp(scratch, Immediate(kPointerSizeLog2));! ! __ cmpp(scratch, num_args);! ! __ j(less_equal, stack_overflow, stack_overflow_distance);! }!
  11. Hidden Class ׉ֿדV8כؔـآؙؑزךٖ؎،ؐزח岣湡׃ת׃׋կ ⢽ִלծ function Point(x, y) { this.x =

    x; this.y = y }; var point = new Point(1, 1); ֿ׸׾Point㘗ה׃ג钠陎ׅ׷׋׭חⰻ鿇ד㘗׾䭯׏גְתׅկ
  12. Map V8דכ •  ずׄفٗػذ؍せ •  ずׄ㘗 •  ずׄ갫䎷 ד㹀纏ׁ׸׋㜥さכずؙׄٓأה׫זׅ״ֲחׅ׷׋׭ծMapהְ ֲ嚊䙀׾䭯׍鴥׫ת׃׋կ

    ֿךMapכؔـآؙؑز欰䧭儗ծؙٓأד֮׸ל؝ٝأزؙٓة㹋 遤㸣✪儗ח欰䧭ׁ׸ծؔـآؙؑزךٖ؎،ؐز׾⥂䭯׃תׅկ ずׄ圓鸡ד֮׸לծずׄMapָⰟ剣ׁ׸תׅկ
  13. Map function Point(x, y) { this.x = x; this.y =

    y; } Map FixedArray [ {a: {offset: 0}}, {b: {offset: 1}} ]
  14. Map Transition function Point(x, y) { this.x = x; this.y

    = y; } Map FixedArray [ {x: {offset: 0}}, {y: {offset: 1}}, transiOon: {address: …} ] var x = new Point(1, 1); x.z = 1; Map FixedArray [ {x: {offset: 0}}, {y: {offset: 1}}, {z: {offset: 2}} ] transition
  15. class X {add() {return 1}}! class Y {add() {return 2}}!

    var m = [new X(), new X(), new Y()];! for (let i = 0; i < m.length; i++) {! m[i].add();! }! !
  16. (Load/Store)IC_Miss 二回目以降 V8 add Receiver (y) Map Cached address (x)

    1. Search 2. Compare Map x !== y 3. (Load/Store)IC_Miss
  17. On Stack Replacement for (let i = 0; i <

    10000; i++) { doSomething(i); } JumpLoop LoopHeader SlowCode FastCode PC(فؚٗٓيؕؐٝة)׾縧ֹ䳔ִ׷
  18. TurboFan TurboFanך剑黝⻉ٔأزדׅկ •  Loop peeling / Loop elimination •  Escape

    analysis •  Lowering •  Effect and control linearize •  Dead code elimination •  Control flow optimization •  Memory optimization