// Attributes a V8 CPU profile of the fixture app to Dart source files through // the build's source map (`flutter build web --profile --source-maps`), for // tool/perf/interaction_bench.mjs --cpuprofile. // // Each sample's stack is mapped frame by frame (a JS function's start position // to its Dart file and line); a file or function counts once per sample, so // the numbers are inclusive milliseconds. Frames without a mapping (CanvasKit, // the browser) are grouped by their script. import { readFile } from 'node:fs/promises'; const B64 = Object.fromEntries([...'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'].map((c, i) => [c, i])); function decodeVlq(segment) { const out = []; let value = 0; let shift = 0; for (const c of segment) { const digit = B64[c]; value += (digit & 31) << shift; if (digit & 32) { shift += 5; continue; } out.push(value & 1 ? -(value >>> 1) : value >>> 1); value = 0; shift = 0; } return out; } /** Loads `/main.dart.js.map`: lookup(line, column) -> { source, line } or null. */ export async function loadSourceMap(dir) { const map = JSON.parse(await readFile(`${dir}/main.dart.js.map`, 'utf8')); const lines = []; let src = 0; let srcLine = 0; let srcCol = 0; let name = 0; map.mappings.split(';').forEach((line) => { const segments = []; let col = 0; for (const seg of line.split(',')) { if (!seg) continue; const v = decodeVlq(seg); col += v[0]; if (v.length >= 4) { src += v[1]; srcLine += v[2]; srcCol += v[3]; if (v.length >= 5) name += v[4]; segments.push([col, src, srcLine, v.length >= 5 ? name : -1]); } } lines.push(segments); }); return (line, column) => { const segs = lines[line]; if (!segs?.length) return null; let lo = 0; let hi = segs.length - 1; let hit = null; while (lo <= hi) { const mid = (lo + hi) >> 1; if (segs[mid][0] <= column) { hit = segs[mid]; lo = mid + 1; } else hi = mid - 1; } hit ??= segs[0]; const source = map.sources[hit[1]] .replace(/^.*\/packages\/flutter\/lib\/src\//, 'flutter/') .replace(/^.*\/pipelines_app\/lib\//, 'app:') .replace(/^(\.\.\/)+lib\//, 'app:') .replace(/^org-dartlang-sdk:\/\/\/(lib\/)?/, 'sdk:'); return { source, line: hit[2] + 1, name: hit[3] >= 0 ? map.names[hit[3]] : null }; }; } /** * Inclusive ms per Dart file and per Dart function (`file:line name`) across * the profile's samples; [filter] keeps the keys worth printing. */ export function attribute(profile, lookup, { n = 30, filter = () => true } = {}) { const byId = new Map(profile.nodes.map((node) => [node.id, node])); const parent = new Map(); for (const node of profile.nodes) for (const c of node.children ?? []) parent.set(c, node.id); const where = new Map(); for (const node of profile.nodes) { const f = node.callFrame; const mapped = f.url.endsWith('main.dart.js') ? lookup(f.lineNumber, f.columnNumber) : null; where.set(node.id, mapped ? { file: mapped.source, fn: `${mapped.source}:${mapped.line} ${f.functionName}` } : { file: `(${f.url.split('/').pop() || f.functionName})`, fn: null }); } const files = new Map(); const fns = new Map(); let busy = 0; profile.samples.forEach((id, i) => { const dt = (profile.timeDeltas[i + 1] ?? 0) / 1000; if (['(idle)', '(program)'].includes(byId.get(id).callFrame.functionName)) return; busy += dt; const seenFiles = new Set(); const seenFns = new Set(); for (let cur = id; cur !== undefined; cur = parent.get(cur)) { const w = where.get(cur); if (!seenFiles.has(w.file)) { seenFiles.add(w.file); files.set(w.file, (files.get(w.file) ?? 0) + dt); } if (w.fn && !seenFns.has(w.fn)) { seenFns.add(w.fn); fns.set(w.fn, (fns.get(w.fn) ?? 0) + dt); } } }); const r1 = (x) => Math.round(x * 10) / 10; const top = (m) => [...m].filter(([k]) => filter(k)).sort((a, b) => b[1] - a[1]).slice(0, n).map(([k, v]) => `${r1(v)} ${k}`); return { busyMs: r1(busy), files: top(files), functions: top(fns) }; }