Sub topic 15
Watching the Engine Work With V8 Flags and Native Syntax
This chapter assumes you can run scripts with Node.js from the command line. It also assumes you know the ideas from earlier chapters: hidden classes (V8 calls them maps), inline caches, tiered compilation, and deoptimization. Here you will check those ideas against a real engine instead of taking them on trust.
Two Kinds of Tooling
Node.js passes V8 options straight through to the engine. They come in two kinds:
- Trace flags such as
--trace-optand--trace-deoptprint log lines while your program runs. - Native syntax is enabled by
--allow-natives-syntax. It lets you call internal V8 functions written with a%prefix, such as%HaveSameMap(a, b).
Run node --v8-options to see every flag your Node version supports.
Matching Questions to Tools
| Question | Tool |
|---|---|
| Do two objects share a hidden class? | `%HaveSameMap(a, b)` |
| Is an object in dictionary mode? | `%HasFastProperties(o)` |
| What map and elements kind does a value have? | `%DebugPrint(value)` |
| When is a function optimized? | `--trace-opt` |
| Why was optimized code thrown away? | `--trace-deopt` |
| What bytecode did Ignition produce? | `--print-bytecode --print-bytecode-filter=name` |
Forcing Optimization on Demand
V8 normally waits until a function is hot before it optimizes it. That makes experiments slow and their results hard to predict. You can force optimization with two native calls. First, %PrepareFunctionForOptimization tells V8 to keep the function's type feedback. Then %OptimizeFunctionOnNextCall makes the next call compile the function with TurboFan.
// Run with: node --allow-natives-syntax --trace-opt --trace-deopt watch.js
function add(a, b) {
return a + b;
}
%PrepareFunctionForOptimization(add);
add(1, 2);
add(3, 4); // feedback: small integers
%OptimizeFunctionOnNextCall(add);
add(5, 6); // compiled with TurboFan here
add('a', 'b'); // breaks the integer assumption, triggers a deoptThe output includes a line like [compiling method ... add ... using TurboFan], followed by a [bailout ... reason: ...] line or a deoptimizing line that names add. The exact wording varies between V8 versions, so search the output for the function name and the words "optimizing", "bailout", or "deoptimizing".
Reading `%DebugPrint`
// Run with: node --allow-natives-syntax inspect.js
const arr = [1, 2, 3];
%DebugPrint(arr);
arr.push(4.5);
%DebugPrint(arr);The first dump reports PACKED_SMI_ELEMENTS and the second reports PACKED_DOUBLE_ELEMENTS. Each dump also shows the map's address, and you can compare addresses to spot a shape change. Release builds of Node print a short summary. Debug builds of V8 print much more.
Common Mistakes
- Trusting one run. Garbage collection, background compilation, and timing all change the output. Repeat the run before drawing a conclusion.
- Measuring a toy. A loop that produces a "perfect" trace may tell you nothing about your real code path. Profile the real application first.
- Forgetting the prepare call. In current V8, calling
%OptimizeFunctionOnNextCallwithout%PrepareFunctionForOptimizationmay do nothing or give misleading results.
Exercise
You run a script with --trace-deopt and see repeated deopt lines for a function area, which receives objects from two different code paths. Which native function would you use next to test whether those objects differ in shape, and how?
Show answer
Use %HaveSameMap(objA, objB) under --allow-natives-syntax, passing one object from each code path. A false result shows the objects have different hidden classes. That points to differences in property order or late-added properties as the likely cause of the deopts.
Exercise
Why does node script.js fail with a SyntaxError when the script contains %HasFastProperties(o)?
Show answer
The % call syntax is only parsed when V8 runs with --allow-natives-syntax. Without that flag, % at the start of an expression is invalid JavaScript.