The best way to control Claude Code, Codex and more from any computer or phone
Mirrored from https://github.com/Camo67/t3code.git
Synced Nov 21, 03:37 UTC
| 1 | /** |
| 2 | * Attribute TypeScript check time and type creation to their source — |
| 3 | * "which types are making the checker slow, and whose fault are they?" |
| 4 | * |
| 5 | * The workspace compiler is tsgo (TS 7), which does NOT support |
| 6 | * `--generateTrace`, so traces are produced with Strada (TS 5.x) used purely |
| 7 | * as a measurement instrument against the same project: |
| 8 | * |
| 9 | * ```sh |
| 10 | * # 1. generate a trace (~2.5 min, ~12GB RAM for packages/alchemy) |
| 11 | * pnpm dlx --package typescript@5.9 tsc \ |
| 12 | * -p packages/alchemy/tsconfig.json \ |
| 13 | * --noEmit --incremental false --composite false \ |
| 14 | * --generateTrace /tmp/tstrace |
| 15 | * |
| 16 | * # 2. attribute it |
| 17 | * node scripts/analyze-type-trace.ts /tmp/tstrace |
| 18 | * ``` |
| 19 | * |
| 20 | * Reports, from `trace.json` + a streaming pass over `types.json`: |
| 21 | * - check self-time per bucket (src/AWS, src/Cloudflare, core, distilled |
| 22 | * lib, effect, node_modules, TS libs) and the top files |
| 23 | * - top-level `structuredTypeRelatedTo` relation pairs by time — this is |
| 24 | * what exposed the 28.4s `Worker<NormalizedBindings<...>>` vs |
| 25 | * `WorkerBindingResource` constraint proof (45% of all check time) |
| 26 | * - locationless union/intersection types attributed to their first |
| 27 | * member's origin, and the top symbols by types created |
| 28 | * |
| 29 | * NOTE: `npx @typescript/analyze-trace` is complementary (per-file hot-spot |
| 30 | * drill-downs from trace.json) but crashes with "Invalid string length" when |
| 31 | * given an Effect-scale types.json — this script streams it instead. |
| 32 | * types.json can exceed 1GB; the stream pass takes ~1 min. |
| 33 | */ |
| 34 | import * as NodeRuntime from "@effect/platform-node/NodeRuntime"; |
| 35 | import * as NodeServices from "@effect/platform-node/NodeServices"; |
| 36 | import { Argument, Command } from "effect/cli"; |
| 37 | import * as Console from "effect/Console"; |
| 38 | import * as Effect from "effect/Effect"; |
| 39 | import * as FileSystem from "effect/FileSystem"; |
| 40 | import * as Path from "effect/Path"; |
| 41 | import * as Stream from "effect/Stream"; |
| 42 | |
| 43 | const BUCKETS = [ |
| 44 | "(unknown)", |
| 45 | "packages/aws/src", |
| 46 | "packages/alchemy/src/Cloudflare", |
| 47 | "packages/alchemy/src (core)", |
| 48 | "submodules/distilled/*/lib", |
| 49 | "node_modules/effect", |
| 50 | "other node_modules", |
| 51 | "typescript libs", |
| 52 | "other", |
| 53 | ] as const; |
| 54 | |
| 55 | // Match by path segment rather than a repo-root prefix so the script works no |
| 56 | // matter which checkout produced the trace. Strada lowercases paths on macOS. |
| 57 | function bucketIdOf(p: string | undefined): number { |
| 58 | if (!p) return 0; |
| 59 | const lower = p.toLowerCase(); |
| 60 | if (lower.includes("node_modules/effect/")) return 5; |
| 61 | if (lower.includes("node_modules/typescript/lib/")) return 7; |
| 62 | if (lower.includes("node_modules/")) return 6; |
| 63 | if (lower.includes("packages/alchemy/src/aws/")) return 1; |
| 64 | if (lower.includes("packages/alchemy/src/cloudflare/")) return 2; |
| 65 | if (lower.includes("packages/alchemy/src/")) return 3; |
| 66 | if (/distilled\/packages\/[^/]+\//.test(lower)) return 4; |
| 67 | return 8; |
| 68 | } |
| 69 | |
| 70 | // Strip the absolute prefix before the first recognizable repo-relative |
| 71 | // segment, purely for display. |
| 72 | const rel = (p: string | undefined) => { |
| 73 | if (!p) return "?"; |
| 74 | const m = p.match(/(?:packages|submodules|distilled|node_modules|examples|scripts)\/.*$/); |
| 75 | return m ? m[0] : p; |
| 76 | }; |
| 77 | |
| 78 | interface TraceEvent { |
| 79 | name?: string; |
| 80 | ph?: string; |
| 81 | ts: number; |
| 82 | dur?: number; |
| 83 | args?: { path?: string; sourceId?: number; targetId?: number }; |
| 84 | } |
| 85 | |
| 86 | interface TypeDescriptor { |
| 87 | id: number; |
| 88 | symbolName?: string; |
| 89 | firstDeclaration?: { path?: string; start?: { line: number } }; |
| 90 | location?: { path?: string; line?: number }; |
| 91 | intersectionTypes?: number[]; |
| 92 | unionTypes?: number[]; |
| 93 | flags?: string[]; |
| 94 | } |
| 95 | |
| 96 | const command = Command.make( |
| 97 | "analyze-type-trace", |
| 98 | { |
| 99 | traceDir: Argument.Directory("traceDir", { mustExist: true }).pipe( |
| 100 | Argument.withDescription("Directory written by `tsc --generateTrace`"), |
| 101 | ), |
| 102 | }, |
| 103 | Effect.fn(function* ({ traceDir }) { |
| 104 | const fs = yield* FileSystem.FileSystem; |
| 105 | const path = yield* Path.Path; |
| 106 | |
| 107 | // ---------- trace.json: check self-time + top-level relation pairs ---------- |
| 108 | const trace: TraceEvent[] = JSON.parse( |
| 109 | yield* fs.readFileString(path.join(traceDir, "trace.json")), |
| 110 | ); |
| 111 | |
| 112 | const stack: { p: string | undefined; start: number; child: number }[] = []; |
| 113 | const fileSelf = new Map<string, number>(); |
| 114 | let totalCheck = 0; |
| 115 | const relEvents: TraceEvent[] = []; |
| 116 | for (const ev of trace) { |
| 117 | if (!ev?.name) continue; |
| 118 | if (ev.name === "checkSourceFile") { |
| 119 | if (ev.ph === "B") { |
| 120 | stack.push({ p: ev.args?.path, start: ev.ts, child: 0 }); |
| 121 | } else if (ev.ph === "E" && stack.length) { |
| 122 | const f = stack.pop()!; |
| 123 | const dur = ev.ts - f.start; |
| 124 | const self = dur - f.child; |
| 125 | if (stack.length) stack[stack.length - 1].child += dur; |
| 126 | fileSelf.set(f.p ?? "?", (fileSelf.get(f.p ?? "?") ?? 0) + self); |
| 127 | totalCheck += self; |
| 128 | } |
| 129 | } else if (ev.name === "structuredTypeRelatedTo" && ev.ph === "X") { |
| 130 | relEvents.push(ev); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Keep only top-level relation intervals (not nested inside a prior one). |
| 135 | relEvents.sort((a, b) => a.ts - b.ts); |
| 136 | let coverEnd = -1; |
| 137 | const pairTime = new Map<string, number>(); |
| 138 | let topLevelRelTotal = 0; |
| 139 | const neededIds = new Set<number>(); |
| 140 | for (const ev of relEvents) { |
| 141 | if (ev.ts >= coverEnd) { |
| 142 | topLevelRelTotal += ev.dur ?? 0; |
| 143 | coverEnd = ev.ts + (ev.dur ?? 0); |
| 144 | const k = `${ev.args!.sourceId}->${ev.args!.targetId}`; |
| 145 | pairTime.set(k, (pairTime.get(k) ?? 0) + (ev.dur ?? 0)); |
| 146 | neededIds.add(ev.args!.sourceId!); |
| 147 | neededIds.add(ev.args!.targetId!); |
| 148 | } |
| 149 | } |
| 150 | const topPairs = [...pairTime.entries()].sort((a, b) => b[1] - a[1]).slice(0, 25); |
| 151 | |
| 152 | const out: string[] = []; |
| 153 | out.push( |
| 154 | `== checkSourceFile self-time: ${(totalCheck / 1e6).toFixed(1)}s over ${fileSelf.size} files ==`, |
| 155 | "", |
| 156 | "== Check self-time by bucket ==", |
| 157 | ); |
| 158 | const bucketTime = new Map<string, number>(); |
| 159 | for (const [p, us] of fileSelf) { |
| 160 | const b = BUCKETS[bucketIdOf(p)]; |
| 161 | bucketTime.set(b, (bucketTime.get(b) ?? 0) + us); |
| 162 | } |
| 163 | for (const [b, us] of [...bucketTime.entries()].sort((a, b) => b[1] - a[1])) { |
| 164 | out.push( |
| 165 | `${(us / 1000).toFixed(0).padStart(9)} ms ${((us / totalCheck) * 100).toFixed(1).padStart(5)}% ${b}`, |
| 166 | ); |
| 167 | } |
| 168 | out.push("", "== Top 30 files by check self-time =="); |
| 169 | for (const [p, us] of [...fileSelf.entries()].sort((a, b) => b[1] - a[1]).slice(0, 30)) { |
| 170 | out.push(`${(us / 1000).toFixed(0).padStart(9)} ms ${rel(p)}`); |
| 171 | } |
| 172 | out.push( |
| 173 | "", |
| 174 | `== Top-level structuredTypeRelatedTo total: ${(topLevelRelTotal / 1e6).toFixed(1)}s ==`, |
| 175 | ); |
| 176 | |
| 177 | // ---------- types.json: streamed attribution ---------- |
| 178 | |
| 179 | const typesPath = path.join(traceDir, "types.json"); |
| 180 | |
| 181 | const MAX = 16_000_000; |
| 182 | const bucketById = new Uint8Array(MAX); |
| 183 | const idInfo = new Map< |
| 184 | number, |
| 185 | { sym?: string; path?: string; line?: number; flags?: string } |
| 186 | >(); |
| 187 | const compositeByBucket = new Map<string, number>(); |
| 188 | const symCount = new Map<string, number>(); |
| 189 | const locatedByBucket = new Map<string, number>(); |
| 190 | let total = 0; |
| 191 | |
| 192 | /** Attributes one types.json line; returns a progress message every 1M types. */ |
| 193 | const ingest = (raw: string): string | undefined => { |
| 194 | let line = raw.trim(); |
| 195 | if (line === "[" || line === "]" || line === "") return; |
| 196 | if (line.startsWith("[")) line = line.slice(1); |
| 197 | if (line.endsWith(",")) line = line.slice(0, -1); |
| 198 | if (line[0] !== "{") return; |
| 199 | let t: TypeDescriptor; |
| 200 | try { |
| 201 | t = JSON.parse(line); |
| 202 | } catch { |
| 203 | return; |
| 204 | } |
| 205 | total++; |
| 206 | const decl = t.firstDeclaration ?? t.location; |
| 207 | const p = decl && ("path" in decl ? decl.path : undefined); |
| 208 | const lineNo = |
| 209 | decl && |
| 210 | ("start" in decl && decl.start ? decl.start.line : (decl as { line?: number }).line); |
| 211 | let b = 0; |
| 212 | if (p) { |
| 213 | b = bucketIdOf(p); |
| 214 | locatedByBucket.set(BUCKETS[b], (locatedByBucket.get(BUCKETS[b]) ?? 0) + 1); |
| 215 | if (t.symbolName) { |
| 216 | const key = `${t.symbolName} @ ${rel(p)}:${lineNo}`; |
| 217 | symCount.set(key, (symCount.get(key) ?? 0) + 1); |
| 218 | } |
| 219 | } else { |
| 220 | const members = t.intersectionTypes ?? t.unionTypes; |
| 221 | if (members) { |
| 222 | // Attribute a locationless composite to its first non-lib member. |
| 223 | for (const m of members) { |
| 224 | if (m > 0 && m < MAX && bucketById[m] !== 0 && bucketById[m] !== 7) { |
| 225 | b = bucketById[m]; |
| 226 | break; |
| 227 | } |
| 228 | } |
| 229 | if (b === 0) { |
| 230 | for (const m of members) { |
| 231 | if (m > 0 && m < MAX && bucketById[m] !== 0) { |
| 232 | b = bucketById[m]; |
| 233 | break; |
| 234 | } |
| 235 | } |
| 236 | } |
| 237 | const bn = BUCKETS[b] + (t.intersectionTypes ? " [intersection]" : " [union]"); |
| 238 | compositeByBucket.set(bn, (compositeByBucket.get(bn) ?? 0) + 1); |
| 239 | } |
| 240 | } |
| 241 | if (t.id < MAX) bucketById[t.id] = b; |
| 242 | if (neededIds.has(t.id)) { |
| 243 | idInfo.set(t.id, { |
| 244 | sym: t.symbolName, |
| 245 | path: p, |
| 246 | line: lineNo, |
| 247 | flags: t.flags?.join("|"), |
| 248 | }); |
| 249 | } |
| 250 | if (total % 1_000_000 === 0) return ` ...streamed ${total} types`; |
| 251 | }; |
| 252 | |
| 253 | yield* fs.stream(typesPath, { chunkSize: 1 << 22 }).pipe( |
| 254 | Stream.decodeText, |
| 255 | Stream.splitLines, |
| 256 | Stream.runForEachArray((lines) => |
| 257 | Effect.forEach( |
| 258 | lines.map(ingest).filter((message) => message !== undefined), |
| 259 | (message) => Console.error(message), |
| 260 | { discard: true }, |
| 261 | ), |
| 262 | ), |
| 263 | ); |
| 264 | |
| 265 | out.push("", `== ${total} types total; located types by bucket ==`); |
| 266 | for (const [b, c] of [...locatedByBucket.entries()].sort((a, b) => b[1] - a[1])) { |
| 267 | out.push(`${String(c).padStart(9)} ${b}`); |
| 268 | } |
| 269 | out.push("", "== Locationless union/intersection types by first member's bucket =="); |
| 270 | for (const [b, c] of [...compositeByBucket.entries()].sort((a, b) => b[1] - a[1])) { |
| 271 | out.push(`${String(c).padStart(9)} ${b}`); |
| 272 | } |
| 273 | out.push("", "== Top 30 symbols by types created =="); |
| 274 | for (const [k, c] of [...symCount.entries()].sort((a, b) => b[1] - a[1]).slice(0, 30)) { |
| 275 | out.push(`${String(c).padStart(9)} ${k}`); |
| 276 | } |
| 277 | out.push("", "== Top 25 top-level relation pairs by time =="); |
| 278 | const fmt = (id: number) => { |
| 279 | const i = idInfo.get(id); |
| 280 | if (!i) return `#${id}`; |
| 281 | const loc = i.path ? `${rel(i.path)}:${i.line}` : i.flags; |
| 282 | return `${i.sym ?? "(anon)"} [${i.flags}] ${loc}`; |
| 283 | }; |
| 284 | for (const [k, us] of topPairs) { |
| 285 | const [s, tg] = k.split("->").map(Number); |
| 286 | out.push(`${(us / 1000).toFixed(0).padStart(8)} ms ${fmt(s)}`, ` -> ${fmt(tg)}`); |
| 287 | } |
| 288 | |
| 289 | const outPath = path.join(traceDir, "attribution.txt"); |
| 290 | yield* fs.writeFileString(outPath, `${out.join("\n")}\n`); |
| 291 | yield* Console.log(out.join("\n")); |
| 292 | yield* Console.error(`\nwritten to ${outPath}`); |
| 293 | }), |
| 294 | ).pipe( |
| 295 | Command.withDescription("Attribute TypeScript check time and type creation to their source"), |
| 296 | ); |
| 297 | |
| 298 | Command.run(command, { version: "0.0.0" }).pipe( |
| 299 | Effect.provide(NodeServices.layer), |
| 300 | NodeRuntime.runMain, |
| 301 | ); |