nanocoder icon indicating copy to clipboard operation
nanocoder copied to clipboard

[Feature] Run React/Ink in production mode in the TUI (about 23% less CPU while streaming)

Open addyCooks opened this issue 1 month ago • 1 comments

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 jsx 2.8 s, :345 jsxs 2.0 s, react.development.js:1081 createElement 1.4 s, react-reconciler.development.js beginWork. The react and react-reconciler groups 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.js starts with import ... from 'react/jsx-runtime' because source/cli.tsx contains JSX (line 933). Static imports are hoisted, so setting process.env.NODE_ENV inside cli.tsx is too late.
  • NODE_ENV is only read in source/utils/logging/index.ts:74, logger-provider.ts:66 (=== 'development'), source/config/mcp-config-loader.ts:183,241 (=== 'test') and useAppLogging.tsx:58. An explicit value stays untouched with ??=.

Steps to Reproduce

  1. pnpm run build
  2. node --cpu-prof dist/cli.js, send one prompt, quit.
  3. grep -c "\.development\.js" *.cpuprofile returns dozens of frames.

Proposed Change

  • Move the render(<App .../>) block out of source/cli.tsx into a new module (for example source/app/render-tui.tsx) loaded with await import(). This also keeps react off the --version fast 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.js frames in a CPU profile of a streamed answer.
  • At least 15% less CPU per streamed turn.
  • NODE_ENV inside an execute_bash call is unchanged from the launching shell.
  • nanocoder --version no longer loads react.

Additional Context

  • Effort: S. Start with source/cli.tsx, source/utils/perf-buffer.ts.
  • Risk: do not leak NODE_ENV=production to child processes. npm install and pnpm install skip 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=test via the ava config, so they are unaffected. Specs: source/app/App.spec.tsx, source/cli.spec.ts.

addyCooks avatar Oct 15 '26 15:10 addyCooks

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).

ozenyunus35 avatar Oct 15 '26 21:10 ozenyunus35