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.
This commit is contained in:
+7
-2
@@ -4,7 +4,7 @@ use std::time::{Duration, Instant};
|
|||||||
|
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
use winit::{
|
use winit::{
|
||||||
event::{ElementState, Event, KeyEvent, WindowEvent},
|
event::{ElementState, Event, KeyEvent, StartCause, WindowEvent},
|
||||||
event_loop::{ControlFlow, EventLoop},
|
event_loop::{ControlFlow, EventLoop},
|
||||||
keyboard::{KeyCode, PhysicalKey},
|
keyboard::{KeyCode, PhysicalKey},
|
||||||
window::{Fullscreen, WindowBuilder},
|
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 => {
|
Event::AboutToWait => {
|
||||||
elwt.set_control_flow(ControlFlow::WaitUntil(Instant::now() + frame_time));
|
elwt.set_control_flow(ControlFlow::WaitUntil(Instant::now() + frame_time));
|
||||||
window.request_redraw();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_ => {}
|
_ => {}
|
||||||
|
|||||||
Reference in New Issue
Block a user