From 4915c5e6bfedb2c24404fe5af717a3f1672f51bf Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Sun, 10 Sep 2023 17:18:31 +0100 Subject: [PATCH] [*] dont like me stds' - big baz --- Source/Time/AuClock.cpp | 175 ++++++++++++++++++++-------------------- 1 file changed, 89 insertions(+), 86 deletions(-) diff --git a/Source/Time/AuClock.cpp b/Source/Time/AuClock.cpp index 7589fcab..d1e8f32a 100644 --- a/Source/Time/AuClock.cpp +++ b/Source/Time/AuClock.cpp @@ -9,60 +9,49 @@ #include "AuClock.hpp" #include "Time.hpp" - #if defined(AURORA_IS_MODERNNT_DERIVED) // TODO (Reece): .... // benchmarking: // https://github.com/microsoft/STL/issues/2085 -struct steady_clock_fast -{ // wraps QueryPerformanceCounter - using rep = long long; - using period = std::nano; - using duration = std::chrono::nanoseconds; - using time_point = _CHRONO time_point; - static constexpr bool is_steady = true; - static_assert(period::num == 1, "This assumes period::num == 1."); +static AuUInt64 _GetSteadyTimeNS() +{ + static const long long gFreq = _Query_perf_frequency(); + const long long uCounter = _Query_perf_counter(); - _NODISCARD static time_point now() noexcept + if (gFreq == 10000000) { - static const long long _Freq = _Query_perf_frequency(); - const long long _Ctr = _Query_perf_counter(); - - if (_Freq == 10000000) - { - return time_point(duration(_Ctr * 100)); - } - else if (_Freq == 1000000) - { - return time_point(duration(_Ctr * 1000)); - } - else if (_Freq == 100000) - { - return time_point(duration(_Ctr * 10000)); - } - else if (_Freq == 100000000) - { - return time_point(duration(_Ctr * 10)); - } - else if (_Freq == 1000000000) - { - return time_point(duration(_Ctr)); - } - else - { - // 6 branches: the default threshold for most jit and language compiler backends to decide to pick a jump table, if the values were in a close range - // otherwise, back to a tree of paths. either way, im sure 6 if elses are faster than grug math with large numbers, modulus, division, and multiplication - const long long _Whole = (_Ctr / _Freq) * period::den; - const long long _Part = (_Ctr % _Freq) * period::den / _Freq; - return time_point(duration(_Whole + _Part)); - } + return uCounter * 100; } -}; + else if (gFreq == 1000000) + { + return uCounter * 1000; + } + else if (gFreq == 100000) + { + return uCounter * 10000; + } + else if (gFreq == 100000000) + { + return uCounter * 10; + } + else if (gFreq == 1000000000) + { + return uCounter; + } + else + { + // 6 branches: the default threshold for most jit and language compiler backends to decide to pick a jump table, if the values were in a close range + // otherwise, back to a tree of paths. either way, im sure 6 if elses are faster than grug math with large numbers, modulus, division, and multiplication + const long long uWhole = (uCounter / gFreq) * 1'000'000'000ull; + const long long uPart = (uCounter % gFreq) * 1'000'000'000ull / gFreq; + return uWhole + uPart; + } +} // ~3.0741 seconds -using high_res_clock = steady_clock_fast; +//using high_res_clock = steady_clock_fast; // holy fuck, we're keeping this // ~2x improvement @@ -70,7 +59,7 @@ using high_res_clock = steady_clock_fast; #else // ~6.07 seconds -using high_res_clock = std::chrono::high_resolution_clock; +//using high_res_clock = std::chrono::high_resolution_clock; #endif @@ -133,6 +122,16 @@ static time_t CalculateTimeT(AuUInt64 in) return sys_clock::to_time_t(TimeFromDurationSinceEpoch(Duration_t(in))); } +static AuInt64 _CurrentClockMS() +{ + return std::chrono::duration_cast(NormalizeEpoch(sys_clock::now().time_since_epoch())).count(); +} + +static AuInt64 _CurrentClockNS() +{ + return std::chrono::duration_cast(NormalizeEpoch(sys_clock::now().time_since_epoch())).count(); +} + namespace Aurora::Time { // removed from public header / deprecating @@ -163,21 +162,21 @@ namespace Aurora::Time AUKN_SYM AuInt64 CurrentClockMS() { - return std::chrono::duration_cast(NormalizeEpoch(sys_clock::now().time_since_epoch())).count(); + return _CurrentClockMS(); } AUKN_SYM AuInt64 CurrentClockNS() { - return std::chrono::duration_cast(NormalizeEpoch(sys_clock::now().time_since_epoch())).count(); + return _CurrentClockNS(); } AUKN_SYM AuUInt64 SteadyClock() { #if defined(AURORA_IS_MODERNNT_DERIVED) return _Query_perf_counter(); - #endif - + #else return SteadyClockNS() / (1000000000ull / SteadyClockJiffies()); + #endif } AUKN_SYM AuUInt64 SteadyClockMS() @@ -188,13 +187,15 @@ namespace Aurora::Time { return AuSToMS(spec.tv_sec) + AuNSToMS(spec.tv_nsec); } - #endif - - #if defined(AURORA_IS_MODERNNT_DERIVED) - return std::chrono::duration_cast(high_res_clock::now().time_since_epoch()).count(); - #endif - + else + { + return 0; + } + #elif defined(AURORA_IS_MODERNNT_DERIVED) + return AuNSToMS(_GetSteadyTimeNS()); + #else return std::chrono::duration_cast(steady_clock::now().time_since_epoch()).count(); + #endif } AUKN_SYM AuUInt64 SteadyClockNS() @@ -205,13 +206,41 @@ namespace Aurora::Time { return AuMSToNS(AuSToMS(spec.tv_sec)) + (AuUInt64)spec.tv_nsec; } - #endif - - #if defined(AURORA_IS_MODERNNT_DERIVED) - return std::chrono::duration_cast(high_res_clock::now().time_since_epoch()).count(); - #endif - + else + { + return 0; + } + #elif defined(AURORA_IS_MODERNNT_DERIVED) + return _GetSteadyTimeNS(); + #else return std::chrono::duration_cast(steady_clock::now().time_since_epoch()).count(); + #endif + } + + AUKN_SYM AuUInt64 SteadyClockJiffies() + { + static AuUInt64 gFrequency = 0; + if (gFrequency != 0) + { + return gFrequency; + } + + #if defined(AURORA_COMPILER_MSVC) + return gFrequency = _Query_perf_frequency(); + #elif defined(AURORA_IS_POSIX_DERIVED) + ::timespec spec {}; + if (::clock_getres(CLOCK_MONOTONIC, &spec) == 0) + { + if (spec.tv_nsec && !spec.tv_sec) + { + return gFrequency = 1000000000ull / spec.tv_nsec; + } + } + + return gFrequency = (1000000000ull / 100ull); + #else + return gFrequency = static_cast(steady_clock::period::den) / static_cast(steady_clock::period::num); + #endif } AUKN_SYM AuInt64 CTimeToMS(time_t time) @@ -459,32 +488,6 @@ namespace Aurora::Time return std::chrono::duration_cast(std::chrono::nanoseconds(in) - gUnixDelta).count(); } - AUKN_SYM AuUInt64 SteadyClockJiffies() - { - static AuUInt64 frequency = 0; - if (frequency != 0) - { - return frequency; - } - - #if defined(AURORA_COMPILER_MSVC) - return frequency = _Query_perf_frequency(); - #endif - - #if defined(AURORA_IS_POSIX_DERIVED) - ::timespec spec {}; - if (::clock_getres(CLOCK_MONOTONIC, &spec) == 0) - { - if (spec.tv_nsec && !spec.tv_sec) - { - return frequency = 1000000000ull / spec.tv_nsec; - } - } - #endif - - return frequency = static_cast(steady_clock::period::den) / static_cast(steady_clock::period::num); - } - AUKN_SYM tm ToCivilTime(AuInt64 time, bool UTC) { std::tm ret {};