nanocoder
nanocoder copied to clipboard
[Feature] Run React/Ink in production mode in the TUI (about 23% less CPU while streaming)
Description
react and react-reconciler load their development builds whenever NODE_ENV is unset, and nanocoder never sets it. The dev builds validate keys, capture owner stacks and emit performance.mark/measure on every render. Streaming re-renders the UI dozens of times per second, so this cost is paid constantly. It is also why source/utils/perf-buffer.ts exists (#521). On a dual-core machine this CPU competes with a local model running on the same CPU.
Environment
- OS: Windows 11 (10.0.26300), 8 logical cores, 7.9 GB RAM
- Node version: 22.22.3
- Nanocoder version: 1.31.0 (main at 5c2e6935)
Evidence
- CPU profile of 8 streamed turns:
react-jsx-runtime.development.js:327 jsx2.8 s,:345 jsxs2.0 s,react.development.js:1081 createElement1.4 s,react-reconciler.development.js beginWork. Thereactandreact-reconcilergroups were 12.3 s of 45 s non-idle CPU (27%). The host was loaded, so read the share, not the seconds. - A/B, 3 alternating rounds, CPU ms per streamed turn (turns 2-4): dev 2063 / 1547 / 1390, production 1188 / 1422 / 937. Median 1547 to 1188 ms (-23%).
- Compiled
dist/cli.jsstarts withimport ... from 'react/jsx-runtime'becausesource/cli.tsxcontains JSX (line 933). Static imports are hoisted, so settingprocess.env.NODE_ENVinsidecli.tsxis too late. NODE_ENVis only read insource/utils/logging/index.ts:74,logger-provider.ts:66(=== 'development'),source/config/mcp-config-loader.ts:183,241(=== 'test') anduseAppLogging.tsx:58. An explicit value stays untouched with??=.
Steps to Reproduce
pnpm run buildnode --cpu-prof dist/cli.js, send one prompt, quit.grep -c "\.development\.js" *.cpuprofilereturns dozens of frames.
Proposed Change
- Move the
render(<App .../>)block out ofsource/cli.tsxinto a new module (for examplesource/app/render-tui.tsx) loaded withawait import(). This also keepsreactoff the--versionfast path (4 modules, 61 KB today). - Just before that import set
process.env.NODE_ENV ??= 'production', and restore the previous state right after the import resolves. React picks its build at module load.
Expected Behavior
- No
*.development.jsframes in a CPU profile of a streamed answer. - At least 15% less CPU per streamed turn.
NODE_ENVinside anexecute_bashcall is unchanged from the launching shell.nanocoder --versionno longer loadsreact.
Additional Context
- Effort: S. Start with
source/cli.tsx,source/utils/perf-buffer.ts. - Risk: do not leak
NODE_ENV=productionto child processes.npm installandpnpm installskip devDependencies when it is set, so the bash tool would behave differently. Hence set-then-restore. - Historic crash #136 came from a third-party Ink package that is no longer a dependency. Run the TUI specs under it anyway.
- Tests set
NODE_ENV=testvia the ava config, so they are unaffected. Specs:source/app/App.spec.tsx,source/cli.spec.ts.
I'd like to pick this up, following the proposed change (render module loaded with await import(), NODE_ENV set to production only around that import and restored right after).