emilk / emilk/egui

`eframe` on Windows high CPU usage due to excessive calls to `glutin::context::make_not_current`

Open
#4,173 6 comments 1 reaction 0 assignees View on GitHub
native-windows performance
Dominant language
Rust
Stars
30.6k
Forks
2.1k
Avg merge
1d 9h
Merged PRs (30d)
72

Description

I noticed that my application CPU usage was much higher than expected on Windows. On a similar machine I was seeing 1%-2% usage on Linux, and around 20%-30% on Window while moving the mouse in window (just mouse movement).

After some optimization I got my rendering loop down to less than 1ms and still the CPU usage was too high. I then sampled the CPU with https://github.com/nico-abram/blondie/tree/main and this is the flamegraph that it produces:

As you can see, almost 90% of the time, it is calling the `make_not_current` function, which seems really excessive...

EDIT: I found in the docs [here](https://learn.microsoft.com/en-us/windows/win32/api/wingdi/nf-wingdi-wglmakecurrent) that this call flushes the rendering context so probably rendering is just very slow... Still not sure why though.

**Screenshots**

![image](https://github.com/emilk/egui/assets/1304177/0a5d9b27-90e5-437f-adcb-02f9b27364c0)

Another flamegraph with Windows Performance Analyzer (similar results):

![image](https://github.com/emilk/egui/assets/1304177/362c8a29-b484-4a84-8759-25959d72ae4e)

This shows the call stack a bit deeper:
* opengl32d.dll `wglMakeCurrent`
* opengl32d.dll `bMakeNoCurrent`
* opengl32d.dll `wglMakeCurrent`
* opengl32d.dll `wglMakeCurrent`
* opengl32d.dll `wglMakeCurrent`
* KernelBase `SetEvent`

**Desktop (please complete the following information):**
- OS: Windows
- Version: egui 0.26

Contributor guide

Open the contributing guide

Research direction

Start by tracing eframe's Windows rendering path around glutin::context::make_not_current and reproduce the mouse-movement workload described in the issue. Use the provided flamegraph or Windows Performance Analyzer call stack to identify why wglMakeCurrent is called so often. Done means the excessive calls and associated CPU usage are reduced without regressing rendering.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
computer-graphics, desktop, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.