From 0c5a6d565cc7b7ea70bd3249d349dd825093bcb5 Mon Sep 17 00:00:00 2001 From: kilyabin <65072190+kilyabin@users.noreply.github.com> Date: Thu, 18 Jun 2026 02:33:23 +0400 Subject: [PATCH] perf: fix WaitUntil being bypassed by request_redraw in AboutToWait Calling request_redraw() inside AboutToWait immediately woke the event loop on every iteration, making WaitUntil a no-op and producing a busy-loop (98% CPU). Move request_redraw to NewEvents(ResumeTimeReached) so the thread actually sleeps between frames. --- src/app.rs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/app.rs b/src/app.rs index 3bd56a5..075cff1 100644 --- a/src/app.rs +++ b/src/app.rs @@ -4,7 +4,7 @@ use std::time::{Duration, Instant}; use anyhow::Result; use winit::{ - event::{ElementState, Event, KeyEvent, WindowEvent}, + event::{ElementState, Event, KeyEvent, StartCause, WindowEvent}, event_loop::{ControlFlow, EventLoop}, keyboard::{KeyCode, PhysicalKey}, window::{Fullscreen, WindowBuilder}, @@ -139,9 +139,14 @@ pub fn run(config: Config, monitor_idx: usize) -> Result<()> { } } + // Timer expired or first run — schedule a redraw + Event::NewEvents(StartCause::ResumeTimeReached { .. } | StartCause::Init) => { + window.request_redraw(); + } + + // All events processed — sleep until next frame Event::AboutToWait => { elwt.set_control_flow(ControlFlow::WaitUntil(Instant::now() + frame_time)); - window.request_redraw(); } _ => {}