LWJGL Forum

Programming => Lightweight Java Gaming Library => Topic started by: Cornix on October 04, 2013, 19:44:45

Title: Display.sync negative performance impact
Post by: Cornix on October 04, 2013, 19:44:45
Hi there.

I am currently using the Method Display.sync(60) to cap the frame rate at 60 frames per second. Works great by the way.

Now, for some performance testing I was trying to see how many triangles I can render until the frame rate drops below 60. I got about 1 000 000 triangles. At 1.1 million the frame rate goes to about 58.
Good values so far.

However, I wanted to test how far the frame rate goes without capping it and commented the Display.sync call out. To my surprise I had to realise, that I could render 1.1 million triangles at a frame rate of about 90 now! Increasing the triangles step by step I got to 2 000 000 triangles until the frame rate got down to about 60!
Thats quite surprising and I just cant explain it.

So why is that? Does Display.sync have such huge overhead?

Thanks in advance.
Title: Re: Display.sync negative performance impact
Post by: nickbrickmaster on October 07, 2013, 16:45:03
AFAIK Display.sync makes each frame take 1000/n ms to complete. Therefore 60 fps would make each frame take ~16ms. Because of this, if those frames take slightly too long to render, it drops the framerate from the cap.
Title: Re: Display.sync negative performance impact
Post by: spasi on October 07, 2013, 19:25:56
The Display.sync implementation is very simple, you can see the source here (https://github.com/LWJGL/lwjgl/blob/master/src/java/org/lwjgl/opengl/Sync.java#L65). As you can see it does rely on two JDK methods that are really sensitive to the OS thread scheduler and inherently inaccurate: Thread.sleep and Thread.yield. Better accuracy could be achieved with bigger thresholds for sleeping and yielding and the addition of a spin loop at the end. The spin loop will burn CPU though and there's no intrinsic in Java that will use an instruction like PAUSE on x86 to reduce power consumption.