beyond-all-reason / spring

A powerful free cross-platform RTS game engine
https://beyond-all-reason.github.io/spring/
Other
210 stars 99 forks source link

Spring.GetTimerMicros gets less precise the longer the engine runs #1488

Open MasterBel2 opened 5 months ago

MasterBel2 commented 5 months ago

The precision gradually reduces over time, on the order of 10s of minutes. Relaunching the engine restores the original precision, but quitting to menu and restarting without a full engine restart does not.

When I begin calling, the results look like this: image Some time (30 mins?) later, the results begin to look like this: image

And gradually grow even worse.

MasterBel2 commented 5 months ago

In about 20 minutes from the latter picture, it looks like this: image Values of .000384 stop being returned, its either .000128, .000256 or .000512

marcushutchings commented 5 months ago

This is in Lua code, right? In Lua all numbers are represented as a 32-bit floating point number. It has an accuracy of about 7.22 decimal digits. So as the number gets bigger, the smallest accurate number it can represent also gets larger. This is normal. This is expected. The real question is: at what time does it start to impact the game?

I mean, the numbers you show have an error below 1ms. What level of accuracy do you need? For keyboard, surely even 10ms inaccuracy isn't an issue?

MasterBel2 commented 5 months ago

Precision deterioration happens noticeably within an hour maximum, some napkin math for signed integer suggests it begins after 35 minutes (aasuming it’s an integer count of microseconds) This is annoying but not a dealbreaker for my use case, which is performance profiling. While working on widgets I often have the engine open for hours at a time, running iterations - either doing a series of performance trials, or just keeping an eye on performance through general code changes. I can definitely restart the engine periodically, but it’s a significant break of flow (since each restart can take over a minute).

marcushutchings commented 4 months ago

We could provide a precision timing function, so you'll have two variables: one giving seconds, the other giving the sub-second timing.

MasterBel2 commented 4 months ago

That would be nice. Note Spring.GetTimerMicros already exists as the precision alternative to Spring.GetTimer