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
This commit is contained in:
Charles Kerr 2023-06-12 02:55:22 -05:00 committed by GitHub
parent fa6d14c22d
commit ab49e98401
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 44 additions and 40 deletions

View file

@ -169,7 +169,7 @@ base::Time ParseTimeProperty(const absl::optional<double>& value) {
return base::Time::FromDoubleT(*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)) if (status.HasExclusionReason(net::CookieInclusionStatus::EXCLUDE_HTTP_ONLY))
return "Failed to create httponly cookie"; return "Failed to create httponly cookie";
if (status.HasExclusionReason( if (status.HasExclusionReason(

View file

@ -167,7 +167,7 @@ const char* const kBuiltinSchemes[] = {
}; };
// Convert error code to string. // Convert error code to string.
std::string ErrorCodeToString(ProtocolError error) { constexpr base::StringPiece ErrorCodeToString(ProtocolError error) {
switch (error) { switch (error) {
case ProtocolError::kRegistered: case ProtocolError::kRegistered:
return "The scheme has been registered"; return "The scheme has been registered";

View file

@ -25,33 +25,36 @@ namespace electron::api {
namespace { namespace {
std::string MessageSourceToString( constexpr base::StringPiece MessageSourceToString(
const blink::mojom::ConsoleMessageSource source) { const blink::mojom::ConsoleMessageSource source) {
if (source == blink::mojom::ConsoleMessageSource::kXml) switch (source) {
case blink::mojom::ConsoleMessageSource::kXml:
return "xml"; return "xml";
if (source == blink::mojom::ConsoleMessageSource::kJavaScript) case blink::mojom::ConsoleMessageSource::kJavaScript:
return "javascript"; return "javascript";
if (source == blink::mojom::ConsoleMessageSource::kNetwork) case blink::mojom::ConsoleMessageSource::kNetwork:
return "network"; return "network";
if (source == blink::mojom::ConsoleMessageSource::kConsoleApi) case blink::mojom::ConsoleMessageSource::kConsoleApi:
return "console-api"; return "console-api";
if (source == blink::mojom::ConsoleMessageSource::kStorage) case blink::mojom::ConsoleMessageSource::kStorage:
return "storage"; return "storage";
if (source == blink::mojom::ConsoleMessageSource::kRendering) case blink::mojom::ConsoleMessageSource::kRendering:
return "rendering"; return "rendering";
if (source == blink::mojom::ConsoleMessageSource::kSecurity) case blink::mojom::ConsoleMessageSource::kSecurity:
return "security"; return "security";
if (source == blink::mojom::ConsoleMessageSource::kDeprecation) case blink::mojom::ConsoleMessageSource::kDeprecation:
return "deprecation"; return "deprecation";
if (source == blink::mojom::ConsoleMessageSource::kWorker) case blink::mojom::ConsoleMessageSource::kWorker:
return "worker"; return "worker";
if (source == blink::mojom::ConsoleMessageSource::kViolation) case blink::mojom::ConsoleMessageSource::kViolation:
return "violation"; return "violation";
if (source == blink::mojom::ConsoleMessageSource::kIntervention) case blink::mojom::ConsoleMessageSource::kIntervention:
return "intervention"; return "intervention";
if (source == blink::mojom::ConsoleMessageSource::kRecommendation) case blink::mojom::ConsoleMessageSource::kRecommendation:
return "recommendation"; return "recommendation";
default:
return "other"; return "other";
}
} }
v8::Local<v8::Value> ServiceWorkerRunningInfoToDict( v8::Local<v8::Value> ServiceWorkerRunningInfoToDict(

View file

@ -379,8 +379,9 @@ namespace electron::api {
namespace { namespace {
std::string CursorTypeToString(const ui::Cursor& cursor) { constexpr base::StringPiece CursorTypeToString(
switch (cursor.type()) { ui::mojom::CursorType cursor_type) {
switch (cursor_type) {
case ui::mojom::CursorType::kPointer: case ui::mojom::CursorType::kPointer:
return "pointer"; return "pointer";
case ui::mojom::CursorType::kCross: case ui::mojom::CursorType::kCross:
@ -3500,14 +3501,14 @@ bool WebContents::IsBeingCaptured() {
void WebContents::OnCursorChanged(const ui::Cursor& cursor) { void WebContents::OnCursorChanged(const ui::Cursor& cursor) {
if (cursor.type() == ui::mojom::CursorType::kCustom) { if (cursor.type() == ui::mojom::CursorType::kCustom) {
Emit("cursor-changed", CursorTypeToString(cursor), Emit("cursor-changed", CursorTypeToString(cursor.type()),
gfx::Image::CreateFrom1xBitmap(cursor.custom_bitmap()), gfx::Image::CreateFrom1xBitmap(cursor.custom_bitmap()),
cursor.image_scale_factor(), cursor.image_scale_factor(),
gfx::Size(cursor.custom_bitmap().width(), gfx::Size(cursor.custom_bitmap().width(),
cursor.custom_bitmap().height()), cursor.custom_bitmap().height()),
cursor.custom_hotspot()); cursor.custom_hotspot());
} else { } else {
Emit("cursor-changed", CursorTypeToString(cursor)); Emit("cursor-changed", CursorTypeToString(cursor.type()));
} }
} }

View file

@ -19,7 +19,8 @@
namespace { namespace {
std::string MediaStreamTypeToString(blink::mojom::MediaStreamType type) { constexpr base::StringPiece MediaStreamTypeToString(
blink::mojom::MediaStreamType type) {
switch (type) { switch (type) {
case blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE: case blink::mojom::MediaStreamType::DEVICE_AUDIO_CAPTURE:
return "audio"; return "audio";

View file

@ -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; using Modifiers = blink::WebInputEvent::Modifiers;
std::vector<std::string> modifier_strings; std::vector<base::StringPiece> modifier_strings;
if (modifiers & Modifiers::kShiftKey) if (modifiers & Modifiers::kShiftKey)
modifier_strings.push_back("shift"); modifier_strings.push_back("shift");
if (modifiers & Modifiers::kControlKey) if (modifiers & Modifiers::kControlKey)

View file

@ -383,7 +383,7 @@ class WebFrameRenderer : public gin::Wrappable<WebFrameRenderer>,
private: private:
bool MaybeGetRenderFrame(v8::Isolate* isolate, bool MaybeGetRenderFrame(v8::Isolate* isolate,
const std::string& method_name, const base::StringPiece method_name,
content::RenderFrame** render_frame_ptr) { content::RenderFrame** render_frame_ptr) {
std::string error_msg; std::string error_msg;
if (!MaybeGetRenderFrame(&error_msg, method_name, render_frame_ptr)) { 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, bool MaybeGetRenderFrame(std::string* error_msg,
const std::string& method_name, const base::StringPiece method_name,
content::RenderFrame** render_frame_ptr) { content::RenderFrame** render_frame_ptr) {
auto* frame = render_frame(); auto* frame = render_frame();
if (!frame) { if (!frame) {
*error_msg = "Render frame was torn down before webFrame." + method_name + *error_msg = base::ToString("Render frame was torn down before webFrame.",
" could be " method_name, " could be executed");
"executed";
return false; return false;
} }
*render_frame_ptr = frame; *render_frame_ptr = frame;