Use std::string_view in dbus::Bus::GetObjectProxy
This change improves the efficiency of call sites which typically
invoke this method with a compile-time constant.
Change-Id: Ib8988d4b4ffaaba9c32b511dbd6d5bd9bca9a659
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6033411
Reviewed-by: Steven Bennetts <stevenjb@chromium.org>
Commit-Queue: Reilly Grant <reillyg@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1384658}
NOKEYCHECK=True
GitOrigin-RevId: 887ed0e584ffacdcafc3010fd893aeb3ba8b2184
diff --git a/bus.cc b/bus.cc
index f15fbce..8408806 100644
--- a/bus.cc
+++ b/bus.cc
@@ -14,6 +14,7 @@
#include "base/logging.h"
#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
+#include "base/strings/strcat.h"
#include "base/strings/stringprintf.h"
#include "base/synchronization/waitable_event.h"
#include "base/task/sequenced_task_runner.h"
@@ -201,20 +202,20 @@
// DCHECK_EQ(0, num_pending_timeouts_);
}
-ObjectProxy* Bus::GetObjectProxy(const std::string& service_name,
+ObjectProxy* Bus::GetObjectProxy(std::string_view service_name,
const ObjectPath& object_path) {
return GetObjectProxyWithOptions(service_name, object_path,
ObjectProxy::DEFAULT_OPTIONS);
}
-ObjectProxy* Bus::GetObjectProxyWithOptions(const std::string& service_name,
+ObjectProxy* Bus::GetObjectProxyWithOptions(std::string_view service_name,
const ObjectPath& object_path,
int options) {
AssertOnOriginThread();
// Check if we already have the requested object proxy.
- const ObjectProxyTable::key_type key(service_name + object_path.value(),
- options);
+ const ObjectProxyTable::key_type key(
+ base::StrCat({service_name, object_path.value()}), options);
ObjectProxyTable::iterator iter = object_proxy_table_.find(key);
if (iter != object_proxy_table_.end()) {
return iter->second.get();
@@ -227,7 +228,7 @@
return object_proxy.get();
}
-bool Bus::RemoveObjectProxy(const std::string& service_name,
+bool Bus::RemoveObjectProxy(std::string_view service_name,
const ObjectPath& object_path,
base::OnceClosure callback) {
return RemoveObjectProxyWithOptions(service_name, object_path,
@@ -235,15 +236,15 @@
std::move(callback));
}
-bool Bus::RemoveObjectProxyWithOptions(const std::string& service_name,
+bool Bus::RemoveObjectProxyWithOptions(std::string_view service_name,
const ObjectPath& object_path,
int options,
base::OnceClosure callback) {
AssertOnOriginThread();
// Check if we have the requested object proxy.
- const ObjectProxyTable::key_type key(service_name + object_path.value(),
- options);
+ const ObjectProxyTable::key_type key(
+ base::StrCat({service_name, object_path.value()}), options);
ObjectProxyTable::iterator iter = object_proxy_table_.find(key);
if (iter != object_proxy_table_.end()) {
scoped_refptr<ObjectProxy> object_proxy = iter->second;
diff --git a/bus.h b/bus.h
index 2ddda8b..9cae4d2 100644
--- a/bus.h
+++ b/bus.h
@@ -12,6 +12,7 @@
#include <memory>
#include <set>
#include <string>
+#include <string_view>
#include <utility>
#include <vector>
@@ -266,13 +267,13 @@
// |object_path| looks like "/org/freedesktop/NetworkManager/Devices/0".
//
// Must be called in the origin thread.
- virtual ObjectProxy* GetObjectProxy(const std::string& service_name,
+ virtual ObjectProxy* GetObjectProxy(std::string_view service_name,
const ObjectPath& object_path);
// Same as above, but also takes a bitfield of ObjectProxy::Options.
// See object_proxy.h for available options.
virtual ObjectProxy* GetObjectProxyWithOptions(
- const std::string& service_name,
+ std::string_view service_name,
const ObjectPath& object_path,
int options);
@@ -300,13 +301,13 @@
// never called. The |callback| argument must not be null.
//
// Must be called in the origin thread.
- virtual bool RemoveObjectProxy(const std::string& service_name,
+ virtual bool RemoveObjectProxy(std::string_view service_name,
const ObjectPath& object_path,
base::OnceClosure callback);
// Same as above, but also takes a bitfield of ObjectProxy::Options.
// See object_proxy.h for available options.
- virtual bool RemoveObjectProxyWithOptions(const std::string& service_name,
+ virtual bool RemoveObjectProxyWithOptions(std::string_view service_name,
const ObjectPath& object_path,
int options,
base::OnceClosure callback);
diff --git a/mock_bus.h b/mock_bus.h
index 68909fd..4f9aaef 100644
--- a/mock_bus.h
+++ b/mock_bus.h
@@ -6,6 +6,7 @@
#define DBUS_MOCK_BUS_H_
#include <stdint.h>
+#include <string>
#include "base/task/sequenced_task_runner.h"
#include "dbus/bus.h"
@@ -22,10 +23,10 @@
public:
MockBus(const Bus::Options& options);
- MOCK_METHOD2(GetObjectProxy, ObjectProxy*(const std::string& service_name,
+ MOCK_METHOD2(GetObjectProxy, ObjectProxy*(std::string_view service_name,
const ObjectPath& object_path));
MOCK_METHOD3(GetObjectProxyWithOptions,
- ObjectProxy*(const std::string& service_name,
+ ObjectProxy*(std::string_view service_name,
const ObjectPath& object_path,
int options));
MOCK_METHOD1(GetExportedObject, ExportedObject*(
diff --git a/object_proxy.cc b/object_proxy.cc
index 3cfedf3..19ca666 100644
--- a/object_proxy.cc
+++ b/object_proxy.cc
@@ -111,7 +111,7 @@
}
ObjectProxy::ObjectProxy(Bus* bus,
- const std::string& service_name,
+ std::string_view service_name,
const ObjectPath& object_path,
int options)
: bus_(bus),
diff --git a/object_proxy.h b/object_proxy.h
index ae3dcf3..ec9c1be 100644
--- a/object_proxy.h
+++ b/object_proxy.h
@@ -11,6 +11,7 @@
#include <memory>
#include <set>
#include <string>
+#include <string_view>
#include <vector>
#include "base/functional/callback.h"
@@ -44,7 +45,7 @@
// Client code should use Bus::GetObjectProxy() or
// Bus::GetObjectProxyWithOptions() instead of this constructor.
ObjectProxy(Bus* bus,
- const std::string& service_name,
+ std::string_view service_name,
const ObjectPath& object_path,
int options);