From f6b200568742cbd94dc8cac17e7ac6728cc82008 Mon Sep 17 00:00:00 2001 From: Skirmantas Kligys Date: Sun, 2 Feb 2020 14:44:26 -0800 Subject: [PATCH] Cache calendar tooltip text to reduce computations. --- src/modules/clock.cpp | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/src/modules/clock.cpp b/src/modules/clock.cpp index 5efca403..e7193539 100644 --- a/src/modules/clock.cpp +++ b/src/modules/clock.cpp @@ -1,4 +1,5 @@ #include "modules/clock.hpp" +#include #include using zoned_time = date::zoned_time; @@ -26,9 +27,28 @@ void weekdays_header(const std::locale& locale, const date::weekday& first_dow, os << "\n"; } +struct CachedCalendar { + date::year_month_day ymd; + std::string text; + + void set(const date::year_month_day& ymd_, std::string text_) { + ymd = ymd_; + text = text_; + } +}; + +std::mutex cached_calendar_mutex; // protects cached_calendar. +CachedCalendar cached_calendar; + std::string calendar_text(const waybar_time& wtime, const date::weekday& first_dow) { const auto daypoint = date::floor(wtime.ztime.get_local_time()); const auto ymd = date::year_month_day(daypoint); + + const std::lock_guard lock(cached_calendar_mutex); + if (cached_calendar.ymd == ymd) { + return cached_calendar.text; + } + const date::year_month ym(ymd.year(), ymd.month()); const auto curr_day = ymd.day(); @@ -63,7 +83,9 @@ std::string calendar_text(const waybar_time& wtime, const date::weekday& first_d } } - return os.str(); + auto result = os.str(); + cached_calendar.set(ymd, result); + return result; } }