Chrome的V8标记功能可以反复进行优化,直到它放弃为止

时间:2014-12-23 19:13:50

标签: javascript google-chrome optimization v8 profiler

我有一个detectSingleScale JavaScript函数,V8正在尝试对其进行优化,据我所知它无法对其进行优化。

使用--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置于这种状态?

提前致谢!

2 个答案:

答案 0 :(得分:4)

看一下跟踪似乎detectSingleScale每次都是一个新的闭包。它每次都通过OSR进行优化( on stack replacement ),因此它没有缓存非OSR版本。

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

下次再创建一个闭包时,首先得到didn't find optimized code消息 - 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

答案 1 :(得分:0)

V8团队目前正致力于优化此案例:Chrome issue tracker

您可以使用--mark_shared_functions_for_tier_up运行以使用正在进行的工作。然后应优化detectSingleScale

消息didn't find optimized code in optimized code map for不是很有用,最近已从代码库中删除。