|
| 1 | +/* |
| 2 | + * stack-churn |
| 3 | + * |
| 4 | + * Measures the allocation pressure of `doResolve`'s recursion-tracking |
| 5 | + * stack across many moderately-deep alias rewrites. |
| 6 | + * |
| 7 | + * Every level of alias rewriting re-enters the resolver pipeline via |
| 8 | + * `doResolve`, which has to extend the stack used to detect cycles. |
| 9 | + * On the `Set<string>`-clone baseline this means `new Set(parent)` per |
| 10 | + * level — a fresh hash-table allocation and an O(n) copy on every hook |
| 11 | + * re-entry. Under a long-running async workload the resulting GC churn |
| 12 | + * is what actually shows up in the numbers (look at p99 / rme when |
| 13 | + * running this case: the Set-clone baseline has markedly higher |
| 14 | + * variance from GC pauses). |
| 15 | + * |
| 16 | + * Unlike `pathological-deep-stack` (one very long chain), this case |
| 17 | + * fans out across several independent chains so the stacks don't all |
| 18 | + * share a common long prefix — closer in shape to large real-world |
| 19 | + * alias configs where the resolver sees many unrelated but non-trivial |
| 20 | + * stacks. |
| 21 | + */ |
| 22 | + |
| 23 | +import fs from "fs"; |
| 24 | +import enhanced from "../../../lib/index.js"; |
| 25 | + |
| 26 | +const { ResolverFactory, CachedInputFileSystem } = enhanced; |
| 27 | + |
| 28 | +const CHAIN_COUNT = 4; |
| 29 | +const CHAIN_DEPTH = 60; |
| 30 | +const RESOLVES_PER_ITER = 20; |
| 31 | + |
| 32 | +/** |
| 33 | + * Build CHAIN_COUNT independent alias chains, each of length CHAIN_DEPTH: |
| 34 | + * c0-0 -> c0-1 -> ... -> c0-(n-1) -> ./target |
| 35 | + * c1-0 -> c1-1 -> ... -> c1-(n-1) -> ./target |
| 36 | + * ... |
| 37 | + * so every top-level resolve forces CHAIN_DEPTH `doResolve` re-entries. |
| 38 | + * @returns {Array<{name: string, alias: string}>} alias list |
| 39 | + */ |
| 40 | +function buildChains() { |
| 41 | + const aliases = []; |
| 42 | + for (let c = 0; c < CHAIN_COUNT; c++) { |
| 43 | + for (let i = 0; i < CHAIN_DEPTH - 1; i++) { |
| 44 | + aliases.push({ name: `c${c}-${i}`, alias: `c${c}-${i + 1}` }); |
| 45 | + } |
| 46 | + aliases.push({ name: `c${c}-${CHAIN_DEPTH - 1}`, alias: "./target" }); |
| 47 | + } |
| 48 | + return aliases; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * @param {import('tinybench').Bench} bench |
| 53 | + * @param {{ fixtureDir: string }} ctx |
| 54 | + */ |
| 55 | +export default function register(bench, { fixtureDir }) { |
| 56 | + const fileSystem = new CachedInputFileSystem(fs, 4000); |
| 57 | + const aliases = buildChains(); |
| 58 | + |
| 59 | + const resolver = ResolverFactory.createResolver({ |
| 60 | + fileSystem, |
| 61 | + extensions: [".js"], |
| 62 | + alias: aliases, |
| 63 | + }); |
| 64 | + |
| 65 | + // Fixed request list (no randomness — CodSpeed requires deterministic work). |
| 66 | + const requests = []; |
| 67 | + for (let i = 0; i < RESOLVES_PER_ITER; i++) { |
| 68 | + requests.push(`c${i % CHAIN_COUNT}-0`); |
| 69 | + } |
| 70 | + |
| 71 | + const resolve = (req) => |
| 72 | + new Promise((resolve, reject) => { |
| 73 | + resolver.resolve({}, fixtureDir, req, {}, (err, result) => { |
| 74 | + if (err) return reject(err); |
| 75 | + if (!result) return reject(new Error(`no result for ${req}`)); |
| 76 | + resolve(result); |
| 77 | + }); |
| 78 | + }); |
| 79 | + |
| 80 | + bench.add( |
| 81 | + `stack-churn: ${CHAIN_COUNT}x${CHAIN_DEPTH} alias chains, ${RESOLVES_PER_ITER} resolves`, |
| 82 | + async () => { |
| 83 | + for (const req of requests) { |
| 84 | + await resolve(req); |
| 85 | + } |
| 86 | + }, |
| 87 | + ); |
| 88 | +} |
0 commit comments