Skip to content

⚡ [Powerplan] Replace inefficient polling loop with event-driven WinWait#42

Merged
Ven0m0 merged 2 commits intomainfrom
perf/powerplan-event-driven-10285690155865980163
Mar 1, 2026
Merged

⚡ [Powerplan] Replace inefficient polling loop with event-driven WinWait#42
Ven0m0 merged 2 commits intomainfrom
perf/powerplan-event-driven-10285690155865980163

Conversation

@Ven0m0
Copy link
Owner

@Ven0m0 Ven0m0 commented Mar 1, 2026

💡 What: Replaced the infinite Sleep and WinExist polling loop with WinWait and WinWaitClose inside ahk/Powerplan.ahk. Removed the fortniteWasRunning state variable as the execution inherently waits.

🎯 Why: The previous script was polling the system every 5 seconds (5000ms via DllCall), creating unnecessary CPU wake-ups and creating up to 5 seconds of latency when the game opens or closes before the power plan would actually switch. WinWait completely halts execution of the thread at the Windows API level until the target window appears or disappears, reducing CPU impact to nearly zero and switching power plans instantly upon launch or exit.

📊 Measured Improvement:

  • Cannot dynamically benchmark locally because AutoHotkey cannot be executed in the current Linux environment.
  • Theoretically, the complexity changes from O(1) polling every 5 seconds (constant CPU wakeups and context switches) to an event-driven hook via the OS. Latency drops from an average of ~2.5s (max 5s) to <10ms. Lines of code are reduced and state tracking logic is eliminated.

PR created automatically by Jules for task 10285690155865980163 started by @Ven0m0

Optimizes `ahk/Powerplan.ahk` by removing an infinite loop that checked `WinExist()` every 5000ms. Replaced it with native `WinWait` and `WinWaitClose` logic. This reduces idle CPU utilization, entirely eliminates the 5-second polling latency when the target window state changes, and simplifies the codebase by removing state-tracking booleans.

Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
Copilot AI review requested due to automatic review settings March 1, 2026 06:30
@google-labs-jules
Copy link
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@gemini-code-assist
Copy link

Warning

You have reached your daily quota limit. Please wait up to 24 hours and I will start processing your requests again!

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Updates the ahk/Powerplan.ahk AutoHotkey v2 utility to switch Windows power plans based on Fortnite window lifecycle using blocking waits instead of a periodic polling loop, aiming to reduce latency and unnecessary wakeups.

Changes:

  • Replaced WinExist + Sleep(5000) polling with WinWait / WinWaitClose.
  • Removed the fortniteWasRunning state-tracking variable and state-change branching.

Comment on lines +29 to +33
WinWait("ahk_exe FortniteClient-Win64-Shipping.exe")
Run(A_ComSpec . " /c " . cmdOnLaunch, , "Hide")

DllCall("kernel32.dll\Sleep", "UInt", 5000)
WinWaitClose("ahk_exe FortniteClient-Win64-Shipping.exe")
Run(A_ComSpec . " /c " . cmdOnClose, , "Hide")
Copy link

Copilot AI Mar 1, 2026

Choose a reason for hiding this comment

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

Run() is asynchronous, so if Fortnite opens/closes quickly you can end up with overlapping powercfg invocations and a race where the earlier launch command finishes after the close command (leaving the wrong power plan active). Consider using RunWait() (or capturing the PID and waiting for it to exit) for both powercfg calls to guarantee ordering.

Copilot uses AI. Check for mistakes.
@Ven0m0 Ven0m0 merged commit 661d4bb into main Mar 1, 2026
1 of 3 checks passed
@Ven0m0 Ven0m0 deleted the perf/powerplan-event-driven-10285690155865980163 branch March 1, 2026 07:48
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