diff --git a/CHANGELOG.md b/CHANGELOG.md index 9b7828c5..437cfd45 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -29,6 +29,9 @@ How to write an entry (from 0.21.0 on; older sections keep their own form): - A swap that loads its page instead (the page answered an error, a module failed to load) still morphs its `data-sn-transition` pair: the load is handed the link that started it ([page transitions](docs/navigation.md#page-transitions)). +- A production build no longer prints `$pp` in place of a string that spells a component's internal props name + (`'$p2'`) inside a template several components write alike: the build renames the props in the code it shares + between them, and now leaves strings alone. ## 0.23.1 (2026-12-02) diff --git a/bench/runtime/page.ts b/bench/runtime/page.ts index 6b2fc69e..e16f8407 100644 --- a/bench/runtime/page.ts +++ b/bench/runtime/page.ts @@ -1,7 +1,7 @@ // Mounts a page for the runtime benchmarks: the real compiler, a production build (no dev // feature, no freezing), the document's own environment. Like `render()` from the testing // module, minus its fake clock and dev checks, which would distort the timings. -import { compileParts } from '../../src/compiler/compile.js'; +import { compileParts } from '../../src/compiler/passes.js'; import { compactIR } from '../../src/ir.js'; import { mount, type App } from '../../src/runtime/app.js'; import { $k } from '../../src/runtime/security.js'; diff --git a/bench/tools/README.md b/bench/tools/README.md index 0a6a553d..fcf876ba 100644 --- a/bench/tools/README.md +++ b/bench/tools/README.md @@ -4,10 +4,11 @@ Plain Node scripts; nothing here is imported by sinter. A base checkout beside t commit, `npm run build` there) is what the A/B and byte comparisons run against. - `instr.mjs` + `hooks.mjs`: a preload. `node --import bench/tools/instr.mjs bench/astro/run-sinter.mjs ` wraps - `Compiler.prototype` methods (`SN_METHODS`, default `decideFor,settle,prerender,worklist,fileConstants,writtenState`; - `#1`/`#2` by pass) and `compile()` through a load hook, and prints each one's calls, sum, p25 and median at exit + the compiler's phases (`SN_METHODS`, default `decideFor,settle,prerender,worklist,fileConstants,writtenState`: a + `Compiler` method, or a stage module's exported function, which the load hook rebinds; `#1`/`#2` by pass) and + `compile()`, and prints each one's calls, sum, p25 and median at exit (`SN_PHASES=`: JSON lines instead). Wall clock (`SN_CLOCK=cpu`: thread CPU, which ticks at 4 ms on some - kernels). `SN_COUNT=1` also rewrites a few lines of `dist/compiler/compile.js` to count calls (row bindings run, proxy + kernels). `SN_COUNT=1` also rewrites a few lines of `dist/compiler/prerender.js` (and `first-paint.js`) to count calls (row bindings run, proxy reads, rows walked, templates rendered, `writeFirstPaint` by kind…); a rewrite whose text is gone throws, so update it with the code. - `ab.mjs [phases…]`: interleaved builds of two apps (each built by the checkout it links), diff --git a/bench/tools/hooks.mjs b/bench/tools/hooks.mjs index fa4e75fb..56211b5e 100644 --- a/bench/tools/hooks.mjs +++ b/bench/tools/hooks.mjs @@ -1,29 +1,36 @@ -// a load hook: appends instrumentation to the compiler's modules; SN_COUNT=1 also counts calls inside the evaluation +// a load hook: appends instrumentation to the compiler's modules (dist/compiler/*.js); SN_COUNT=1 also counts calls inside the evaluation import { readFileSync } from 'node:fs'; const C = process.env.SN_COUNT === '1'; +/** the counters, shared by every module a rewrite counts in */ +const COUNT = 'const __c = (globalThis.__snCount ??= {});\nconst __n = (k, d = 1) => { __c[k] = (__c[k] ?? 0) + d; };\n'; +/** must match instr.mjs */ +const METHODS = (process.env.SN_METHODS ?? 'decideFor,settle,prerender,worklist,fileConstants,writtenState').split(','); const rep = (src, a, b) => { if (!src.includes(a)) throw new Error('hook: not found ' + a); return src.replace(a, b); }; export async function load(url, ctx, next) { const r = await next(url, ctx); - if (url.endsWith('/dist/compiler/compile.js')) { - let src = String(r.source ?? readFileSync(new URL(url))); + const file = url.match(/\/dist\/compiler\/([\w-]+\.js)$/)?.[1]; + if (!file) return r; + const src0 = String(r.source ?? readFileSync(new URL(url))); + let src = src0; + const tail = []; + // the compiler's phases that are module functions now: rebind each one to its timed wrapper (instr.mjs) + for (const m of METHODS) if (src.includes(`export function ${m}(`)) tail.push(`${m} = globalThis.__snWrap ? globalThis.__snWrap(${JSON.stringify(m)}, ${m}) : ${m};`); + if (file === 'prerender.js') { + tail.push('globalThis.__snCollecting = collecting;'); if (C) { - src = 'const __c = (globalThis.__snCount ??= {});\nconst __n = (k, d = 1) => { __c[k] = (__c[k] ?? 0) + d; };\n' + src; + src = COUNT + src; src = rep(src, 'const run = (sc, code) => {', 'const run = (sc, code) => { __n("row run");'); src = rep(src, 'const mine = shared ? used.filter((c) => !shared.has(c)) : used;', 'const mine = shared ? used.filter((c) => !shared.has(c)) : used; __n("used", used.length); __n("mine", mine.length); __n("cands", cands.length); if (process.env.SN_MINE) console.error(mine.slice(0, 30).join("\\n"));'); - if (src.includes('const vals = rowFns ? progOf(nodes, L)?.(sc) : undefined;')) src = rep(src, 'const vals = rowFns ? progOf(nodes, L)?.(sc) : undefined;', 'const vals = rowFns ? progOf(nodes, L)?.(sc) : undefined; if (vals) { __n("row program calls"); __n("row program values", ROW_PARTS.get(nodes)?.list.length ?? 0); }'); - if (src.includes('const mine = this.placedUnit ? unitCands(this.placedUnit, this.tplEls, this.fns) : null;')) src = rep(src, 'const mine = this.placedUnit ? unitCands(this.placedUnit, this.tplEls, this.fns) : null;', 'const mine = this.placedUnit ? unitCands(this.placedUnit, this.tplEls, this.fns) : null; __n("unit cands " + (this.collecting() ? 1 : 2) + (this.placedUnit ? (mine ? " hit" : " miss") : " none")); __n("tplEls" + (this.collecting() ? 1 : 2), this.tplEls.size); __n("unit list", mine?.list.length ?? 0);'); + src = rep(src, 'const mine = cx.placedUnit ? unitCands(cx.placedUnit, cx.tplEls, cx.fns) : null;', 'const mine = cx.placedUnit ? unitCands(cx.placedUnit, cx.tplEls, cx.fns) : null; __n("unit cands " + (collecting(cx) ? 1 : 2) + (cx.placedUnit ? (mine ? " hit" : " miss") : " none")); __n("tplEls" + (collecting(cx) ? 1 : 2), cx.tplEls.size); __n("unit list", mine?.list.length ?? 0);'); src = rep(src, 'const run = (code) => {', 'const run = (code) => { __n("top run");'); src = rep(src, 'get: (_, p) => (p === t.item', 'get: (_, p) => (__n("row proxy get"), p === t.item'); src = rep(src, 'const rowsOf = (t, items, key, sc, out, recs) => {', 'const rowsOf = (t, items, key, sc, out, recs) => { __n("rows walked", items.length);'); src = rep(src, 'const render = (tid, rec) => {', 'const render = (tid, rec) => { __n("templates rendered");'); - src = rep(src, 'function writeFirstPaint(el, kind, name, v, fresh = false) {', 'function writeFirstPaint(el, kind, name, v, fresh = false) { __n("writeFirstPaint " + kind);'); } - return { ...r, source: src + '\n;globalThis.__snInstr?.(Compiler);\n' }; } - if (url.endsWith('/dist/compiler/index.js')) { - const src = String(r.source ?? readFileSync(new URL(url))); - return { ...r, source: src + '\n;compile = globalThis.__snWrap ? globalThis.__snWrap("compile()", compile) : compile;\n' }; - } - return r; + if (file === 'compile.js') tail.push('globalThis.__snInstr?.(Compiler);'); + if (C && file === 'first-paint.js') src = COUNT + rep(src, 'function writeFirstPaint(el, kind, name, v, fresh = false) {', 'function writeFirstPaint(el, kind, name, v, fresh = false) { __n("writeFirstPaint " + kind);'); + if (file === 'index.js') tail.push('compile = globalThis.__snWrap ? globalThis.__snWrap("compile()", compile) : compile;'); + return tail.length || src !== src0 ? { ...r, source: src + '\n;' + tail.join('\n') + '\n' } : r; } diff --git a/bench/tools/instr.mjs b/bench/tools/instr.mjs index 8feca9b3..203c2c41 100644 --- a/bench/tools/instr.mjs +++ b/bench/tools/instr.mjs @@ -7,7 +7,9 @@ const cpu = process.env.SN_CLOCK === "cpu" ? () => { const u = process.threadCpu let depth = new Map(); function wrap(name0, f) { return function (...a) { - const name = name0 === 'compile()' || name0 === 'decideFor' || typeof this?.collecting !== 'function' ? name0 : name0 + (this.collecting() ? '#1' : '#2'); + // a method's compiler is `this`; a stage function's is its first argument + const cx = this?.constructor?.name === 'Compiler' ? this : a[0]?.constructor?.name === 'Compiler' ? a[0] : null; + const name = name0 === 'compile()' || name0 === 'decideFor' || !cx || !globalThis.__snCollecting ? name0 : name0 + (globalThis.__snCollecting(cx) ? '#1' : '#2'); const d = depth.get(name0) ?? 0; if (d) return f.apply(this, a); depth.set(name0, 1); @@ -19,6 +21,7 @@ function wrap(name0, f) { }; } globalThis.__snWrap = wrap; +/** must match hooks.mjs; a Compiler method is wrapped here, a stage module's function by the load hook */ const METHODS = (process.env.SN_METHODS ?? 'decideFor,settle,prerender,worklist,fileConstants,writtenState').split(','); globalThis.__snInstr = (C) => { for (const m of METHODS) if (C.prototype[m]) C.prototype[m] = wrap(m, C.prototype[m]); diff --git a/docs/spec.md b/docs/spec.md index 0dfa1e5f..fd68df92 100644 --- a/docs/spec.md +++ b/docs/spec.md @@ -181,7 +181,7 @@ The page renders correctly before its module has run; the module then fills in d - a `data-sn-if` of that kind renders its branch (or its `data-sn-else`, the `z` template), and the early bindings in it; nested `data-sn-if`s the same way. The branch is cloned from its head template before the anchor's end comment; its nodes, the end comment and the branch number are left on the start comment (`Symbol.for('sinter.early')`); the runtime adopts them on its first toggle (binds the existing nodes instead of cloning), or removes them if another branch is to show. An anchor is rendered once its end comment is parsed (the observer sees comments as the parser inserts them: one more `nodeType` check per added node), and the first scan covers anchors already parsed in the head. - the code for each template the early file renders is written once into a table (`B[