perf: improve temporaries in WebWorkerObserver::WorkerScriptReadyForEvaluation() (#46377)

refactor: small refactor to WebWorkerObserver::WorkerScriptReadyForEvaluation()

- replace a std::vector<std::string> local with a compile-time array
  of std::string_view
- remove .c_str() pessimization when making v8 Strings from string_views

Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com>
Co-authored-by: Charles Kerr <charles@charleskerr.com>
This commit is contained in:
trop[bot] 2025-03-31 07:33:00 -05:00 committed by GitHub
parent 073df4e738
commit 15d2a7dc4c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -4,9 +4,11 @@
#include "shell/renderer/web_worker_observer.h" #include "shell/renderer/web_worker_observer.h"
#include <string_view>
#include <utility> #include <utility>
#include "base/no_destructor.h" #include "base/no_destructor.h"
#include "base/strings/strcat.h"
#include "base/threading/thread_local.h" #include "base/threading/thread_local.h"
#include "shell/common/api/electron_bindings.h" #include "shell/common/api/electron_bindings.h"
#include "shell/common/gin_helper/event_emitter_caller.h" #include "shell/common/gin_helper/event_emitter_caller.h"
@ -71,15 +73,14 @@ void WebWorkerObserver::WorkerScriptReadyForEvaluation(
// is loaded. See corresponding change in node/init.ts. // is loaded. See corresponding change in node/init.ts.
v8::Local<v8::Object> global = worker_context->Global(); v8::Local<v8::Object> global = worker_context->Global();
std::vector<std::string> keys = {"fetch", "Response", "FormData", for (const std::string_view key :
"Request", "Headers", "EventSource"}; {"fetch", "Response", "FormData", "Request", "Headers", "EventSource"}) {
for (const auto& key : keys) {
v8::MaybeLocal<v8::Value> value = v8::MaybeLocal<v8::Value> value =
global->Get(worker_context, gin::StringToV8(isolate, key.c_str())); global->Get(worker_context, gin::StringToV8(isolate, key));
if (!value.IsEmpty()) { if (!value.IsEmpty()) {
std::string blink_key = "blink" + key; std::string blink_key = base::StrCat({"blink", key});
global global
->Set(worker_context, gin::StringToV8(isolate, blink_key.c_str()), ->Set(worker_context, gin::StringToV8(isolate, blink_key),
value.ToLocalChecked()) value.ToLocalChecked())
.Check(); .Check();
} }