Skip to content

Stop the hidden panel draining the battery; fix the window crash - #1

Merged
arshdwani merged 1 commit into
mainfrom
fix-battery-drain
Oct 28, 2026
Merged

arshdwani merged 1 commit into
mainfrom
fix-battery-drain

Conversation

@arshdwani

Copy link
Copy Markdown
Owner

Why

Dynamo was using 18–30% of a CPU core around the clock on battery, even with its panel closed (10+ hours of CPU over 17 days of uptime). The cause: the popover's SwiftUI views stayed alive after it closed, and the Energy tab's flow arrows (TimelineView at 30 fps) kept laying out the whole hidden panel. It hits anyone whose last tab was Energy, because the battery arrow is always "active" on battery.

What changed

  • Panel built on open, released on close (App.swift). The popover gets a fresh NSHostingController each time it opens and drops it in popoverDidClose. The fallback window is released on close too, and reopens where it was.
  • Static flow arrows (Views.swift). With .preferredContentSize sizing, every animation frame made the popover re-measure the entire panel (~20% CPU while open). The arrows keep their dashes, colors and direction chevron, but don't move.
  • Less background work while closed (Model.swift):
    • Polls every 20 s instead of 3 s.
    • Picks up plug/unplug and charge changes from IOPSNotificationCreateRunLoopSource, so the menu bar icon still updates immediately.
    • Skips pmset, system_profiler and history, which only the panel shows; they load at launch and refresh when the panel opens.
  • Window crash fix (App.swift). Opening the window (relaunching from Spotlight/Finder, or the notch fallback) crashed 1.5 on macOS 26.6. AppKit aborted after too many Update Constraints passes, because .preferredContentSize sizing in an NSWindow keeps re-invalidating constraints. Windows now use .standardBounds; the popover keeps .preferredContentSize.
  • Daemon timer tolerance (Daemon.swift). The 5 s tick gets 1 s of tolerance so macOS can coalesce its wakeups.

Measurements

Average app CPU on an Apple Silicon MacBook on battery, Energy tab, with the same scripted open/close sequence on each build:

1.5 This PR
Panel never opened 0.1% 0%
Popover open 21.2% 1.1%
Popover closed 17.8% ~0%
Window open crashes 1.4%
Window closed — 0%

Policy tests pass (./build.sh test), and the build has no warnings.

Notes for review

  • Hidden poll interval. 20 s plus 20% tolerance keeps the status file well within DaemonStatus.isFresh's 60 s window, given the daemon's 20 s heartbeat. Hence 20 s rather than 30.
  • Power-source notifications are rare. I counted 2 in 90 s on battery, so they don't add meaningful refreshes.
  • Popover sizing is unchanged. I tried .standardBounds for the popover too. It cut the animated cost, but it offset the popover 20 pt from the status item, and after closing it sometimes still used CPU. So the popover keeps .preferredContentSize, and the arrows are static instead.
  • Screenshots. The snapshot/README images render the arrows exactly as before, since ImageRenderer only ever captured a single frame of the animation.

The popover's SwiftUI views stayed alive after it closed, and the Energy
tab's 30 fps flow-arrow animation kept laying out the whole hidden panel:
about 18% CPU around the clock on battery. The panel is now built on open
and released on close. The arrows are static: animating them made the
popover re-measure the panel every frame (about 20% CPU while open, now 1%).

While the panel is closed the app polls every 20 s instead of 3 s, picks up
battery changes from power source notifications, and skips pmset,
system_profiler and history, which only the panel shows.

Opening the window (relaunching the app, or the notch fallback) crashed on
macOS 26: preferredContentSize sizing in an NSWindow loops AppKit's
constraint pass. Windows now size from the view's bounds.

The daemon's 5 s timer gets 1 s of tolerance so its wakeups can coalesce.
@arshdwani
arshdwani merged commit fa60e1c into main Oct 28, 2026
2 checks passed
@arshdwani
arshdwani deleted the fix-battery-drain branch October 28, 2026 12:05
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.

1 participant