tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

commit 47362f8052957f8bc162ff033990a4f633c39535
parent c7c3aa50f25f969ecd9f7fe1bd66d8cea462ec78
Author: Jens Stutte <jstutte@mozilla.com>
Date:   Tue, 18 Nov 2025 08:13:08 +0000

Bug 1999986 - Remove ToSecondsSigDigits from TimeDuration. r=glandium

ToSecondsSigDigits was always intransparent about the actual precision wrt ToSeconds and not really helpful in deciding, what to show to a human reader.
Now that both on Windows and MacOS the precision is always the very same as that of ToSeconds and after switching the last consumers to use just that, we can just remove it alltogether.

Differential Revision: https://phabricator.services.mozilla.com/D272842

Diffstat:
Mmozglue/misc/TimeStamp.h | 19++++++-------------
Mmozglue/misc/TimeStamp_darwin.cpp | 6------
Mmozglue/misc/TimeStamp_posix.cpp | 64----------------------------------------------------------------
Mmozglue/misc/TimeStamp_windows.cpp | 6------
4 files changed, 6 insertions(+), 89 deletions(-)

diff --git a/mozglue/misc/TimeStamp.h b/mozglue/misc/TimeStamp.h @@ -34,7 +34,6 @@ class TimeStampTests; class BaseTimeDurationPlatformUtils { public: static MFBT_API double ToSeconds(int64_t aTicks); - static MFBT_API double ToSecondsSigDigits(int64_t aTicks); static MFBT_API int64_t TicksFromMilliseconds(double aMilliseconds); }; @@ -71,6 +70,8 @@ class BaseTimeDuration { return *this; } + // ToSeconds returns the (fractional) number of seconds of the duration + // with the maximum representable precision. double ToSeconds() const { if (mValue == INT64_MAX) { return PositiveInfinity<double>(); @@ -80,19 +81,11 @@ class BaseTimeDuration { } return BaseTimeDurationPlatformUtils::ToSeconds(mValue); } - // Return a duration value that includes digits of time we think to - // be significant. This method should be used when displaying a - // time to humans. - double ToSecondsSigDigits() const { - if (mValue == INT64_MAX) { - return PositiveInfinity<double>(); - } - if (mValue == INT64_MIN) { - return NegativeInfinity<double>(); - } - return BaseTimeDurationPlatformUtils::ToSecondsSigDigits(mValue); - } + // ToMilliseconds returns the (fractional) number of milliseconds of the + // duration with the maximum representable precision. double ToMilliseconds() const { return ToSeconds() * 1000.0; } + // ToMicroseconds returns the (fractional) number of microseconds of the + // duration with the maximum representable precision. double ToMicroseconds() const { return ToMilliseconds() * 1000.0; } // Using a double here is safe enough; with 53 bits we can represent diff --git a/mozglue/misc/TimeStamp_darwin.cpp b/mozglue/misc/TimeStamp_darwin.cpp @@ -51,12 +51,6 @@ double BaseTimeDurationPlatformUtils::ToSeconds(int64_t aTicks) { return (aTicks * sNsPerTickd) / kNsPerSecd; } -double BaseTimeDurationPlatformUtils::ToSecondsSigDigits(int64_t aTicks) { - MOZ_ASSERT(gInitialized, "calling TimeDuration too early"); - // We trust mach_timebase_info that all digits are significant. - return ToSeconds(aTicks); -} - int64_t BaseTimeDurationPlatformUtils::TicksFromMilliseconds( double aMilliseconds) { MOZ_ASSERT(gInitialized, "calling TimeDuration too early"); diff --git a/mozglue/misc/TimeStamp_posix.cpp b/mozglue/misc/TimeStamp_posix.cpp @@ -54,10 +54,6 @@ # include <pthread.h> #endif -// Estimate of the smallest duration of time we can measure. -static uint64_t sResolution; -static uint64_t sResolutionSigDigs; - #ifdef CLOCK_MONOTONIC_COARSE static bool sSupportsMonotonicCoarseClock = false; #endif @@ -66,7 +62,6 @@ static bool sSupportsMonotonicCoarseClock = false; static const uint16_t kNsPerUs = 1000; #endif -static const uint64_t kNsPerMs = 1000000; static const uint64_t kNsPerSec = 1000000000; static const double kNsPerMsd = 1000000.0; static const double kNsPerSecd = 1000000000.0; @@ -99,63 +94,12 @@ static uint64_t ClockTimeNs(const clockid_t aClockId = CLOCK_MONOTONIC) { return TimespecToNs(ts); } -static uint64_t ClockResolutionNs() { - // NB: why not rely on clock_getres()? Two reasons: (i) it might - // lie, and (ii) it might return an "ideal" resolution that while - // theoretically true, could never be measured in practice. Since - // clock_gettime() likely involves a system call on your platform, - // the "actual" timing resolution shouldn't be lower than syscall - // overhead. - - uint64_t start = ClockTimeNs(); - uint64_t end = ClockTimeNs(); - uint64_t minres = (end - start); - - // 10 total trials is arbitrary: what we're trying to avoid by - // looping is getting unlucky and being interrupted by a context - // switch or signal, or being bitten by paging/cache effects - for (int i = 0; i < 9; ++i) { - start = ClockTimeNs(); - end = ClockTimeNs(); - - uint64_t candidate = (start - end); - if (candidate < minres) { - minres = candidate; - } - } - - if (0 == minres) { - // measurable resolution is either incredibly low, ~1ns, or very - // high. fall back on clock_getres() - struct timespec ts; - if (0 == clock_getres(CLOCK_MONOTONIC, &ts)) { - minres = TimespecToNs(ts); - } - } - - if (0 == minres) { - // clock_getres probably failed. fall back on NSPR's resolution - // assumption - minres = 1 * kNsPerMs; - } - - return minres; -} - namespace mozilla { double BaseTimeDurationPlatformUtils::ToSeconds(int64_t aTicks) { return double(aTicks) / kNsPerSecd; } -double BaseTimeDurationPlatformUtils::ToSecondsSigDigits(int64_t aTicks) { - // don't report a value < mResolution ... - int64_t valueSigDigs = sResolution * (aTicks / sResolution); - // and chop off insignificant digits - valueSigDigs = sResolutionSigDigs * (valueSigDigs / sResolutionSigDigs); - return double(valueSigDigs) / kNsPerSecd; -} - int64_t BaseTimeDurationPlatformUtils::TicksFromMilliseconds( double aMilliseconds) { double result = aMilliseconds * kNsPerMsd; @@ -189,14 +133,6 @@ void TimeStamp::Startup() { } #endif - sResolution = ClockResolutionNs(); - - // find the number of significant digits in sResolution, for the - // sake of ToSecondsSigDigits() - for (sResolutionSigDigs = 1; !(sResolutionSigDigs == sResolution || - 10 * sResolutionSigDigs > sResolution); - sResolutionSigDigs *= 10); - gInitialized = true; } diff --git a/mozglue/misc/TimeStamp_windows.cpp b/mozglue/misc/TimeStamp_windows.cpp @@ -64,12 +64,6 @@ MFBT_API double BaseTimeDurationPlatformUtils::ToSeconds(int64_t aTicks) { return double(aTicks) / sTicksPerSecd; } -MFBT_API double BaseTimeDurationPlatformUtils::ToSecondsSigDigits( - int64_t aTicks) { - // We trust QueryPerformanceFrequency that all digits are significant. - return ToSeconds(aTicks); -} - MFBT_API int64_t BaseTimeDurationPlatformUtils::TicksFromMilliseconds(double aMilliseconds) { double result = sTicksPerMsd * aMilliseconds;