Paul Borza
Paul Borza

Reputation: 191

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

I have a detectSingleScale JavaScript function which V8 is trying to optimize and as far as I can tell it can't optimize it.

When running Chrome with --trace_deopt --trace_opt --trace_opt_verbose --code_comments I see hundreds of log lines like below:

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%)]

The addresses for the optimized code are all different. I'm wondering what could trigger such a V8 behavior. Under which circumstance can a function put V8 in such a state?

Thanks in advance!

Upvotes: 3

Views: 918

Answers (2)

user835611
user835611

Reputation: 2366

The V8 team currently works on optimizing this case: Chrome issue tracker.

You can run with --mark_shared_functions_for_tier_up to use the work in progress. Then detectSingleScale should be optimized.

The message didn't find optimized code in optimized code map for was not very useful and was deleted from the code base recently.

Upvotes: 0

Vyacheslav Egorov
Vyacheslav Egorov

Reputation: 10492

Looking at the trace it seems that detectSingleScale is a new closure every time. It is optimized every time via OSR (on stack replacement), so it does not have an non OSR version cached.

The first time you create and run the closure it gets optimized via OSR and resulting code it put into a cache.

Next time you create a closure again you first get didn't find optimized code message - Factory::NewFunctionFromSharedFunctionInfo[1] tries to look up a non-OSR version (OSR id is set to BailoutId::None()) and doesn't find any because the only optimized version is OSR one.

Then V8 sees the hot loop and decides to OSR the function - this time it finds already optimized code with a matching OSR id in the cache and uses it.

Here is a repro to illustrate this

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();

When I run this file with d8 I get:

$ 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

Upvotes: 4

Related Questions