fix(parser): performance issue due to debug code#17834
Conversation
|
Build successful! You can test your changes in the REPL here: https://babeljs.io/repl/build/61034 |
|
I actually saw this performance change, but mistakenly thought it was unrelated to our code.🤦♂️ |
|
commit: |
|
I have also done a At this point I don't think we have other code paths affected by the build configuration change. |
Thank you. This website is great, I will keep an eye on it. |
The optional chains do not affect Babel 7 because they are transpiled.
| if (parser != null) { | ||
| if (parser.optionFlags & OptionFlags.Ranges) this.range = [pos, 0]; | ||
| if (parser.filename) this.loc.filename = parser.filename; | ||
| } |
There was a problem hiding this comment.
I'm surprised this affects performance, could this be improved in V8?
There was a problem hiding this comment.
This change is guided from the CPU profile of running many-async-arrow-tests compared between Babel 7 and Babel 8. Previously, there will be 4.4% (around 400) ticks spent on the JavaScript function *Node, with this change, V8 more eagerly optimizes the Node constructor to machine code and therefore it will be only 0 or 1 tick spent on *Node.
Note that I can not observe performance changes when running real-case. Inspecting the CPU profile, it seems that V8 eventually will optimize the Node constructor.
In Babel 7 the optional chain is tranpiled so Babel 7 is not affected. In Babel 8 they are not. It seems that optional chain in hot path has an impact on the timing of the optimization. As we know that Node is a hot path, and the second ?. check is useless if the parser is not nullish (most case), we can restructure the code to avoid the duplicate nullish check and also let V8 more eagerly optimize it.
Here is the benchmark result between 7.29.0 and 8.0.0-rc.2 when running
benchmark/babel-parser/many-async-arrows/bench.mjsIn this PR we fixed a performance regression introduced in #17756, where we only inlined the
NODE_ENVenvironment when building the@babel/standalone. This unfortunately impacts@babel/parserbecause we have debug code in the hot pathfinishNodeAt, and although the debug code error is probably not activated for end users, theprocess.envaccess has to be supported from a C++ runtime, which incurs a great toll on the performance.Here is the benchmark result between 7.29.0 and this PR, I will investigate if there are any other performance issues and open new PRs.