javascript Chrome Profiler 中的“未优化”警告是什么意思?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22509262/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 23:16:59  来源:igfitidea点击:

What do the "Not optimized" warnings in the Chrome Profiler mean?

javascriptgoogle-chromeoptimizationgoogle-chrome-devtoolsdeveloper-tools

提问by Luke

When I use the Developer Tools in Chrome to collect JavaScript CPU Profiles, I am getting two mysterious warnings on functions:

当我使用 Chrome 中的开发者工具收集 JavaScript CPU 配置文件时,我收到两个关于函数的神秘警告:

  • Not optimized: optimized too many times
  • Not optimized: inlining bailed out
  • 未优化:优化次数过多
  • 未优化:内联退出

What do these actually mean? and what are some possible solutions?

这些实际上是什么意思?有哪些可能的解决方案?

Another one I've seen is Not optimized: TryCatchStatement, but that makes sense. Solution is to remove the try-catch.

我见过的另一个是未优化的: TryCatchStatement,但这是有道理的。解决方案是删除try-catch。

The closest attempt at an explanation I've found so far was this - https://github.com/GoogleChrome/devtools-docs/issues/53

迄今为止我发现的最接近的解释尝试是这个 - https://github.com/GoogleChrome/devtools-docs/issues/53

回答by David

  1. I believe that "Not optimized: optimized too many times" refers to when the chrome optimizer keeps reoptimizing a function.

    https://groups.google.com/forum/#!topic/v8-users/_oZ4fUSitRY

    If I recall correctly, there are several things that can cause this including parameters that change type, I'll try to dig up the link.

    This one is somewhat cryptic and fixes will depend on your code. I've had this pop up in my code many times and sometimes I just can't fix it.

  2. 'Not optimized: inlining bailed out' Seems answered in the link you posted.

  3. For the try/catch, a non-exhaustive but useful list of Chrome optimization quirks can be found on this github page:

    https://github.com/petkaantonov/bluebird/wiki/Optimization-killers

    This page mentions that try/catches are not currently optimized:

    • Generator functions
    • Functions that contain a for-of statement
    • Functions that contain a try-catch statement
    • Functions that contain a try-finally statement
    • Functions that contain a compound let assignment
    • Functions that contain a compound const assignment
    • Functions that contain object literals that contain proto, or get or set declarations.
  1. 我相信“未优化:优化太多次”是指 Chrome 优化器不断重新优化功能。

    https://groups.google.com/forum/#!topic/v8-users/_oZ4fUSitRY

    如果我没记错的话,有几件事可能会导致这种情况,包括改变类型的参数,我会尝试挖掘链接。

    这个有点神秘,修复将取决于您的代码。我的代码中多次出现这个问题,有时我无法修复它。

  2. '未优化:内联退出'似乎在您发布的链接中得到了回答。

  3. 对于 try/catch,可以在此 github 页面上找到非详尽但有用的 Chrome 优化怪癖列表:

    https://github.com/petkaantonov/bluebird/wiki/Optimization-killers

    此页面提到 try/catchs 当前未优化:

    • 发电机功能
    • 包含 for-of 语句的函数
    • 包含 try-catch 语句的函数
    • 包含 try-finally 语句的函数
    • 包含复合 let 赋值的函数
    • 包含复合常量赋值的函数
    • 包含包含proto或 get 或 set 声明的对象字面量的函数。

回答by Paul Irish

The explanations for these bailout reasons are being crowdsourced and documented in this github thread: https://github.com/GoogleChrome/devtools-docs/issues/53

这些救助原因的解释正在众包并记录在此 github 线程中:https: //github.com/GoogleChrome/devtools-docs/issues/53

The short explanation: V8 will not try to optimize functions with some constructs, try/catch block is one example, the full list may change over time as the engine evolves. It can also give up if it tries to optimize and then has to deoptimize some hot function too many times (e.g. because of the type feedback being different each time the function executes).

简短说明:V8 不会尝试使用某些构造优化函数,try/catch 块就是一个例子,完整列表可能会随着引擎的发展而改变。如果它尝试优化然后不得不多次去优化某个热函数,它也可能放弃(例如,因为每次函数执行时类型反馈都不同)。

回答by Timo K?hk?nen

I had a

我曾有一个

function generate_year_blob(year,action,callback){ ... do_blob({act: action, cb:callback, ...}) ... }

function generate_year_blob(year,action,callback){ ... do_blob({act: action, cb:callback, ...}) ... }

and I called it always using only one parameter like this generate_year_blob(this_year).

我总是只使用一个这样的参数来调用它generate_year_blob(this_year)

Those action(expected to be string) and callback(expected to be function) were passed to do_blob()function.

那些action(预期为字符串)和callback(预期为函数)被传递给do_blob()函数。

When I changed the the call from generate_year_blob(this_year)to generate_year_blob(this_year,'',null), the 'Not optimized: optimized too many times' warning disappeared.

当我将调用从 更改generate_year_blob(this_year)为 时generate_year_blob(this_year,'',null),“未优化:优化太多次”警告消失了。

I didn't find this out immediately, because there were many similar functions generate_month_blob(...), generate_day_blob(...)etc. which were called with all parameters defined.

我没有立刻发现这一点,因为有许多类似的功能generate_month_blob(...)generate_day_blob(...)等等。这被称为与定义的所有参数。

回答by Hampus

The first one is likely because the engine has optimised it, but then found the optimisation no good for some reason (maybe the return type varies over time, etc).

第一个可能是因为引擎对其进行了优化,但后来发现由于某种原因优化没有用(可能返回类型随时间变化等)。

Enabling the flags --trace-opt and --trace-deopt should help you pinpoint.

启用标志 --trace-opt 和 --trace-deopt 应该可以帮助您查明。

My excuses if the links provided in the comments already pointed you there.

如果评论中提供的链接已经指向你那里,我的借口。

回答by Joshua McCready

I was getting a lot of the 'Not optimized: optimized too many times' warnings and these functions were running much slower than they should have.

我收到了很多“未优化:优化太多次”的警告,而这些功能的运行速度比它们应有的要慢得多。

I was able to fix these functions by doing these things:

我能够通过做这些事情来修复这些功能:

  1. Removing unused variable declarations

  2. Removing function calls from loops that iterate many times (>1000 i suspect)

  1. 删除未使用的变量声明

  2. 从迭代多次的循环中删除函数调用(我怀疑 >1000)