Skip to content

Reduce Juice idle power usage - #18

Open
EClinick wants to merge 5 commits into
masterfrom
task/ec/reduce-juice-idle-power
Open

Reduce Juice idle power usage#18
EClinick wants to merge 5 commits into
masterfrom
task/ec/reduce-juice-idle-power

Conversation

@EClinick

Copy link
Copy Markdown
Owner

What changed

  • Make live-power sampling demand-aware: 5 seconds for menu-bar-only background work and 2 seconds while the popover or Stats window is visible.
  • Stop background samples from invalidating hidden SwiftUI charts, while keeping the AppKit menu-bar watt label current.
  • Cache the helper protocol handshake per connection and cancel settled request timeouts.
  • Tear down closed Stats window hosting work and skip battery observers only on batteryless Mac minis.
  • Add concurrency, cadence, lifecycle, and laptop battery regression coverage.

Why

The installed app could sustain roughly 38-41% CPU because hidden app-scoped SwiftUI views were re-rendering every two seconds. Helper requests also repeated a connection-scoped handshake. In signed isolated runtime sampling, the patched app was usually at 0% CPU with brief 0.5-0.7% activity, about a 99.4% sampled reduction. A short-window coalition comparison also moved directionally from about 1.07 W to 0.0006 W; that watt figure is diagnostic rather than a calibrated benchmark.

Laptop battery behavior is intentionally preserved: laptops still perform the initial battery read, keep the 60-second polling cadence, and react to Low Power Mode notifications. Only Mac mini mode avoids battery-specific background resources that can produce no readings.

Verification

  • DEVELOPER_DIR=/Applications/Xcode.app/Contents/Developer CLANG_MODULE_CACHE_PATH=/tmp/juice-clang-module-cache SWIFTPM_MODULECACHE_OVERRIDE=/tmp/juice-swiftpm-module-cache swift test --no-parallel — 275 tests in 35 suites passed
  • swift test --filter BatteryViewModelTests — 2 focused battery tests passed
  • git diff --check
  • Signed isolated dev-build runtime comparison described above

Copilot AI review requested due to automatic review settings July 29, 2026 20:47
@vercel

vercel Bot commented Jul 29, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

1 Skipped Deployment
Project Deployment Actions Updated (UTC)
website Ignored Ignored Preview Jul 29, 2026 9:09pm

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 58287f489f

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +280 to +282
} catch {
state = .unavailable
return .unavailable

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Retry stale checkState transport failures

When the connection is invalidated or interrupted while this handshake is in flight and its error handler wins, the caught error may belong to a context that is no longer current. This branch nevertheless publishes .unavailable and returns immediately, so the replacement connection is never checked and a helper crash/update race can falsely report that the helper is unavailable. Mirror the stale-success path by checking isCurrent(context) in the catch and continuing when it is false.

Useful? React with 👍 / 👎.

@EClinick

Copy link
Copy Markdown
Owner Author

Benchmark repeat: launch-idle compute usage

Repeated on the M4 Mac mini (Mac16,10) against exact PR head 8549e42 and installed Juice 0.2.9.

Method

  • Restarted production Juice and Juice Dev within the same second.
  • Allowed both processes to settle for 45 seconds.
  • Sampled both variants simultaneously for 180.803 seconds.
  • Confirmed through CGWindowListCopyWindowInfo that neither popover was instantiated or on-screen before or after the run. This result is therefore specifically the true launch-idle workload.
  • App metrics come from cumulative proc_pid_rusage(RUSAGE_INFO_V6) deltas sampled once per second.
  • App and root-owned helper energy come from their separate resource-coalition CPU/GPU/ANE counters. The total below is app coalition plus its matching helper coalition.
Metric Installed 0.2.9 PR head Reduction
Average app CPU 0.732617% 0.055652% 92.4%
Maximum 1-second CPU 2.696750% 1.961529% 27.3%
Instructions/second 28.423M 1.091M 96.2%
CPU cycles/second 17.458M 1.305M 92.5%
Package-idle wakeups/second 0.774 0.100 87.1%
Interrupt wakeups/second 3.667 0.653 82.2%
App coalition compute 0.002317774 W 0.000132155 W 94.3%
Helper coalition compute 0.000869322 W 0.000160737 W 81.5%
Total app + helper compute 0.003187096 W 0.000292892 W 90.8%

Interpretation

This repeat independently confirms that the PR materially reduces background compute. Under true launch-idle conditions, attributable app-plus-helper compute energy was about 10.9x lower. The result is also consistent with the earlier hidden-instantiated-popover run, where total coalition compute fell from 0.03467 W to 0.00945 W (72.7% lower) and app CPU fell from 10.08% to 4.03% (60.0% lower).

The watt values are kernel coalition CPU/GPU/ANE energy attributable to Juice, not whole-system wall-outlet power. They are useful for comparing these two builds under the same workload but should not be presented as total Mac power draw.

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