From 51151620661e6b2a204a139a72fea86f839ea123 Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Tue, 9 Nov 2021 15:37:42 -0400 Subject: [PATCH] Use libuv's cached current time for `time_now()` We are calling time_now() a huge amount, and it is a major consumer of CPU cycles, but we don't need it: most of the time the current event loop time is enough. --- llarp/ev/ev.hpp | 8 ++++---- llarp/ev/ev_libuv.hpp | 6 ++++++ 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/llarp/ev/ev.hpp b/llarp/ev/ev.hpp index f39d3041f..898d526a3 100644 --- a/llarp/ev/ev.hpp +++ b/llarp/ev/ev.hpp @@ -75,11 +75,11 @@ namespace llarp virtual bool running() const = 0; + // Returns a current steady clock time value representing the current time with event loop tick + // granularity. That is, the value is typically only updated at the beginning of an event loop + // tick. virtual llarp_time_t - time_now() const - { - return llarp::time_now_ms(); - } + time_now() const = 0; // Calls a function/lambda/etc. If invoked from within the event loop itself this calls the // given lambda immediately; otherwise it passes it to `call_soon()` to be queued to run at the diff --git a/llarp/ev/ev_libuv.hpp b/llarp/ev/ev_libuv.hpp index 86dacac79..62c3d5a4c 100644 --- a/llarp/ev/ev_libuv.hpp +++ b/llarp/ev/ev_libuv.hpp @@ -31,6 +31,12 @@ namespace llarp::uv bool running() const override; + llarp_time_t + time_now() const override + { + return m_Impl->now(); + } + void call_later(llarp_time_t delay_ms, std::function callback) override;