-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile-deopt.mjs
More file actions
227 lines (197 loc) · 7.94 KB
/
profile-deopt.mjs
File metadata and controls
227 lines (197 loc) · 7.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
#!/usr/bin/env node
/**
* Deoptimization & Inline Cache Analysis
*
* Detects V8 deoptimizations and polymorphic inline caches (ICs) that
* silently destroy performance. These are the #1 cause of "the code looks
* fast but isn't" bugs.
*
* Usage:
* node scripts/profile-deopt.mjs # all benchmarks
* node scripts/profile-deopt.mjs --filter "flat array" # filtered
* node scripts/profile-deopt.mjs --target my-script.mjs # custom script
*
* Output:
* profiles/deopt-<timestamp>.log — filtered deopt events
*
* What to look for:
* - "eager" or "soft" deopts: V8 optimised a function then had to bail out
* - "lazy" deopts: speculative optimisation failed at runtime
* - Polymorphic/megamorphic ICs: object shape changes forcing slow lookups
*/
import { execFileSync } from "node:child_process";
import { mkdirSync, writeFileSync } from "node:fs";
import { resolve, join } from "node:path";
const ROOT = resolve(import.meta.dirname, "..");
const PROFILES_DIR = join(ROOT, "profiles");
const BENCH_FILE = join(ROOT, "packages/bridge/bench/engine.bench.ts");
// ── Parse args ───────────────────────────────────────────────────────────────
const args = process.argv.slice(2);
function getArg(name) {
const idx = args.indexOf(`--${name}`);
return idx !== -1 && idx + 1 < args.length ? args[idx + 1] : undefined;
}
const hasFlag = (name) => args.includes(`--${name}`);
const filter = getArg("filter");
const target = getArg("target");
if (hasFlag("help")) {
console.log(`
Deoptimization & IC Analysis Script
Options:
--filter <name> Only run benchmarks matching this substring
--target <file> Profile a custom script instead of bench harness
--help Show this help
Output:
profiles/deopt-<timestamp>.log — deoptimization events (filtered & annotated)
`);
process.exit(0);
}
// ── Setup ────────────────────────────────────────────────────────────────────
mkdirSync(PROFILES_DIR, { recursive: true });
const timestamp = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
const scriptToProfile = target ? resolve(target) : BENCH_FILE;
console.log(`\n🔬 Deoptimization & IC Analysis`);
console.log(` Script: ${scriptToProfile}`);
console.log(` Filter: ${filter ?? "(all benchmarks)"}`);
console.log();
// ── Run with --trace-deopt and other V8 flags ────────────────────────────────
const nodeArgs = [
"--experimental-transform-types",
"--conditions",
"source",
// Deoptimization tracing
"--trace-deopt",
// Also log when functions get optimised (useful for correlation)
"--trace-opt",
scriptToProfile,
];
const env = {
...process.env,
BRIDGE_PROFILE_FILTER: filter ?? "",
};
console.log("Running with V8 trace flags...");
let stdout, stderr;
try {
stdout = execFileSync("node", nodeArgs, {
cwd: ROOT,
env,
encoding: "utf-8",
maxBuffer: 100 * 1024 * 1024,
stdio: ["pipe", "pipe", "pipe"],
});
stderr = "";
} catch (e) {
stdout = e.stdout ?? "";
stderr = e.stderr ?? "";
}
// V8 trace output goes to stdout
const allOutput = stdout + "\n" + stderr;
const lines = allOutput.split("\n");
// ── Filter and analyze ───────────────────────────────────────────────────────
function isBridgeCode(line) {
return (
line.includes("bridge-core") ||
line.includes("bridge-compiler") ||
line.includes("bridge-stdlib") ||
line.includes("ExecutionTree") ||
line.includes("resolveWires") ||
line.includes("pullSingle") ||
line.includes("materializeShadows") ||
line.includes("schedule") ||
line.includes("callTool")
);
}
// Actual deoptimizations (bad) — these contain "deoptimiz" but NOT "for optimization"
const bridgeDeoptLines = lines.filter((line) => {
const lower = line.toLowerCase();
// Actual deopt events contain "deoptimiz" without "for optimization"
const isDeopt = lower.includes("[deoptimiz") || lower.includes("deopt:");
// Exclude "marking for optimization" which is good
const isOptMarking =
lower.includes("for optimization") || lower.includes("[compiling");
return isDeopt && !isOptMarking && isBridgeCode(line);
});
// Optimization events (good) — marking/compiling for optimization
const bridgeOptLines = lines.filter((line) => {
const lower = line.toLowerCase();
const isOpt = lower.includes("[marking") || lower.includes("[compiling");
return isOpt && isBridgeCode(line);
});
// Extract unique function names being optimized
const optimizedFns = new Set();
for (const line of bridgeOptLines) {
const match = line.match(/JSFunction (\w+)/);
if (match) optimizedFns.add(match[1]);
}
// Extract unique deoptimized functions
const deoptFns = new Set();
for (const line of bridgeDeoptLines) {
const match = line.match(/JSFunction (\w+)/);
if (match) deoptFns.add(match[1]);
}
// ── Output report ────────────────────────────────────────────────────────────
const report = [
`# Deoptimization Report — ${timestamp}`,
``,
`Script: ${scriptToProfile}`,
`Filter: ${filter ?? "(all)"}`,
``,
`## Summary`,
``,
`Total trace lines: ${lines.length}`,
`Bridge deopt events: ${bridgeDeoptLines.length}`,
`Bridge optimization events: ${bridgeOptLines.length}`,
``,
`Functions optimized: ${[...optimizedFns].join(", ") || "(none)"}`,
`Functions deoptimized: ${[...deoptFns].join(", ") || "(none)"}`,
``,
`## Deoptimization Events (Bridge code only)`,
``,
...(bridgeDeoptLines.length > 0
? bridgeDeoptLines
: [
"(none — no deoptimizations detected in Bridge code. Types are stable.)",
]),
``,
`## Optimization Events (Bridge code only)`,
``,
`These are GOOD — V8 is optimizing these functions:`,
``,
...(bridgeOptLines.length > 0 ? bridgeOptLines.slice(0, 100) : ["(none)"]),
``,
`## Interpretation Guide`,
``,
`### Deoptimization Types (BAD — these hurt performance)`,
`- "eager deopt": V8 had to bail out immediately — usually a type change`,
`- "soft deopt": V8 deoptimized but may re-optimize later — watch for repeats`,
`- "lazy deopt": Speculative optimization failed when the code actually ran`,
`- Multiple deopts on the same function: the function has unstable types`,
``,
`### Optimization Types (GOOD — these confirm V8 is optimizing)`,
`- "marking for optimization to MAGLEV": Mid-tier optimizing compiler`,
`- "marking for optimization to TURBOFAN_JS": Top-tier optimizing compiler`,
`- "compiling method": V8 is actively compiling optimized code`,
``,
`### What to do`,
`- If no deopts appear: your hot paths have stable types — this is ideal`,
`- If deopts appear: check for object shape changes, type mixing, or delete`,
`- If a function shows MAGLEV but not TURBOFAN: it may have too many code paths`,
``,
].join("\n");
const reportPath = join(PROFILES_DIR, `deopt-${timestamp}.log`);
writeFileSync(reportPath, report, "utf-8");
console.log(`\n✅ Deopt report: profiles/deopt-${timestamp}.log`);
console.log(` Bridge deopt events: ${bridgeDeoptLines.length}`);
console.log(` Bridge optimization events: ${bridgeOptLines.length}`);
console.log(
` Functions optimized: ${[...optimizedFns].join(", ") || "(none)"}`,
);
if (bridgeDeoptLines.length === 0) {
console.log(`\n ✨ No deoptimizations in Bridge code — types are stable.`);
} else {
console.log(
`\n ⚠️ Found ${bridgeDeoptLines.length} deoptimization(s) in: ${[...deoptFns].join(", ")}`,
);
console.log(` Check the report for details.`);
}
console.log();