From 211bf4ec37a73dea06d15233ffa747e9387b8947 Mon Sep 17 00:00:00 2001 From: Fedor Indutny Date: Thu, 19 Jun 2014 11:27:24 -0400 Subject: [PATCH] darwin: invoke `mach_timebase_info` only once According to @aktau, the call to `mach_timebase_info` costs 90% of the total execution time of `uv_hrtime()`. The result of the call is static on all existing platforms, so there is no need in invoking it multiple times. Signed-off-by: Fedor Indutny --- src/unix/darwin.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/unix/darwin.c b/src/unix/darwin.c index 77e662f4..f37d58df 100644 --- a/src/unix/darwin.c +++ b/src/unix/darwin.c @@ -179,12 +179,14 @@ void uv__cf_loop_signal(uv_loop_t* loop, cf_loop_signal_cb cb, void* arg) { uint64_t uv__hrtime(void) { - mach_timebase_info_data_t info; + static mach_timebase_info_data_t info; - if (mach_timebase_info(&info) != KERN_SUCCESS) - abort(); + if ((ACCESS_ONCE(uint32_t, info.numer) == 0 || + ACCESS_ONCE(uint32_t, info.denom) == 0) && + mach_timebase_info(&info) != KERN_SUCCESS) + abort(); - return mach_absolute_time() * info.numer / info.denom; + return mach_absolute_time() * info.numer / info.denom; }