javascript-Chrome的V8标记功能可反复优化,直到放弃

我有一个V8试图优化的detectSingleScale JavaScript函数,据我所知它无法优化它.

当使用–trace_deopt –trace_opt –trace_opt_verbose –code_comments运行Chrome时,我看到数百行日志,如下所示:

6087 [found optimized code for 0x1a9b67169161 <JS Function detectSingleScale (SharedFunctionInfo 0x145b5fa71bb1)> at OSR AST id 218]
6088 [didn't find optimized code in optimized code map for 0x145b5fa71bb1 <SharedFunctionInfo detectSingleScale>]
6089 [marking 0x1a9b6721ab91 <JS Function detectSingleScale (SharedFunctionInfo 0x145b5fa71bb1)> for recompilation, reason: hot and stable, ICs with typeinfo: 140/144 (97%), generic ICs: 0/144 (0%)]
6090 [found optimized code for 0x1a9b6721ab91 <JS Function detectSingleScale (SharedFunctionInfo 0x145b5fa71bb1)> at OSR AST id 218]
6091 [didn't find optimized code in optimized code map for 0x145b5fa71bb1 <SharedFunctionInfo detectSingleScale>]
6092 [marking 0x1a9b672c80f1 <JS Function detectSingleScale (SharedFunctionInfo 0x145b5fa71bb1)> for recompilation, reason: hot and stable, ICs with typeinfo: 140/144 (97%), generic ICs: 0/144 (0%)]
6093 [found optimized code for 0x1a9b672c80f1 <JS Function detectSingleScale (SharedFunctionInfo 0x145b5fa71bb1)> at OSR AST id 218]
6094 [didn't find optimized code in optimized code map for 0x145b5fa71bb1 <SharedFunctionInfo detectSingleScale>]
6095 [marking 0x1a9b67379db1 <JS Function detectSingleScale (SharedFunctionInfo 0x145b5fa71bb1)> for recompilation, reason: hot and stable, ICs with typeinfo: 140/144 (97%), generic ICs: 0/144 (0%)]

> V8标记了detectSingleScale函数以进行优化.
>然后说找到了优化版本.
>但随后它说找不到.整个过程一遍又一遍地重启.

优化代码的地址都不同.我想知道什么会触发这种V8行为.在什么情况下功能可以使V8处于这种状态?

提前致谢!

解决方法:

仔细观察一下踪迹,似乎detectSingleScale每次都是新的闭合.每次都通过OSR(在更换堆栈时)进行优化,因此不会缓存非OSR版本.

首次创建并运行闭包时,它会通过OSR进行优化,并将生成的代码放入缓存中.

下次再次创建闭包时,首先会发现找不到优化的代码消息-Factory :: NewFunctionFromSharedFunctionInfo [1]尝试查找非OSR版本(OSR ID设置为BailoutId :: None()),并且没有.找不到任何版本,因为唯一的优化版本是OSR版本.

然后,V8看到热循环并决定对OSR进行功能-这次,它在高速缓存中找到具有匹配OSR ID的已经优化的代码并使用它.

这是一个复制品来说明这一点

function foo() {
  print('! creating bar')
  var bar = function () {
    for (var i = 0; i < 100000; i++) {
      // OSR happens in this loop.
    }

    // Add a literal here to ensure we hit Runtime_NewClosure
    // instead of FastNewClosureStub - stub doesn't log anything.
    var a = [];
  }

  print('! running bar')
  bar();
  print('-- done')
}

foo();
foo();
foo();

当我使用d8运行此文件时,我得到:

$out/ia32.release/d8 --trace-opt test.js
! creating bar
! running bar
[marking 0x4301b705 <JS Function bar (SharedFunctionInfo 0x4c208765)> for recompilation, reason: small function, ICs with typeinfo: 2/2 (100%), generic ICs: 0/2 (0%)]
[compiling method 0x4301b705 <JS Function bar (SharedFunctionInfo 0x4c208765)> using Crankshaft]
[optimizing 0x4301b705 <JS Function bar (SharedFunctionInfo 0x4c208765)> - took 0.082, 0.119, 0.047 ms]
-- done
! creating bar
[didn't find optimized code in optimized code map for 0x4c208765 <SharedFunctionInfo bar>]
! running bar
[marking 0x4301bdb5 <JS Function bar (SharedFunctionInfo 0x4c208765)> for recompilation, reason: small function, ICs with typeinfo: 2/2 (100%), generic ICs: 0/2 (0%)]
[found optimized code for 0x4301bdb5 <JS Function bar (SharedFunctionInfo 0x4c208765)> at OSR AST id 10]
-- done
! creating bar
[didn't find optimized code in optimized code map for 0x4c208765 <SharedFunctionInfo bar>]
! running bar
[marking 0x4301be35 <JS Function bar (SharedFunctionInfo 0x4c208765)> for recompilation, reason: hot and stable, ICs with typeinfo: 2/2 (100%), generic ICs: 0/2 (0%)]
[found optimized code for 0x4301be35 <JS Function bar (SharedFunctionInfo 0x4c208765)> at OSR AST id 10]
-- done

[1] https://github.com/v8/v8-git-mirror/blob/master/src/factory.cc#L1396-L1397

上一篇:Python中的操作顺序和快速计算


下一篇:javascript-是否可以指示浏览器首先绘制页面的哪些元素?