From 167784f93bcc73407fec6ed04ea3b18ee7491cd0 Mon Sep 17 00:00:00 2001 From: Robbert van der Helm Date: Sat, 1 May 2021 16:16:25 +0200 Subject: [PATCH] Prevent unnecessary copies in ScopedValueCache --- src/common/utils.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/common/utils.h b/src/common/utils.h index 4f14e4fd..7dc15413 100644 --- a/src/common/utils.h +++ b/src/common/utils.h @@ -144,19 +144,20 @@ class ScopedValueCache { class Guard { public: Guard(std::optional& cached_value) : cached_value(cached_value) {} - ~Guard() { if (is_active) { - cached_value.reset(); + cached_value.get().reset(); } } Guard(const Guard&) = delete; Guard& operator=(const Guard&) = delete; - Guard(Guard&& o) : cached_value(o.cached_value) { o.is_active = false; } + Guard(Guard&& o) : cached_value(std::move(o.cached_value)) { + o.is_active = false; + } Guard& operator=(Guard&& o) { - cached_value = o.cache; + cached_value = std::move(o.cached_value); o.is_active = false; return *this; @@ -164,7 +165,7 @@ class ScopedValueCache { private: bool is_active = true; - std::optional& cached_value; + std::reference_wrapper> cached_value; }; /**