From 765c7e711a0759366a5fca4c3dec66738ef7cde9 Mon Sep 17 00:00:00 2001 From: Lioncash Date: Fri, 20 Jul 2018 15:27:17 -0400 Subject: [PATCH] logging/backend: Use std::string_view in RemoveBackend() and GetBackend() These can just use a view to a string since its only comparing against two names in both cases for matches. This avoids constructing std::string instances where they aren't necessary. --- src/common/logging/backend.cpp | 20 ++++++++++---------- src/common/logging/backend.h | 5 +++-- 2 files changed, 13 insertions(+), 12 deletions(-) diff --git a/src/common/logging/backend.cpp b/src/common/logging/backend.cpp index 0166beff0..de0ce23b3 100644 --- a/src/common/logging/backend.cpp +++ b/src/common/logging/backend.cpp @@ -47,11 +47,11 @@ public: backends.push_back(std::move(backend)); } - void RemoveBackend(const std::string& backend_name) { + void RemoveBackend(std::string_view backend_name) { std::lock_guard lock(writing_mutex); - auto it = std::remove_if(backends.begin(), backends.end(), [&backend_name](const auto& i) { - return !strcmp(i->GetName(), backend_name.c_str()); - }); + const auto it = + std::remove_if(backends.begin(), backends.end(), + [&backend_name](const auto& i) { return backend_name == i->GetName(); }); backends.erase(it, backends.end()); } @@ -63,10 +63,10 @@ public: filter = f; } - Backend* GetBackend(const std::string& backend_name) { - auto it = std::find_if(backends.begin(), backends.end(), [&backend_name](const auto& i) { - return !strcmp(i->GetName(), backend_name.c_str()); - }); + Backend* GetBackend(std::string_view backend_name) { + const auto it = + std::find_if(backends.begin(), backends.end(), + [&backend_name](const auto& i) { return backend_name == i->GetName(); }); if (it == backends.end()) return nullptr; return it->get(); @@ -268,11 +268,11 @@ void AddBackend(std::unique_ptr backend) { Impl::Instance().AddBackend(std::move(backend)); } -void RemoveBackend(const std::string& backend_name) { +void RemoveBackend(std::string_view backend_name) { Impl::Instance().RemoveBackend(backend_name); } -Backend* GetBackend(const std::string& backend_name) { +Backend* GetBackend(std::string_view backend_name) { return Impl::Instance().GetBackend(backend_name); } diff --git a/src/common/logging/backend.h b/src/common/logging/backend.h index f738869db..595810561 100644 --- a/src/common/logging/backend.h +++ b/src/common/logging/backend.h @@ -8,6 +8,7 @@ #include #include #include +#include #include #include "common/file_util.h" #include "common/logging/filter.h" @@ -107,9 +108,9 @@ private: void AddBackend(std::unique_ptr backend); -void RemoveBackend(const std::string& backend_name); +void RemoveBackend(std::string_view backend_name); -Backend* GetBackend(const std::string& backend_name); +Backend* GetBackend(std::string_view backend_name); /** * Returns the name of the passed log class as a C-string. Subclasses are separated by periods