Skip to content

Commit 4a31ed3

Browse files
committed
if the map is small do a sequential search, otherwise use the standard find()
1 parent 06e4631 commit 4a31ed3

File tree

1 file changed

+16
-4
lines changed

1 file changed

+16
-4
lines changed

src/details/registry.cpp

+16-4
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,27 @@ namespace spdlog {
6868
}
6969
}
7070

71+
// if the map is small do a sequential search, otherwise use the standard find()
7172
std::shared_ptr<logger> registry::get(const std::string &logger_name) {
72-
std::lock_guard<std::mutex> lock(logger_map_mutex_);
73-
auto found = loggers_.find(logger_name);
74-
return found == loggers_.end() ? nullptr : found->second;
73+
if (loggers_.size() <= 20) {
74+
for (const auto &[key, val]: loggers_) {
75+
if (logger_name == key) {
76+
return val;
77+
}
78+
}
79+
return nullptr;
80+
}
81+
else {
82+
auto found = loggers_.find(logger_name);
83+
return found == loggers_.end() ? nullptr : found->second;
84+
}
7585
}
7686

87+
// if the map is small do a sequential search and avoid creating string for find(logger_name)
88+
// otherwise use the standard find()
7789
std::shared_ptr<logger> registry::get(std::string_view logger_name) {
7890
std::lock_guard<std::mutex> lock(logger_map_mutex_);
79-
// if the map is small do a sequential search to avoid creating string for find(logger_name)
91+
8092
if (loggers_.size() <= 20) {
8193
for (const auto &[key, val]: loggers_) {
8294
if (logger_name == key) {

0 commit comments

Comments
 (0)