winit icon indicating copy to clipboard operation
winit copied to clipboard

High CPU usage in empty application regardless of control flow

Open jangler opened this issue 1 year ago • 4 comments

Hi, I'm getting ~30% CPU usage (as reported by Task Manager) for an empty application in release mode on Windows 10. The code is just the "event loop" example from the crate documentation; it doesn't matter which ControlFlow I use. I'm not moving the mouse or interacting with the window in any way.

flamegraph.zip

jangler avatar Dec 12 '24 14:12 jangler

Same on Debian 12 XFCE

alexdesander avatar Dec 13 '24 00:12 alexdesander

I am seeing 100 % CPU usage on macOS and when running in the browser using winit 0.30.8.

I assume this is the same example mentioned above, but I will paste what I used here for reference:

use winit::application::ApplicationHandler;
use winit::event::WindowEvent;
use winit::event_loop::{ActiveEventLoop, ControlFlow, EventLoop};
use winit::window::{Window, WindowId};

#[derive(Default)]
struct App {
    window: Option<Window>,
}

impl ApplicationHandler for App {
    fn resumed(&mut self, event_loop: &ActiveEventLoop) {
        self.window = Some(event_loop.create_window(Window::default_attributes()).unwrap());
    }

    fn window_event(&mut self, event_loop: &ActiveEventLoop, id: WindowId, event: WindowEvent) {
        match event {
            WindowEvent::CloseRequested => {
                println!("The close button was pressed; stopping");
                event_loop.exit();
            },
            WindowEvent::RedrawRequested => {
                self.window.as_ref().unwrap().request_redraw();
            }
            _ => (),
        }
    }
}

fn main() {
    let event_loop = EventLoop::new().unwrap();
    // both ControlFlow options result in 100 %
    // event_loop.set_control_flow(ControlFlow::Poll);
    event_loop.set_control_flow(ControlFlow::Wait);
    let mut app = App::default();
    event_loop.run_app(&mut app);
}

On macOS It runs at 100 % CPU usage even when commenting out the call to request_redraw() and with either ControlFlow::Poll and ControlFlow::Wait.

Edit: On web, it runs with low CPU usage with ControlFlow::Wait or by switching to PollStrategy::IdleCallback.

I stumbled upon the issue when noticing the high CPU usage in the browser in a new project. I compared it to a different project that I have and does not show such high CPU usage. The other project uses winit 0.26, so I suspect there has been a regression in performance between the two versions.

dragly avatar Feb 08 '25 08:02 dragly

I am seeing 100 % CPU usage on macOS and when running in the browser using winit 0.30.8.

Please open a new issue, that's for a different platform (and none of them share implementation details).

madsmtm avatar Feb 08 '25 14:02 madsmtm

FWIW, I get the same behavior running examples/application.rs when continuous redraw is toggled on (with Shift+R), even though redraws only seem to be requested at 56 Hz (my display runs at 75 Hz).

Edit: I guess in that case the low frame rate is a consequence of slow software pixel-pushing, and in cases where the pixel-pushing is faster or removed it becomes an issue of unlimited frame rate.

jangler avatar Aug 25 '25 17:08 jangler