Skip to content

perf: reduce CPU usage by implementing conditional redraws#157

Merged
sectore merged 1 commit intosectore:mainfrom
fgbm:main
Feb 5, 2026
Merged

perf: reduce CPU usage by implementing conditional redraws#157
sectore merged 1 commit intosectore:mainfrom
fgbm:main

Conversation

@fgbm
Copy link
Copy Markdown
Contributor

@fgbm fgbm commented Feb 4, 2026

Summary

This PR optimizes the application's CPU usage by preventing unnecessary terminal redraws. Previously, the UI was being redrawn on every Render event (~60 FPS) even if the state hadn't changed, leading to high CPU load in idle states.

Key Changes:

  • Conditional Redrawing: Added a needs_redraw flag to the App state. The terminal now only draws when this flag is set.
  • State Change Tracking: The flag is triggered by specific events that affect the UI:
    • Tick events (time updates).
    • User input (key presses).
    • Terminal resize events.
    • Internal app events (e.g., clock completion).
  • LocalTime Optimization: Updated LocalTimeState to filter out Tick events from its update stream, as time updates are already handled globally.

Test plan

  1. Idle Load: Run the app with a paused timer. CPU usage should be near 0% or significantly lower than before.
  2. Active Load: Start a timer. The UI should update every 100ms (on Tick) with minimal CPU impact.
  3. Responsiveness: Navigate through menus and switch modes. The UI should remain snappy and responsive to all inputs.
  4. Resizing: Resize the terminal window and verify the UI adapts correctly.

@sectore
Copy link
Copy Markdown
Owner

sectore commented Feb 5, 2026

@fgbm Thanks! Great find + fix 👍

Just double-tested everything and it looks really good on my machine (~60% less CPU usage).

  • before
before-timr-cpu-usage
  • now (your patch)
patched-timr-cpu-usage

@sectore sectore merged commit e64435d into sectore:main Feb 5, 2026
@sectore
Copy link
Copy Markdown
Owner

sectore commented Feb 7, 2026

And for release build I got 0.7% - 0.0% CPU usage:

timr-07p timr-0p

Impressive results @fgbm, thx again!

I'm going to publish a new release with it today...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants