From b428315c6dd9aea97611ab3ef98145b870ef33ae Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 16 Apr 2024 18:48:54 -0500 Subject: [PATCH] perf: remove unnecessary `.c_str()` calls (#41869) * perf: remove unnecessary c_str() call when invoking promise.RejectWithErrorMessage() RejectWithErrorMessage() takes a std::string_view * perf: remove unnecessary c_str() call when invoking Environment::SetVar() the val arg to Environment::SetVar() takes a const std::string& * refactor: use string_view variant of base::UTF8ToWide() * perf: remove unnecessary c_str() call when instantiating a ScopedHString ScopedHString has always taken a StringPiece * refactor: use simpler invocation of base::make_span() * perf: remove unnecessary c_str() call when calling base::CommandLine::HasSwitch() HasSwitch() already takes a string_piece * perf: remove unnecessary c_str() call when calling net::HttpResponseHeaders::AddHeader() AddHeader() already takes a StringPiece arg * perf: omit unnecessary str -> wstr -> str conversion in DesktopCapturer::UpdateSourcesList() this conversion was made redundant by c670e38 --- shell/browser/api/electron_api_app.cc | 3 +-- shell/browser/api/electron_api_desktop_capturer.cc | 6 +----- shell/browser/electron_browser_main_parts.cc | 2 +- shell/browser/net/asar/asar_url_loader.cc | 2 +- .../browser/notifications/win/windows_toast_notification.cc | 2 +- shell/common/api/electron_api_command_line.cc | 5 ++--- shell/common/api/electron_api_shell.cc | 2 +- 7 files changed, 8 insertions(+), 14 deletions(-) diff --git a/shell/browser/api/electron_api_app.cc b/shell/browser/api/electron_api_app.cc index 27d2ec3fa11..01ebe1a544d 100644 --- a/shell/browser/api/electron_api_app.cc +++ b/shell/browser/api/electron_api_app.cc @@ -462,8 +462,7 @@ void OnClientCertificateSelected( return; auto certs = net::X509Certificate::CreateCertificateListFromBytes( - base::as_bytes(base::make_span(data.c_str(), data.size())), - net::X509Certificate::FORMAT_AUTO); + base::as_bytes(base::make_span(data)), net::X509Certificate::FORMAT_AUTO); if (!certs.empty()) { scoped_refptr cert(certs[0].get()); for (auto& identity : *identities) { diff --git a/shell/browser/api/electron_api_desktop_capturer.cc b/shell/browser/api/electron_api_desktop_capturer.cc index be459fb57f5..5d9bc96a03a 100644 --- a/shell/browser/api/electron_api_desktop_capturer.cc +++ b/shell/browser/api/electron_api_desktop_capturer.cc @@ -399,11 +399,7 @@ void DesktopCapturer::UpdateSourcesList(DesktopMediaList* list) { int device_name_index = 0; for (auto& source : screen_sources) { const auto& device_name = device_names[device_name_index++]; - std::wstring wide_device_name; - base::UTF8ToWide(device_name.c_str(), device_name.size(), - &wide_device_name); - const int64_t device_id = - base::PersistentHash(base::WideToUTF8(wide_device_name.c_str())); + const int64_t device_id = base::PersistentHash(device_name); source.display_id = base::NumberToString(device_id); } } diff --git a/shell/browser/electron_browser_main_parts.cc b/shell/browser/electron_browser_main_parts.cc index 05914ebafc9..c843ecbbce0 100644 --- a/shell/browser/electron_browser_main_parts.cc +++ b/shell/browser/electron_browser_main_parts.cc @@ -313,7 +313,7 @@ int ElectronBrowserMainParts::PreCreateThreads() { std::string str; if (env->GetVar("LC_ALL", &str)) lc_all.emplace(std::move(str)); - env->SetVar("LC_ALL", locale.c_str()); + env->SetVar("LC_ALL", locale); } #endif diff --git a/shell/browser/net/asar/asar_url_loader.cc b/shell/browser/net/asar/asar_url_loader.cc index c1e83c5d6e7..0891df0bfe8 100644 --- a/shell/browser/net/asar/asar_url_loader.cc +++ b/shell/browser/net/asar/asar_url_loader.cc @@ -267,7 +267,7 @@ class AsarURLLoader : public network::mojom::URLLoader { } if (head->headers) { head->headers->AddHeader(net::HttpRequestHeaders::kContentType, - head->mime_type.c_str()); + head->mime_type); } client_->OnReceiveResponse(std::move(head), std::move(consumer_handle), std::nullopt); diff --git a/shell/browser/notifications/win/windows_toast_notification.cc b/shell/browser/notifications/win/windows_toast_notification.cc index 9fcdd17d5e2..b39eded77fc 100644 --- a/shell/browser/notifications/win/windows_toast_notification.cc +++ b/shell/browser/notifications/win/windows_toast_notification.cc @@ -540,7 +540,7 @@ HRESULT WindowsToastNotification::SetXmlImage(IXmlDocument* doc, ComPtr src_attr; RETURN_IF_FAILED(attrs->GetNamedItem(src, &src_attr)); - ScopedHString img_path(icon_path.c_str()); + const ScopedHString img_path{icon_path}; if (!img_path.success()) return E_FAIL; diff --git a/shell/common/api/electron_api_command_line.cc b/shell/common/api/electron_api_command_line.cc index dd25183981a..5dcd03a550f 100644 --- a/shell/common/api/electron_api_command_line.cc +++ b/shell/common/api/electron_api_command_line.cc @@ -14,12 +14,11 @@ namespace { bool HasSwitch(const std::string& name) { - return base::CommandLine::ForCurrentProcess()->HasSwitch(name.c_str()); + return base::CommandLine::ForCurrentProcess()->HasSwitch(name); } base::CommandLine::StringType GetSwitchValue(const std::string& name) { - return base::CommandLine::ForCurrentProcess()->GetSwitchValueNative( - name.c_str()); + return base::CommandLine::ForCurrentProcess()->GetSwitchValueNative(name); } void AppendSwitch(const std::string& switch_string, diff --git a/shell/common/api/electron_api_shell.cc b/shell/common/api/electron_api_shell.cc index b05b5216b57..6881ca78499 100644 --- a/shell/common/api/electron_api_shell.cc +++ b/shell/common/api/electron_api_shell.cc @@ -51,7 +51,7 @@ void OnOpenFinished(gin_helper::Promise promise, if (error.empty()) promise.Resolve(); else - promise.RejectWithErrorMessage(error.c_str()); + promise.RejectWithErrorMessage(error); } v8::Local OpenExternal(const GURL& url, gin::Arguments* args) {