perf: prefer base::SplitStringPiece() over base::SplitString() (#45924)

* perf: use base::SplitStringPiece() in SetNodeOptions()

* perf: use base::SplitStringPiece() in StringToAccelerator()

* refactor: StringToAccelerator() now takes a std::string_view
This commit is contained in:
Charles Kerr 2025-03-09 17:36:13 -05:00 committed by GitHub
parent 20414f66ca
commit 288ef37b1d
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 9 additions and 8 deletions

View file

@ -7,6 +7,7 @@
#include <stdio.h> #include <stdio.h>
#include <string> #include <string>
#include <string_view>
#include <vector> #include <vector>
#include "base/logging.h" #include "base/logging.h"
@ -16,7 +17,7 @@
namespace accelerator_util { namespace accelerator_util {
bool StringToAccelerator(const std::string& shortcut, bool StringToAccelerator(const std::string_view shortcut,
ui::Accelerator* accelerator) { ui::Accelerator* accelerator) {
if (!base::IsStringASCII(shortcut)) { if (!base::IsStringASCII(shortcut)) {
LOG(ERROR) << "The accelerator string can only contain ASCII characters, " LOG(ERROR) << "The accelerator string can only contain ASCII characters, "
@ -26,14 +27,14 @@ bool StringToAccelerator(const std::string& shortcut,
return false; return false;
} }
std::vector<std::string> tokens = base::SplitString( const std::vector<std::string_view> tokens = base::SplitStringPiece(
shortcut, "+", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); shortcut, "+", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
// Now, parse it into an accelerator. // Now, parse it into an accelerator.
int modifiers = ui::EF_NONE; int modifiers = ui::EF_NONE;
ui::KeyboardCode key = ui::VKEY_UNKNOWN; ui::KeyboardCode key = ui::VKEY_UNKNOWN;
std::optional<char16_t> shifted_char; std::optional<char16_t> shifted_char;
for (const auto& token : tokens) { for (const std::string_view token : tokens) {
ui::KeyboardCode code = electron::KeyboardCodeFromStr(token, &shifted_char); ui::KeyboardCode code = electron::KeyboardCodeFromStr(token, &shifted_char);
if (shifted_char) if (shifted_char)
modifiers |= ui::EF_SHIFT_DOWN; modifiers |= ui::EF_SHIFT_DOWN;

View file

@ -6,7 +6,7 @@
#define ELECTRON_SHELL_BROWSER_UI_ACCELERATOR_UTIL_H_ #define ELECTRON_SHELL_BROWSER_UI_ACCELERATOR_UTIL_H_
#include <map> #include <map>
#include <string> #include <string_view>
#include "base/memory/raw_ptr.h" #include "base/memory/raw_ptr.h"
#include "shell/browser/ui/electron_menu_model.h" #include "shell/browser/ui/electron_menu_model.h"
@ -21,7 +21,7 @@ typedef struct {
typedef std::map<ui::Accelerator, MenuItem> AcceleratorTable; typedef std::map<ui::Accelerator, MenuItem> AcceleratorTable;
// Parse a string as an accelerator. // Parse a string as an accelerator.
bool StringToAccelerator(const std::string& shortcut, bool StringToAccelerator(std::string_view shortcut,
ui::Accelerator* accelerator); ui::Accelerator* accelerator);
// Generate a table that contains menu model's accelerators and command ids. // Generate a table that contains menu model's accelerators and command ids.

View file

@ -395,14 +395,14 @@ void SetNodeOptions(base::Environment* env) {
if (electron::fuses::IsNodeOptionsEnabled()) { if (electron::fuses::IsNodeOptionsEnabled()) {
std::string options; std::string options;
env->GetVar("NODE_OPTIONS", &options); env->GetVar("NODE_OPTIONS", &options);
std::vector<std::string> parts = base::SplitString( const std::vector<std::string_view> parts = base::SplitStringPiece(
options, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY); options, " ", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
bool is_packaged_app = electron::api::App::IsPackaged(); bool is_packaged_app = electron::api::App::IsPackaged();
for (const auto& part : parts) { for (const std::string_view part : parts) {
// Strip off values passed to individual NODE_OPTIONs // Strip off values passed to individual NODE_OPTIONs
std::string option = part.substr(0, part.find('=')); const std::string_view option = part.substr(0, part.find('='));
if (is_packaged_app && !pkg_opts.contains(option)) { if (is_packaged_app && !pkg_opts.contains(option)) {
// Explicitly disallow majority of NODE_OPTIONS in packaged apps // Explicitly disallow majority of NODE_OPTIONS in packaged apps