From 06e4631dde759197d4126e560c0c8d8aa741defb Mon Sep 17 00:00:00 2001 From: gabime Date: Sat, 16 Mar 2024 23:47:39 +0200 Subject: [PATCH] Use find if registry is bigger than 20 --- src/details/registry.cpp | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/src/details/registry.cpp b/src/details/registry.cpp index 9944f54c..d96a9247 100644 --- a/src/details/registry.cpp +++ b/src/details/registry.cpp @@ -12,7 +12,9 @@ #ifdef _WIN32 #include "spdlog/sinks/wincolor_sink.h" #else + #include "spdlog/sinks/ansicolor_sink.h" + #endif #endif // SPDLOG_DISABLE_DEFAULT_LOGGER @@ -74,12 +76,20 @@ namespace spdlog { std::shared_ptr registry::get(std::string_view logger_name) { std::lock_guard lock(logger_map_mutex_); - for (const auto &[key, val]: loggers_) { - if (logger_name == key) { - return val; + // if the map is small do a sequential search to avoid creating string for find(logger_name) + 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; } std::shared_ptr registry::get(const char *logger_name) {