From ab49e9840184d7efd82d9c5eecf0f4158def51b3 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Mon, 12 Jun 2023 02:55:22 -0500 Subject: [PATCH] perf: prefer base::StringPiece over std::string for build-time strings (#38717) * perf: use base::StringPiece in InclusionStatusToString() The strings are all build-time constants and this is a private function * perf: use base::StringPiece in ErrorCodeToString() The strings are all build-time constants and this is a private function * perf: use base::StringPiece in MessageSourceToString() The strings are all build-time constants and this is a private function * perf: use base::StringPiece in CursorTypeToString() The strings are all build-time constants and this is a private function * perf: use base::StringPiece in MediaStreamTypeToString() The strings are all build-time constants and this is a private function * perf: use base::StringPiece in ModifiersToArray() The strings are all build-time constants and this is a private function * perf: use base::StringPiece in WebFrameRenderer::MaybeGetRenderFrame() The strings are all build-time constants and this is a private function --- shell/browser/api/electron_api_cookies.cc | 2 +- shell/browser/api/electron_api_protocol.cc | 2 +- .../electron_api_service_worker_context.cc | 55 ++++++++++--------- .../browser/api/electron_api_web_contents.cc | 9 +-- .../browser/web_contents_permission_helper.cc | 3 +- .../common/gin_converters/blink_converter.cc | 4 +- shell/renderer/api/electron_api_web_frame.cc | 9 ++- 7 files changed, 44 insertions(+), 40 deletions(-) diff --git a/shell/browser/api/electron_api_cookies.cc b/shell/browser/api/electron_api_cookies.cc index 41115a0db7b8..b7ec0a733ad8 100644 --- a/shell/browser/api/electron_api_cookies.cc +++ b/shell/browser/api/electron_api_cookies.cc @@ -169,7 +169,7 @@ base::Time ParseTimeProperty(const absl::optional& value) { return base::Time::FromDoubleT(*value); } -std::string InclusionStatusToString(net::CookieInclusionStatus status) { +base::StringPiece InclusionStatusToString(net::CookieInclusionStatus status) { if (status.HasExclusionReason(net::CookieInclusionStatus::EXCLUDE_HTTP_ONLY)) return "Failed to create httponly cookie"; if (status.HasExclusionReason( diff --git a/shell/browser/api/electron_api_protocol.cc b/shell/browser/api/electron_api_protocol.cc index 6bbec23965ee..59b8ad6ced88 100644 --- a/shell/browser/api/electron_api_protocol.cc +++ b/shell/browser/api/electron_api_protocol.cc @@ -167,7 +167,7 @@ const char* const kBuiltinSchemes[] = { }; // Convert error code to string. -std::string ErrorCodeToString(ProtocolError error) { +constexpr base::StringPiece ErrorCodeToString(ProtocolError error) { switch (error) { case ProtocolError::kRegistered: return "The scheme has been registered"; diff --git a/shell/browser/api/electron_api_service_worker_context.cc b/shell/browser/api/electron_api_service_worker_context.cc index 959a87bc68a0..ef2a0ceeab7d 100644 --- a/shell/browser/api/electron_api_service_worker_context.cc +++ b/shell/browser/api/electron_api_service_worker_context.cc @@ -25,33 +25,36 @@ namespace electron::api { namespace { -std::string MessageSourceToString( +constexpr base::StringPiece MessageSourceToString( const blink::mojom::ConsoleMessageSource source) { - if (source == blink::mojom::ConsoleMessageSource::kXml) - return "xml"; - if (source == blink::mojom::ConsoleMessageSource::kJavaScript) - return "javascript"; - if (source == blink::mojom::ConsoleMessageSource::kNetwork) - return "network"; - if (source == blink::mojom::ConsoleMessageSource::kConsoleApi) - return "console-api"; - if (source == blink::mojom::ConsoleMessageSource::kStorage) - return "storage"; - if (source == blink::mojom::ConsoleMessageSource::kRendering) - return "rendering"; - if (source == blink::mojom::ConsoleMessageSource::kSecurity) - return "security"; - if (source == blink::mojom::ConsoleMessageSource::kDeprecation) - return "deprecation"; - if (source == blink::mojom::ConsoleMessageSource::kWorker) - return "worker"; - if (source == blink::mojom::ConsoleMessageSource::kViolation) - return "violation"; - if (source == blink::mojom::ConsoleMessageSource::kIntervention) - return "intervention"; - if (source == blink::mojom::ConsoleMessageSource::kRecommendation) - return "recommendation"; - return "other"; + switch (source) { + case blink::mojom::ConsoleMessageSource::kXml: + return "xml"; + case blink::mojom::ConsoleMessageSource::kJavaScript: + return "javascript"; + case blink::mojom::ConsoleMessageSource::kNetwork: + return "network"; + case blink::mojom::ConsoleMessageSource::kConsoleApi: + return "console-api"; + case blink::mojom::ConsoleMessageSource::kStorage: + return "storage"; + case blink::mojom::ConsoleMessageSource::kRendering: + return "rendering"; + case blink::mojom::ConsoleMessageSource::kSecurity: + return "security"; + case blink::mojom::ConsoleMessageSource::kDeprecation: + return "deprecation"; + case blink::mojom::ConsoleMessageSource::kWorker: + return "worker"; + case blink::mojom::ConsoleMessageSource::kViolation: + return "violation"; + case blink::mojom::ConsoleMessageSource::kIntervention: + return "intervention"; + case blink::mojom::ConsoleMessageSource::kRecommendation: + return "recommendation"; + default: + return "other"; + } } v8::Local ServiceWorkerRunningInfoToDict( diff --git a/shell/browser/api/electron_api_web_contents.cc b/shell/browser/api/electron_api_web_contents.cc index 4a2e08a0803a..d3200559eb86 100644 --- a/shell/browser/api/electron_api_web_contents.cc +++ b/shell/browser/api/electron_api_web_contents.cc @@ -379,8 +379,9 @@ namespace electron::api { namespace { -std::string CursorTypeToString(const ui::Cursor& cursor) { - switch (cursor.type()) { +constexpr base::StringPiece CursorTypeToString( + ui::mojom::CursorType cursor_type) { + switch (cursor_type) { case ui::mojom::CursorType::kPointer: return "pointer"; case ui::mojom::CursorType::kCross: @@ -3500,14 +3501,14 @@ bool WebContents::IsBeingCaptured() { void WebContents::OnCursorChanged(const ui::Cursor& cursor) { if (cursor.type() == ui::mojom::CursorType::kCustom) { - Emit("cursor-changed", CursorTypeToString(cursor), + Emit("cursor-changed", CursorTypeToString(cursor.type()), gfx::Image::CreateFrom1xBitmap(cursor.custom_bitmap()), cursor.image_scale_factor(), gfx::Size(cursor.custom_bitmap().width(), cursor.custom_bitmap().height()), cursor.custom_hotspot()); } else { - Emit("cursor-changed", CursorTypeToString(cursor)); + Emit("cursor-changed", CursorTypeToString(cursor.type())); } } diff --git a/shell/browser/web_contents_permission_helper.cc b/shell/browser/web_contents_permission_helper.cc index 01173c217490..fe2cd63ac4b9 100644 --- a/shell/browser/web_contents_permission_helper.cc +++ b/shell/browser/web_contents_permission_helper.cc @@ -19,7 +19,8 @@ namespace { -std::string MediaStreamTypeToString(blink::mojom::MediaStreamType type) { +constexpr base::StringPiece MediaStreamTypeToString( + blink::mojom::MediaStreamType type) { switch (type) { case blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE: return "audio"; diff --git a/shell/common/gin_converters/blink_converter.cc b/shell/common/gin_converters/blink_converter.cc index bda0113c05cd..36ca46d71774 100644 --- a/shell/common/gin_converters/blink_converter.cc +++ b/shell/common/gin_converters/blink_converter.cc @@ -180,9 +180,9 @@ struct Converter { } }; -std::vector ModifiersToArray(int modifiers) { +std::vector ModifiersToArray(int modifiers) { using Modifiers = blink::WebInputEvent::Modifiers; - std::vector modifier_strings; + std::vector modifier_strings; if (modifiers & Modifiers::kShiftKey) modifier_strings.push_back("shift"); if (modifiers & Modifiers::kControlKey) diff --git a/shell/renderer/api/electron_api_web_frame.cc b/shell/renderer/api/electron_api_web_frame.cc index b1a2b791cc3d..7ba114f6065c 100644 --- a/shell/renderer/api/electron_api_web_frame.cc +++ b/shell/renderer/api/electron_api_web_frame.cc @@ -383,7 +383,7 @@ class WebFrameRenderer : public gin::Wrappable, private: bool MaybeGetRenderFrame(v8::Isolate* isolate, - const std::string& method_name, + const base::StringPiece method_name, content::RenderFrame** render_frame_ptr) { std::string error_msg; if (!MaybeGetRenderFrame(&error_msg, method_name, render_frame_ptr)) { @@ -394,13 +394,12 @@ class WebFrameRenderer : public gin::Wrappable, } bool MaybeGetRenderFrame(std::string* error_msg, - const std::string& method_name, + const base::StringPiece method_name, content::RenderFrame** render_frame_ptr) { auto* frame = render_frame(); if (!frame) { - *error_msg = "Render frame was torn down before webFrame." + method_name + - " could be " - "executed"; + *error_msg = base::ToString("Render frame was torn down before webFrame.", + method_name, " could be executed"); return false; } *render_frame_ptr = frame;