From 1552c9353b4f0d78252fa1f1e3631189991b7c95 Mon Sep 17 00:00:00 2001 From: Alan Jowett Date: Mon, 11 Nov 2024 08:09:41 -0800 Subject: [PATCH 1/3] Disable by default unused code Signed-off-by: Alan Jowett --- src/crab_utils/stats.cpp | 4 ---- src/crab_utils/stats.hpp | 25 +++++++++++++++++++++---- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/src/crab_utils/stats.cpp b/src/crab_utils/stats.cpp index f257819ae..8e9b24002 100644 --- a/src/crab_utils/stats.cpp +++ b/src/crab_utils/stats.cpp @@ -94,7 +94,6 @@ void CrabStats::reset() { sw.clear(); } -void CrabStats::count(const std::string& name) { ++(*counters)[name]; } void CrabStats::count_max(const std::string& name, const unsigned v) { (*counters)[name] = std::max((*counters)[name], v); } @@ -102,9 +101,6 @@ void CrabStats::count_max(const std::string& name, const unsigned v) { unsigned CrabStats::uset(const std::string& n, const unsigned v) { return (*counters)[n] = v; } unsigned CrabStats::get(const std::string& n) { return (*counters)[n]; } -void CrabStats::start(const std::string& name) { (*sw)[name].start(); } -void CrabStats::stop(const std::string& name) { (*sw)[name].stop(); } -void CrabStats::resume(const std::string& name) { (*sw)[name].resume(); } /** Outputs all statistics to std output */ void CrabStats::Print(std::ostream& OS) { diff --git a/src/crab_utils/stats.hpp b/src/crab_utils/stats.hpp index f402318f3..02eb220cc 100644 --- a/src/crab_utils/stats.hpp +++ b/src/crab_utils/stats.hpp @@ -33,6 +33,7 @@ inline std::ostream& operator<<(std::ostream& OS, const Stopwatch& sw) { } class CrabStats { + static const bool enabled = true; static thread_local lazy_allocator> counters; static thread_local lazy_allocator> sw; @@ -44,13 +45,29 @@ class CrabStats { /* counters */ static unsigned get(const std::string& n); static unsigned uset(const std::string& n, unsigned v); - static void count(const std::string& name); + static void count(const std::string& name) { + if (enabled) { + ++(*counters)[name]; + } + } static void count_max(const std::string& name, unsigned v); /* stop watch */ - static void start(const std::string& name); - static void stop(const std::string& name); - static void resume(const std::string& name); + static void start(const std::string& name) { + if (enabled) { + (*sw)[name].start(); + } + } + static void stop(const std::string& name) { + if (enabled) { + (*sw)[name].stop(); + } + } + static void resume(const std::string& name) { + if (enabled) { + (*sw)[name].resume(); + } + } /** Outputs all statistics to std output */ static void Print(std::ostream& OS); From 38e3c902edf83e55030518684b4a5df8903665e6 Mon Sep 17 00:00:00 2001 From: Alan Jowett Date: Mon, 11 Nov 2024 08:31:57 -0800 Subject: [PATCH 2/3] PR feedback Signed-off-by: Alan Jowett --- src/crab_utils/stats.hpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/crab_utils/stats.hpp b/src/crab_utils/stats.hpp index 02eb220cc..15c6f2892 100644 --- a/src/crab_utils/stats.hpp +++ b/src/crab_utils/stats.hpp @@ -33,7 +33,9 @@ inline std::ostream& operator<<(std::ostream& OS, const Stopwatch& sw) { } class CrabStats { - static const bool enabled = true; + /// Controls whether statistics collection is active. + /// When false, all statistics methods become no-ops for better performance. + static constexpr bool enabled = false; static thread_local lazy_allocator> counters; static thread_local lazy_allocator> sw; From 40dd829464096ebd90762a3f8601b62e52143e28 Mon Sep 17 00:00:00 2001 From: Alan Jowett Date: Mon, 11 Nov 2024 09:33:50 -0800 Subject: [PATCH 3/3] PR feedback Signed-off-by: Alan Jowett --- src/crab_utils/stats.hpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/crab_utils/stats.hpp b/src/crab_utils/stats.hpp index 15c6f2892..d60a1c49f 100644 --- a/src/crab_utils/stats.hpp +++ b/src/crab_utils/stats.hpp @@ -48,7 +48,7 @@ class CrabStats { static unsigned get(const std::string& n); static unsigned uset(const std::string& n, unsigned v); static void count(const std::string& name) { - if (enabled) { + if constexpr (enabled) { ++(*counters)[name]; } } @@ -56,17 +56,17 @@ class CrabStats { /* stop watch */ static void start(const std::string& name) { - if (enabled) { + if constexpr (enabled) { (*sw)[name].start(); } } static void stop(const std::string& name) { - if (enabled) { + if constexpr (enabled) { (*sw)[name].stop(); } } static void resume(const std::string& name) { - if (enabled) { + if constexpr (enabled) { (*sw)[name].resume(); } }