Skip to content

Release the Python GIL when rendering #171

@dcnieho

Description

@dcnieho

In my application i have (at least) two threads, one main thread where all the GUI code runs, and a second thread with an event loop for async code (a lot of the code in the app is async). I notice that when i run this code without a GUI, it is way more performant then when running it with the GUI. My hunch (and i do not know how to verify this) is that this is because rendering and then just waiting for the swap/screen flip to occur blocks and doesn't allow the event loop thread to run as much as it could.

I have tried dirty tricks like keeping track in the BeforeImGuiRender callback how much time has elapsed since the last swap and then sleeping a bit if there is still enough time left until the next swap. This seems to speed up my app, expect it has the nasty side effect of occasionally freezing it for a few seconds, so its not workable.

But this got me thinking. I assume that the GIL is being held all the time. But it should be possible to release it from after the BeforeImGuiRender callback until the start of the next frame, or something along those lines, which would free other python code to run while we await the swap (quite similar to the Model no 2 example here since this is essentially a sleep.

I am not sure if we'd be stepping into untold complexity here with all the various backends, but if it would be possible to release the GIL for those backends where it would make sense, if running from Python in the first place, I think that could do wonders for app performance in some cases. As said, i have not profiled (nor do i know how to approach this), so i have no verified my hunch.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions