From e03c160e274643a67ca4834aef41f7cfd461f261 Mon Sep 17 00:00:00 2001 From: gabime Date: Thu, 6 Jun 2019 18:19:36 +0300 Subject: [PATCH] Optmize set_formatter to avoid redundant clone --- include/spdlog/logger-inl.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/include/spdlog/logger-inl.h b/include/spdlog/logger-inl.h index 5a7143ce..f3cdb6ab 100644 --- a/include/spdlog/logger-inl.h +++ b/include/spdlog/logger-inl.h @@ -64,13 +64,21 @@ SPDLOG_INLINE const std::string &logger::name() const return name_; } -// set formatting for the sinks in this logger. +// set formatting for the sinks in this logger. redundant // each sink will get a seperate instance of the formatter object. SPDLOG_INLINE void logger::set_formatter(std::unique_ptr f) { - for (auto &sink : sinks_) + for (auto it = sinks_.begin(); it != sinks_.end(); ++it) { - sink->set_formatter(f->clone()); + if (std::next(it) == sinks_.end()) + { + // last element - we can be move it. + (*it)->set_formatter(std::move(f)); + } + else + { + (*it)->set_formatter(f->clone()); + } } }