glfw: glfwPollEvents is slow on El Capitan, glfw 3.2.1

glfwPollEvents takes more than 3 milliseconds when I hold any keys pressed, otherwise it takes less than 0.1 millisecond.

I am able to reproduce this with glfw 3.2.1 and C using the code:

  /* Loop until the user closes the window */
  while (!glfwWindowShouldClose(window))
  {
    /* Render here */
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the buffers

    /* Swap front and back buffers */
    glfwSwapBuffers(window);

    /* Poll for and process events */
    double seconds = glfwGetTime();
    glfwPollEvents();
    seconds = glfwGetTime() - seconds;
    printf("%.7f\n", seconds);
  }

I get this when I hold any key pressed:

0.0030390
0.0013822
0.0031350
0.0000474
0.0034653
0.0000789
0.0036935
0.0000486
0.0041693
0.0000461
0.0031637
0.0000409
0.0034748

The original ticket was created for the Go wrapper, but it turned out that it’s a problem with glfw, not the wrapper: https://github.com/go-gl/glfw/issues/223

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 17 (5 by maintainers)

Commits related to this issue

Most upvoted comments

I did as you suggested. Program:

#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <stdio.h>
int main(int argc, char** argv)
{
    GLFWwindow* window;
    if ( !glfwInit() )
    {
        return -1;
    }
    glfwWindowHint (GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint (GLFW_CONTEXT_VERSION_MINOR, 3);
#ifdef __APPLE__
    glfwWindowHint (GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif
    glfwWindowHint (GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);

    /* Create a windowed mode window and its OpenGL context */
    window = glfwCreateWindow( 800, 600, "Hello World", NULL, NULL );
    if (!window)
    {
        glfwTerminate();
        return -1;
    }
    glfwMakeContextCurrent(window);
    if (!gladLoadGLLoader((GLADloadproc) glfwGetProcAddress))
    {
        printf("%s\n", "failed to init glad");
        return -1;
    }
    glfwSwapInterval(1);

    double frames[200];
    int i = 0;
    while (!glfwWindowShouldClose(window) && i < 200)
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear the buffers

        glfwSwapBuffers(window);

        double seconds = glfwGetTime();
        glfwPollEvents();
        seconds = glfwGetTime() - seconds;
        frames[i] = seconds;
        i++;
    }
    for (int j = 0; j < i; ++j) {
        printf("%.7f\n", frames[j]);
    }
    printf("%s\n", glfwGetVersionString());
    glfwTerminate();
    return 0;
}
Output
0.1394113
0.0004885
0.0006531
0.0005780
0.0004491
0.0002267
0.0005291
0.0001076
0.0004920
0.0000843
0.0000736
0.0001842
0.0000607
0.0001751
0.0003098
0.0000675
0.0039968
0.0001658
0.0000773
0.0001009
0.0001033
0.0000588
0.0000585
0.0000600
0.0000813
0.0001110
0.0000994
0.0000614
0.0002019
0.0000961
0.0001486
0.0005011
0.0000685
0.0005295
0.0001771
0.0005305
0.0000703
0.0005247
0.0005470
0.0001083
0.0000727
0.0004756
0.0000782
0.0005358
0.0000771
0.0005146
0.0005125
0.0000786
0.0004990
0.0000758
0.0000692
0.0005233
0.0005397
0.0000875
0.0005091
0.0000956
0.0001826
0.0005277
0.0005262
0.0000842
0.0000737
0.0005242
0.0005796
0.0000955
0.0000746
0.0005209
0.0005237
0.0001070
0.0005735
0.0000963
0.0005400
0.0000975
0.0001587
0.0005420
0.0005492
0.0001000
0.0001596
0.0005094
0.0001025
0.0005152
0.0005242
0.0000969
0.0000758
0.0005204
0.0004892
0.0000557
0.0005129
0.0000514
0.0005398
0.0000961
0.0005629
0.0000695
0.0000723
0.0005211
0.0000986
0.0005295
0.0001007
0.0005218
0.0000998
0.0005261
0.0000888
0.0005180
0.0000853
0.0005263
0.0001022
0.0000581
0.0005248
0.0005666
0.0001128
0.0005443
0.0000588
0.0005152
0.0000998
0.0005445
0.0000642
0.0005074
0.0000771
0.0005191
0.0001449
0.0005110
0.0001023
0.0000645
0.0005360
0.0005567
0.0001021
0.0000631
0.0005324
0.0004745
0.0001180
0.0005262
0.0001012
0.0005549
0.0001026
0.0005373
0.0000797
0.0005123
0.0000982
0.0000767
0.0004683
0.0005440
0.0000725
0.0005011
0.0000588
0.0005165
0.0001005
0.0005469
0.0001053
0.0005682
0.0001012
0.0005189
0.0000999
0.0001007
0.0005412
0.0005040
0.0001046
0.0005605
0.0001004
0.0005036
0.0001050
0.0006127
0.0001090
0.0005429
0.0000591
0.0004885
0.0001002
0.0001055
0.0005230
0.0000742
0.0005387
0.0005156
0.0001003
0.0000991
0.0005227
0.0001758
0.0005133
0.0000651
0.0005149
0.0000586
0.0005379
0.0000969
0.0005134
0.0000941
0.0005299
0.0000981
0.0005321
0.0001024
0.0005259
0.0000598
0.0006063
0.0000624
0.0000998
0.0000972
0.0001002
0.0000969
0.0005663
0.0001032
0.0000947
0.0001001
0.0001008
0.0002971
3.3.0 Cocoa NSGL

Thanks for the detailed report.

Aside: printf("%s\n", glfwGetVersionString()); is a little bit more useful than printf("%d %d\n", GLFW_VERSION_MAJOR, GLFW_VERSION_MINOR); because it contains more information.

For reference, running the same test on my Linux PC takes 11ms for the very first call, but subsequent calls to glfwPollEvents reliably take around 0.07ms (around 6x faster than your results).