From 4a31ed38d3cf96a772a7f5175def384aec7a86b7 Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 17 Mar 2024 00:14:56 +0200 Subject: [PATCH] if the map is small do a sequential search, otherwise use the standard find() --- src/details/registry.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/src/details/registry.cpp b/src/details/registry.cpp index d96a9247..efb555c3 100644 --- a/src/details/registry.cpp +++ b/src/details/registry.cpp @@ -68,15 +68,27 @@ namespace spdlog { } } + // if the map is small do a sequential search, otherwise use the standard find() 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 (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 the map is small do a sequential search and avoid creating string for find(logger_name) + // otherwise use the standard find() std::shared_ptr registry::get(std::string_view logger_name) { std::lock_guard lock(logger_map_mutex_); - // 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) {