Skip to content

Conversation

dwdvIl
Copy link

@dwdvIl dwdvIl commented Jul 29, 2025

Running a project rendering a rectangle and a simple gradient shader at 60fps, it was taking up 14-17% of my CPU.
Added a small check so it sleeps only when necessary, at a reasonable stability.
CPU usage is now 2-4%.
Compared to an empty project at the same fps and no libraries, the difference is just 1%.
I did run it on Linux, 1-3% usage vs 5% originally.

An adaptive affinity was added to compensate for high fps. Lower values produce more consistent frametimes at the cost of higher CPU usage.
The affinity formula is simplified from this equation: ( 1 - (tick.framerate - 60) / (240-60) ) * 0.35 + 0.5

Tested in LÖVE 11.5

@bjornbytes
Copy link
Owner

I think .0005 is actually getting converted to a zero ms sleep (.0005 * 1000 gets passed to love::sleep which takes an int, so it gets truncated to 0). This was a mistake.

I'm a little unsure about the logic for the affinity, but changing the sleep to .001 is a welcome change that fixes the main problem.

LÖVE 12 supports high resolution sleep, which should also fix the issue.

@dwdvIl
Copy link
Author

dwdvIl commented Jul 29, 2025

The affinity is a concept i came up with after testing the frametime graph using MSI Afterburner, your code gives a very consistant framerate, however since it gets truncated to 0 it's due to the while loop blocking the program until the condition isn't met.
But even changing this value to be the bare minimum (.001) makes the frametime be inconsistant and most of the time LÖVE won't reach the target framerate!

It's a range between 0.5 and 0.85, and it depends on your target framerate, the while loop still blocks the program, but this time it calls timer.sleep(0.001) whenever the delta is lower than frametime * affinity.

It adjusts how likely timer.sleep will be called per iteration, and it depends on the target framerate because high values limit the maximum framerate. For example 0.85 at 165fps will result in 160fps max, so it's adjusted to 0.6460375 to compensate.
I've tested up to 240fps (my monitor refresh rate), took samples and plotted a linear equation for the formula.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants