From 4052bc0621ab6d52a76e7dc36ba75a0d1dd29ae5 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 17 Mar 2024 00:27:43 +0200 Subject: [PATCH] Use find if registry is bigger than 20 in registry::get(std::string_view logger_name) --- include/spdlog/details/registry-inl.h | 35 +++++++++++++++++++++------ 1 file changed, 27 insertions(+), 8 deletions(-) diff --git a/include/spdlog/details/registry-inl.h b/include/spdlog/details/registry-inl.h index ab390aeb..4be34c9e 100644 --- a/include/spdlog/details/registry-inl.h +++ b/include/spdlog/details/registry-inl.h @@ -78,21 +78,40 @@ SPDLOG_INLINE void registry::initialize_logger(std::shared_ptr new_logge } } -SPDLOG_INLINE std::shared_ptr registry::get(const std::string &logger_name) { - std::lock_guard lock(logger_map_mutex_); - auto found = loggers_.find(logger_name); - return found == loggers_.end() ? nullptr : found->second; +// if the map is small do a sequential search, otherwise use the standard find() +std::shared_ptr registry::get(const std::string &logger_name) { + if (loggers_.size() <= 20) { + for (const auto &[key, val]: loggers_) { + if (logger_name == key) { + return val; + } + } + return nullptr; + } + else { + auto found = loggers_.find(logger_name); + return found == loggers_.end() ? nullptr : found->second; + } } #if __cplusplus >= 201703L // C++17 +// if the map is small do a sequential search and avoid creating string for find(logger_name) +// otherwise use the standard find() SPDLOG_INLINE std::shared_ptr registry::get(std::string_view logger_name) { std::lock_guard lock(logger_map_mutex_); - for (const auto &[key, val] : loggers_) { - if (key == logger_name) { - return val; + if (loggers_.size() <= 20) { + for (const auto &[key, val]: loggers_) { + if (logger_name == key) { + return val; + } } + return nullptr; + } + // otherwise use the normal map lookup + else { + auto found = loggers_.find(std::string(logger_name)); + return found == loggers_.end() ? nullptr : found->second; } - return nullptr; } SPDLOG_INLINE std::shared_ptr registry::get(const char *logger_name) {