Chrome的V8标记功能可以一遍又一遍地进行优化,直到放弃为止

Chrome's V8 marks function for optimization over and over again till it gives up

本文关键字:一遍 优化 放弃 V8 Chrome 功能      更新时间:2023-09-26

我有一个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 置于这种状态?

提前感谢!

查看痕迹,似乎每次detectSingleScale都是一个新的闭包。它每次都通过OSR(堆栈替换)进行优化,因此它没有缓存非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

V8 团队目前正在优化这种情况:Chrome 问题跟踪器。

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

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