Browse Source

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
Charles Kerr 1 year ago
parent
commit
ab49e98401

+ 1 - 1
shell/browser/api/electron_api_cookies.cc

@@ -169,7 +169,7 @@ base::Time ParseTimeProperty(const absl::optional<double>& 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(

+ 1 - 1
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";

+ 29 - 26
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<v8::Value> ServiceWorkerRunningInfoToDict(

+ 5 - 4
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()));
   }
 }
 

+ 2 - 1
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";

+ 2 - 2
shell/common/gin_converters/blink_converter.cc

@@ -180,9 +180,9 @@ struct Converter<blink::WebInputEvent::Modifiers> {
   }
 };
 
-std::vector<std::string> ModifiersToArray(int modifiers) {
+std::vector<base::StringPiece> ModifiersToArray(int modifiers) {
   using Modifiers = blink::WebInputEvent::Modifiers;
-  std::vector<std::string> modifier_strings;
+  std::vector<base::StringPiece> modifier_strings;
   if (modifiers & Modifiers::kShiftKey)
     modifier_strings.push_back("shift");
   if (modifiers & Modifiers::kControlKey)

+ 4 - 5
shell/renderer/api/electron_api_web_frame.cc

@@ -383,7 +383,7 @@ class WebFrameRenderer : public gin::Wrappable<WebFrameRenderer>,
 
  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<WebFrameRenderer>,
   }
 
   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;