Search code examples
javaopengllwjglglfw

glfw pollevents() really, really slow


I've been doing some benchmarking on my game-engine and found a foul culprit:

glfwPollevents();

I've timed this thoroughly and 95% of the time it behaves as expected, but sometimes it eats 5-200% of my update interval (which is 1/60 second). I have literally no idea what's causing this. During these immense polls, no of my defined callbacks are called. Everything is quiet and still. It's completely unpredictable.

I've got a few theories: 1. the scheduler interrupts somewhere within the function. This would be very unlikely, and I would see the same behaviour in other parts of my loops, which I don't.

  1. There's some evil callback that I don't know about somewhere that's beeing called and eats time.

  2. lwjgl problems. LWJGL is a wrapper from glfw, so that it can be used in Java, which I do.

  3. drivers, but which?


Solution

  • glfwPollEvents(); is a wrapper for the OS-specific system call that enables you to check for any events applied to the window. There are a lot of things that count as events, including user-level stuff like selecting or resizing the window, but also including System-level stuff that GLFW doesn't give you access to. There's any number of things that could cause brief interrupts/hangs like you're describing.

    Generally speaking, the best advice is to handle rendering on a different thread, and use a message queue to instruct the main thread to perform "main thread critical tasks" (like opening a new window or other similarly important tasks).