Skip to content

Commit 8efe97d

Browse files
committed
win: more reliable uv__hrtime precision
Reduces floating-point operations that can cause precision loss. Thanks to @Arnavion for suggesting the fix: nodejs/node#1272 (comment)
1 parent 2eb1c18 commit 8efe97d

1 file changed

Lines changed: 5 additions & 7 deletions

File tree

src/win/util.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@
5858
static char *process_title;
5959
static CRITICAL_SECTION process_title_lock;
6060

61-
/* Interval (in seconds) of the high-resolution clock. */
62-
static double hrtime_interval_ = 0;
61+
/* Interval of the high-resolution clock. */
62+
static uint64_t hrtime_interval_ = 0;
6363

6464

6565
/*
@@ -71,11 +71,9 @@ void uv__util_init() {
7171
/* Initialize process title access mutex. */
7272
InitializeCriticalSection(&process_title_lock);
7373

74-
/* Retrieve high-resolution timer frequency
75-
* and precompute its reciprocal.
76-
*/
74+
/* Retrieve high-resolution timer frequency */
7775
if (QueryPerformanceFrequency(&perf_frequency)) {
78-
hrtime_interval_ = 1.0 / perf_frequency.QuadPart;
76+
hrtime_interval_ = perf_frequency.QuadPart;
7977
} else {
8078
hrtime_interval_= 0;
8179
}
@@ -484,7 +482,7 @@ uint64_t uv__hrtime(double scale) {
484482
* performance counter interval, integer math could cause this computation
485483
* to overflow. Therefore we resort to floating point math.
486484
*/
487-
return (uint64_t) ((double) counter.QuadPart * hrtime_interval_ * scale);
485+
return (uint64_t) ((counter.QuadPart / hrtime_interval_) * scale);
488486
}
489487

490488

0 commit comments

Comments
 (0)