Skip to content

Commit 35440e4

Browse files
durandomsirmalloc
andauthored
perf: keep the TUI off the status line render path (sirmalloc#575)
Claude Code re-runs this binary every couple of seconds for as long as a session is open, so module load time is paid continuously rather than once. src/ccstatusline.ts imports runTUI statically, which pulls ink, React and yoga-layout into the render path even though rendering never touches them. Making that import dynamic is not enough on its own: --outfile cannot split, so the whole graph still gets parsed. Building with --splitting moves the TUI into its own chunk and shrinks the entry from 3.34 MB to 21 KB. Measured on an M-series Mac, 20 runs per data point, real payload piped in: before 238, 237, 238 ms/render after 226, 222, 220 ms/render That is roughly 7%, and the rendered output is byte-identical. The interactive TUI was smoke-tested under a pty and behaves the same. scripts/replace-version.ts had to change too: with splitting the __PACKAGE_VERSION__ placeholder lands in a chunk rather than in dist/ccstatusline.js, so patching only the entry would have silently shipped an unreplaced placeholder. It now patches every emitted script and fails loudly when it finds none. Packaging is unaffected: files is already dist/, and bin/main/exports keep pointing at dist/ccstatusline.js. Verified the built output runs under node as well as bun. Assisted-by: claude:claude-opus-5 Co-authored-by: Matthew Breedlove <[email protected]>
1 parent 05554cd commit 35440e4

3 files changed

Lines changed: 27 additions & 11 deletions

File tree

‎package.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
],
2424
"scripts": {
2525
"start": "bun run src/ccstatusline.ts",
26-
"build": "rm -rf dist/* ; bun build src/ccstatusline.ts --target=node --outfile=dist/ccstatusline.js --target-version=14",
26+
"build": "rm -rf dist/* ; bun build src/ccstatusline.ts --target=node --splitting --format=esm --outdir=dist --target-version=14",
2727
"postbuild": "bun run scripts/replace-version.ts",
2828
"example": "cat scripts/payload.example.json | bun start",
2929
"video:studio": "remotion studio remotion/index.ts",

‎scripts/replace-version.ts‎

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import {
44
readFileSync,
5+
readdirSync,
56
writeFileSync
67
} from 'fs';
78
import { join } from 'path';
@@ -15,14 +16,25 @@ interface PackageJson {
1516
const packageJson = JSON.parse(readFileSync('package.json', 'utf-8')) as PackageJson;
1617
const version = packageJson.version;
1718

18-
// Read the bundled file
19-
const bundledFilePath = join('dist', 'ccstatusline.js');
20-
let bundledContent = readFileSync(bundledFilePath, 'utf-8');
21-
22-
// Replace the placeholder with the actual version
23-
bundledContent = bundledContent.replace(/__PACKAGE_VERSION__/g, version);
19+
// The build emits the entry plus code-split chunks, and the placeholder can
20+
// land in any of them, so patch every emitted script rather than just the entry.
21+
let patched = 0;
22+
for (const file of readdirSync('dist')) {
23+
if (!file.endsWith('.js')) {
24+
continue;
25+
}
26+
const path = join('dist', file);
27+
const content = readFileSync(path, 'utf-8');
28+
if (!content.includes('__PACKAGE_VERSION__')) {
29+
continue;
30+
}
31+
writeFileSync(path, content.replace(/__PACKAGE_VERSION__/g, version));
32+
patched++;
33+
}
2434

25-
// Write back the modified content
26-
writeFileSync(bundledFilePath, bundledContent);
35+
if (patched === 0) {
36+
console.error('✗ No __PACKAGE_VERSION__ placeholder found in dist/');
37+
process.exit(1);
38+
}
2739

28-
console.log(`✓ Replaced version placeholder with ${version}`);
40+
console.log(`✓ Replaced version placeholder with ${version} in ${patched} file(s)`);

‎src/ccstatusline.ts‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/usr/bin/env node
22
import chalk from 'chalk';
33

4-
import { runTUI } from './tui';
54
import type { SkillsMetrics } from './types';
65
import type { RenderContext } from './types/RenderContext';
76
import type { StatusJSON } from './types/StatusJSON';
@@ -353,6 +352,11 @@ async function main() {
353352
const { updatemessage, ...newSettings } = settings;
354353
await saveSettings(newSettings);
355354
}
355+
// Imported lazily: the TUI pulls in ink/React/yoga-layout, which the
356+
// status line render path never touches. Claude Code re-runs this
357+
// binary every couple of seconds, so keeping that graph off the
358+
// render path is worth the dynamic import here.
359+
const { runTUI } = await import('./tui');
356360
runTUI();
357361
}
358362
}

0 commit comments

Comments
 (0)