Loading page...Загружаем страницу...正在加载页面...
AtlasRepo

I built a leaner alternative to Electron. Then I found it was burning 90% CPU at idle

说明与用途

Natyv 最初在空闲时消耗 70–93% CPU,原因是事件循环未被节流且每帧都无条件重绘。通过将 SDL_PollEvent 替换为 SDL_WaitEventTimeout、添加脏帧检查以及事件驱动唤醒,idle CPU 降至 0%,同时保持了小体积。

问题

A native desktop runtime (Natyv) was consuming 70–93% CPU at idle due to an unthrottled event loop and unconditional redraws, making it less efficient than Electron or Tauri.

结果

After refactoring the event loop and draw logic, Natyv achieved 0% idle CPU, matching Electron and Tauri, while maintaining a small footprint.

工作流

  • Benchmark Natyv, Electron, and Tauri on the same app and machine.
  • Identify high idle CPU caused by busy loop and unconditional redraw.
  • Replace SDL_PollEvent with SDL_WaitEventTimeout(16) and enable VSync to throttle loop to ~60Hz.
  • Add dirty-check logic to skip redraws when nothing changed, accounting for time-driven UI elements.
  • Introduce event-driven wakeups from worker threads and force redraw on new windows.
  • Audit and remove remaining unconditional recompute passes; use blocking wait when idle.
  • Verify 0% idle CPU with ps and confirm memory usage unchanged.

证据

Idle CPU did not. Electron and Tauri both settled to 0% CPU at idle, completely unsurprising, both have mature, heavily optimized event loops. Natyv was sustaining 70 to 93% CPU doing absolutely nothing.

来源

Stage 1 replaced the busy poll with SDL_WaitEventTimeout(&event, 16), waking at most 60 times a second... idle CPU dropped from 70 to 93% down to about 22 to 24%.

来源

Final, measured result: 0.0% idle CPU, confirmed via ps, matching Electron and Tauri exactly.

来源

  • 记录日期: 2026-09-20T16:45:32.899Z