Switch from Float to Integer Time Values

- Use fast data types where possible so the compiler can optimize for speed based on platform
    - for example, 128 bits might be fastest on ARM
    - good future-proofing

- Refactor GetTimeSinceStart() to be a bit faster
    - multiplication is much faster than division

- Implement a RageTimer method to get the seconds value as a plain int, for the places which cast the seconds value to an int

- Changing from GetTimeSinceStartFast() to GetTimeSinceStart() where accuracy is important

- Changing from GetTimeSinceStart() to GetUsecsSinceStart() for timestamp diffs

- Adjust RageThreads to accomodate an unsigned timestamp value
   - a constant for the maximum value of `uint_fast64_t` replaces `-1` to accommodate the change from signed to unsigned for the `locked_at` variable
   - i have separate constants for `std::numeric_limits<std::uint_fast64_t>::max()` and `static_cast<std::uint_fast64_t>(-1)`, so the reader understands -1 represents an error code, though they evaluate to the same value, so i could remove one of the two

- Add two methods to calculate the MMSSMsMs / MMSSMsMsMs time value from usecs directly instead of inferring it from a seconds value, in RageUtil

- Use a similar counter/modulo based method for WheelNotifyIcon, similar to what i did for text_glow in NoteField in 2eeee03

- Make `g_iStartTime` static const for safety

Rename two timer functions:
GetUsecsSinceStart -> GetTimeSinceStartMicroseconds
GetMicrosecondsSinceStart -> GetSystemTimeAsMicroseconds

Remove std prefix from uint_fast64_t
This commit is contained in:
sukibaby
2024-09-22 01:27:45 -07:00
committed by teejusb
parent 53cd968b90
commit eb35a9b9af
24 changed files with 95 additions and 55 deletions
+2 -2
View File
@@ -87,7 +87,7 @@ public:
* underlying timers may be 32-bit, but implementations should try to avoid
* wrapping if possible.
*/
static std::int64_t GetMicrosecondsSinceStart();
static std::int64_t GetSystemTimeInMicroseconds();
/*
* Add file search paths, higher priority first.
@@ -129,7 +129,7 @@ public:
void RegisterWithLua();
private:
/* This are helpers for GetMicrosecondsSinceStart on systems with a timer
/* This are helpers for GetSystemTimeInMicroseconds on systems with a timer
* that may loop or move backwards. */
static std::int64_t FixupTimeIfLooped( std::int64_t usecs );
static std::int64_t FixupTimeIfBackwards( std::int64_t usecs );
+2 -2
View File
@@ -4,7 +4,7 @@
#include <cstdint>
/*
* This is a helper for GetMicrosecondsSinceStart on systems with a system
* This is a helper for GetSystemTimeInMicroseconds on systems with a system
* timer that may loop or move backwards.
*
* The time may decrease last for at least two reasons:
@@ -23,7 +23,7 @@
*
* This helper only needs to be used if one or both of the above conditions can occur.
* If the underlying timer is reliable, this doesn't need to be used (for a small
* efficiency bonus). Also, you may omit this for GetMicrosecondsSinceStart() when
* efficiency bonus). Also, you may omit this for GetSystemTimeInMicroseconds() when
* bAccurate == false.
*/
+1 -1
View File
@@ -258,7 +258,7 @@ bool ArchHooks_MacOSX::GoToURL( RString sUrl )
return result == 0;
}
std::int64_t ArchHooks::GetMicrosecondsSinceStart()
std::int64_t ArchHooks::GetSystemTimeInMicroseconds()
{
// http://developer.apple.com/qa/qa2004/qa1398.html
static double factor = 0.0;
+3 -3
View File
@@ -120,7 +120,7 @@ static void TestTLS()
#endif
#if 1
/* If librt is available, use CLOCK_MONOTONIC to implement GetMicrosecondsSinceStart,
/* If librt is available, use CLOCK_MONOTONIC to implement GetSystemTimeInMicroseconds,
* if supported, so changes to the system clock don't cause problems. */
namespace
{
@@ -149,7 +149,7 @@ clockid_t ArchHooks_Unix::GetClock()
return g_Clock;
}
std::int64_t ArchHooks::GetMicrosecondsSinceStart()
std::int64_t ArchHooks::GetSystemTimeInMicroseconds()
{
OpenGetTime();
@@ -162,7 +162,7 @@ std::int64_t ArchHooks::GetMicrosecondsSinceStart()
return iRet;
}
#else
std::int64_t ArchHooks::GetMicrosecondsSinceStart()
std::int64_t ArchHooks::GetSystemTimeInMicroseconds()
{
struct timeval tv;
gettimeofday( &tv, nullptr );
+1 -1
View File
@@ -13,7 +13,7 @@ public:
void DumpDebugInfo();
void SetTime( tm newtime );
std::int64_t GetMicrosecondsSinceStart();
std::int64_t GetSystemTimeInMicroseconds();
void MountInitialFilesystems( const RString &sDirOfExecutable );
float GetDisplayAspectRatio() { return 4.0f/3; }
+1 -1
View File
@@ -39,7 +39,7 @@ static void InitTimer()
QueryPerformanceFrequency(&g_liFrequency);
}
std::int64_t ArchHooks::GetMicrosecondsSinceStart()
std::int64_t ArchHooks::GetSystemTimeInMicroseconds()
{
// Make sure the timer is initialized.
if (!g_bTimerInitialized) {
@@ -301,8 +301,9 @@ void RageSoundDriver::Update()
// LOG->Trace("set (#%i) %p from STOPPING to HALTING", i, m_Sounds[i].m_pSound);
}
static float fNext = 0;
if( RageTimer::GetTimeSinceStart() >= fNext )
constexpr uint_fast64_t iUsecs = 1000000;
static uint_fast64_t fNextUsecs = 0;
if (RageTimer::GetTimeSinceStartMicroseconds() >= fNextUsecs)
{
/* Lockless: only Mix() can write to underruns. */
int current_underruns = underruns;
@@ -314,7 +315,7 @@ void RageSoundDriver::Update()
/* Don't log again for at least a second, or we'll burst output
* and possibly cause more underruns. */
fNext = RageTimer::GetTimeSinceStart() + 1;
fNextUsecs = RageTimer::GetTimeSinceStartMicroseconds() + iUsecs;
}
}
@@ -493,7 +494,7 @@ std::int64_t RageSoundDriver::ClampHardwareFrame( std::int64_t iHardwareFrame )
/* Clamp the output to one per second, so one underruns don't cascade due to
* output spam. */
static std::int64_t lastTime = 0;
std::int64_t currentTime = RageTimer::GetUsecsSinceStart();
std::int64_t currentTime = RageTimer::GetTimeSinceStartMicroseconds();
if( lastTime == 0 || (currentTime - lastTime) > 1000000 )
{
LOG->Trace("RageSoundDriver: driver returned a lesser position (%" PRId64 " < %" PRId64 ")", iHardwareFrame, m_iMaxHardwareFrame);
@@ -529,9 +530,9 @@ std::int64_t RageSoundDriver::GetHardwareFrame( RageTimer *pTimestamp=nullptr )
do
{
iStartTime = RageTimer::GetUsecsSinceStart();
iStartTime = RageTimer::GetTimeSinceStartMicroseconds();
iPositionFrames = GetPosition();
std::uint64_t elapsedTime = RageTimer::GetUsecsSinceStart() - iStartTime;
std::uint64_t elapsedTime = RageTimer::GetTimeSinceStartMicroseconds() - iStartTime;
if (elapsedTime <= iThreshold) break;
} while (--iTries);
+1 -1
View File
@@ -25,7 +25,7 @@ void RageSoundDriver_Null::Update()
std::int64_t RageSoundDriver_Null::GetPosition() const
{
return std::int64_t( RageTimer::GetTimeSinceStart() * m_iSampleRate );
return (RageTimer::GetTimeSinceStartMicroseconds() * m_iSampleRate) / 1000000;
}
RageSoundDriver_Null::RageSoundDriver_Null()