From 42e4d74992fccb95d20a293b44197ebf3d0155d5 Mon Sep 17 00:00:00 2001 From: Sebastian Ullrich Date: Thu, 9 Feb 2023 14:48:16 +0100 Subject: [PATCH] perf: optimize negative `time_task` --- src/library/time_task.cpp | 24 ++++++++++++++---------- 1 file changed, 14 insertions(+), 10 deletions(-) diff --git a/src/library/time_task.cpp b/src/library/time_task.cpp index d2ae2fa70c8a..4d359cab5cc0 100644 --- a/src/library/time_task.cpp +++ b/src/library/time_task.cpp @@ -43,17 +43,21 @@ void finalize_time_task() { time_task::time_task(std::string const & category, options const & opts, name decl) : m_category(category) { - if (get_profiler(opts)) { + if (g_current_time_task && !m_category.size()) { + // exclude given block from surrounding task + // avoid somewhat costly accesses on `opts` + m_timeit = optional([](second_duration _) {}); + m_parent_task = g_current_time_task; + g_current_time_task = this; + } else if (get_profiler(opts)) { m_timeit = optional(get_profiling_threshold(opts), [=](second_duration duration) mutable { - if (m_category.size()) { - sstream ss; - ss << m_category; - if (decl) - ss << " of " << decl; - ss << " took " << display_profiling_time{duration} << "\n"; - // output atomically, like IO.print - tout() << ss.str(); - } + sstream ss; + ss << m_category; + if (decl) + ss << " of " << decl; + ss << " took " << display_profiling_time{duration} << "\n"; + // output atomically, like IO.print + tout() << ss.str(); }); m_parent_task = g_current_time_task; g_current_time_task = this;