if the map is small do a sequential search, otherwise use the standard find()

This commit is contained in:
gabime 2024-03-17 00:14:56 +02:00
parent 06e4631dde
commit 4a31ed38d3

View File

@ -68,15 +68,27 @@ namespace spdlog {
}
}
// if the map is small do a sequential search, otherwise use the standard find()
std::shared_ptr<logger> registry::get(const std::string &logger_name) {
std::lock_guard<std::mutex> 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<logger> registry::get(std::string_view logger_name) {
std::lock_guard<std::mutex> 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) {