tor-browser

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

commit 5bd4eac454f43bb3bc78b76518ee6b99ada3794f
parent f2288771b8b0b7f24aa8d34b3ceabe25ae3a463f
Author: Jens Stutte <jstutte@mozilla.com>
Date:   Tue, 18 Nov 2025 08:13:07 +0000

Bug 1995934 - Remove BaseTimeDuration::Resolution. r=glandium

There is no consumer left and we can treat the resolution as an internal implementation detail nobody should need to care about.

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

Diffstat:
Mmozglue/misc/TimeStamp.h | 9---------
Mmozglue/misc/TimeStamp_darwin.cpp | 12+-----------
Mmozglue/misc/TimeStamp_posix.cpp | 4----
Mmozglue/misc/TimeStamp_windows.cpp | 17+++++------------
Mxpcom/tests/gtest/TestTimeStamp.cpp | 29++++++++++++++++++++++++-----
5 files changed, 30 insertions(+), 41 deletions(-)

diff --git a/mozglue/misc/TimeStamp.h b/mozglue/misc/TimeStamp.h @@ -36,7 +36,6 @@ class BaseTimeDurationPlatformUtils { static MFBT_API double ToSeconds(int64_t aTicks); static MFBT_API double ToSecondsSigDigits(int64_t aTicks); static MFBT_API int64_t TicksFromMilliseconds(double aMilliseconds); - static MFBT_API int64_t ResolutionInTicks(); }; /** @@ -237,14 +236,6 @@ class BaseTimeDuration { return aStream << aDuration.ToMilliseconds() << " ms"; } - // Return a best guess at the system's current timing resolution, - // which might be variable. BaseTimeDurations below this order of - // magnitude are meaningless, and those at the same order of - // magnitude or just above are suspect. - static BaseTimeDuration Resolution() { - return FromTicks(BaseTimeDurationPlatformUtils::ResolutionInTicks()); - } - // We could define additional operators here: // -- convert to/from other time units // -- scale duration by a float diff --git a/mozglue/misc/TimeStamp_darwin.cpp b/mozglue/misc/TimeStamp_darwin.cpp @@ -26,9 +26,6 @@ #include "mozilla/TimeStamp.h" #include "mozilla/Uptime.h" -// Each tick is significant, so we have a resolution of 1. -static constexpr uint64_t kResolution = 1; - static const uint64_t kUsPerSec = 1000000; static const double kNsPerMsd = 1000000.0; static const double kNsPerSecd = 1000000000.0; @@ -56,9 +53,7 @@ double BaseTimeDurationPlatformUtils::ToSeconds(int64_t aTicks) { double BaseTimeDurationPlatformUtils::ToSecondsSigDigits(int64_t aTicks) { MOZ_ASSERT(gInitialized, "calling TimeDuration too early"); - // As we fix the resolution to 1, all digits are significant and there are - // no extra calculations needed. Ensure we do not change this inadvertedly. - static_assert(kResolution == 1); + // We trust mach_timebase_info that all digits are significant. return ToSeconds(aTicks); } @@ -77,11 +72,6 @@ int64_t BaseTimeDurationPlatformUtils::TicksFromMilliseconds( return result; } -int64_t BaseTimeDurationPlatformUtils::ResolutionInTicks() { - MOZ_ASSERT(gInitialized, "calling TimeDuration too early"); - return static_cast<int64_t>(kResolution); -} - void TimeStamp::Startup() { if (gInitialized) { return; diff --git a/mozglue/misc/TimeStamp_posix.cpp b/mozglue/misc/TimeStamp_posix.cpp @@ -171,10 +171,6 @@ int64_t BaseTimeDurationPlatformUtils::TicksFromMilliseconds( return result; } -int64_t BaseTimeDurationPlatformUtils::ResolutionInTicks() { - return static_cast<int64_t>(sResolution); -} - static bool gInitialized = false; void TimeStamp::Startup() { diff --git a/mozglue/misc/TimeStamp_windows.cpp b/mozglue/misc/TimeStamp_windows.cpp @@ -33,11 +33,6 @@ static double sTicksPerMsd; // ---------------------------------------------------------------------------- static constexpr double kMsPerSecd = 1000.0; -// Note: Resolution used to be sampled based on a loop of QPC calls. -// While it is true that on most systems we cannot expect to subsequently -// sample QPC values as fast as the QPC frequency, we still will get that -// as resolution of the sampled values, that is we have 1 tick resolution. -static constexpr LONGLONG kResolution = 1; namespace mozilla { @@ -50,6 +45,10 @@ static inline ULONGLONG PerformanceCounter() { static void InitConstants() { // Query the frequency from QPC and rely on it for all values. + // Note: The resolution used to be sampled based on a loop of QPC calls. + // While it is true that on most systems we cannot expect to subsequently + // sample QPC values as fast as the QPC frequency, we still will get that + // as resolution of the sampled values, that is we have 1 tick resolution. LARGE_INTEGER freq; bool hasQPC = ::QueryPerformanceFrequency(&freq); MOZ_RELEASE_ASSERT(hasQPC); @@ -67,9 +66,7 @@ MFBT_API double BaseTimeDurationPlatformUtils::ToSeconds(int64_t aTicks) { MFBT_API double BaseTimeDurationPlatformUtils::ToSecondsSigDigits( int64_t aTicks) { - // As we fix the resolution to 1, all digits are significant and there are - // no extra calculations needed. Ensure we do not change this inadvertedly. - static_assert(kResolution == 1); + // We trust QueryPerformanceFrequency that all digits are significant. return ToSeconds(aTicks); } @@ -88,10 +85,6 @@ BaseTimeDurationPlatformUtils::TicksFromMilliseconds(double aMilliseconds) { return (int64_t)result; } -MFBT_API int64_t BaseTimeDurationPlatformUtils::ResolutionInTicks() { - return static_cast<int64_t>(kResolution); -} - // Note that we init early enough during startup such that we are supposed to // not yet have started other threads which could try to use us. static bool gInitialized = false; diff --git a/xpcom/tests/gtest/TestTimeStamp.cpp b/xpcom/tests/gtest/TestTimeStamp.cpp @@ -62,9 +62,28 @@ TEST(TimeStamp, Main) EXPECT_TRUE(td.ToSeconds() < -1.0); EXPECT_TRUE(td.ToSeconds() > -20.0); - double resolution = TimeDuration::Resolution().ToSecondsSigDigits(); - printf(" (platform timer resolution is ~%g s)\n", resolution); - EXPECT_TRUE(1e-10 < resolution); - // Don't upper-bound sanity check ... although NSPR reports 1ms - // resolution, it might be lying, so we shouldn't compare with it + // Now() is trying to ensure the best possible precision on each platform, + // but guarantees at least one millisecond resolution. Given the huge + // difference between the assumed resolution of the clock on modern CPUs + // (< 100ns, often close to 1ns as of 2025) and that guarantee, we can safely + // test for 1ms without fearing intermittents caused by jitter. + TimeStamp start = TimeStamp::Now(); + TimeStamp last = start; + int updated = 0; + int same = 0; + while ((last - start).ToMilliseconds() < 1.0) { + TimeStamp next = TimeStamp::Now(); + if ((next - last).ToMicroseconds() > 0.0) { + // Only count if we saw progress in the ticks. + ++updated; + last = next; + } else { + ++same; + } + } + printf(" Poll saw updated iterations in 1ms: %d\n", updated); + printf(" Poll saw same iterations in 1ms: %d\n", same); + // If we saw 2 updates, we can be pretty sure to be able to guarantee at + // least 1ms resolution. In practice we see much higher numbers here (>>1K). + EXPECT_GE(updated, 2); }