perf: prefer NewFromUtf8Literal() over NewFromUtf8() for string literals (#44412)

* perf: prefer NewFromUtf8Literal() over NewFromUtf8() for string literals

the string length is known at compile time and no need to call ToLocalChecked()

* perf: string length is known when calling NewFromUtf8(), so use it

* perf: remove unnecessary calls to c_str()

these just force the code being called to have to recalculate the string length
This commit is contained in:
Charles Kerr 2024-10-28 08:12:32 -05:00 committed by GitHub
parent 348801b20e
commit 5d0d15a0b3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 22 additions and 24 deletions

View file

@ -146,7 +146,7 @@ class ScriptExecutionCallback {
const v8::Local<v8::Object>& result) {
v8::MaybeLocal<v8::Value> maybe_result;
bool success = true;
std::string error_message =
std::string errmsg =
"An unknown exception occurred while getting the result of the script";
{
v8::TryCatch try_catch(isolate);
@ -164,7 +164,7 @@ class ScriptExecutionCallback {
auto message = try_catch.Message();
if (!message.IsEmpty()) {
gin::ConvertFromV8(isolate, message->Get(), &error_message);
gin::ConvertFromV8(isolate, message->Get(), &errmsg);
}
}
}
@ -173,10 +173,11 @@ class ScriptExecutionCallback {
if (callback_)
std::move(callback_).Run(
v8::Undefined(isolate),
v8::Exception::Error(
v8::String::NewFromUtf8(isolate, error_message.c_str())
.ToLocalChecked()));
promise_.RejectWithErrorMessage(error_message);
v8::Exception::Error(v8::String::NewFromUtf8(
isolate, errmsg.data(),
v8::NewStringType::kNormal, errmsg.size())
.ToLocalChecked()));
promise_.RejectWithErrorMessage(errmsg);
} else {
v8::Local<v8::Context> context = promise_.GetContext();
v8::Context::Scope context_scope(context);
@ -210,7 +211,7 @@ class ScriptExecutionCallback {
promise_.Resolve(value);
}
} else {
const char error_message[] =
const char errmsg[] =
"Script failed to execute, this normally means an error "
"was thrown. Check the renderer console for the error.";
if (!callback_.is_null()) {
@ -219,13 +220,12 @@ class ScriptExecutionCallback {
std::move(callback_).Run(
v8::Undefined(isolate),
v8::Exception::Error(
v8::String::NewFromUtf8(isolate, error_message)
.ToLocalChecked()));
v8::String::NewFromUtf8Literal(isolate, errmsg)));
}
promise_.RejectWithErrorMessage(error_message);
promise_.RejectWithErrorMessage(errmsg);
}
} else {
const char error_message[] =
const char errmsg[] =
"WebFrame was removed before script could run. This normally means "
"the underlying frame was destroyed";
if (!callback_.is_null()) {
@ -233,10 +233,10 @@ class ScriptExecutionCallback {
v8::Context::Scope context_scope(context);
std::move(callback_).Run(
v8::Undefined(isolate),
v8::Exception::Error(v8::String::NewFromUtf8(isolate, error_message)
.ToLocalChecked()));
v8::Exception::Error(
v8::String::NewFromUtf8Literal(isolate, errmsg)));
}
promise_.RejectWithErrorMessage(error_message);
promise_.RejectWithErrorMessage(errmsg);
}
delete this;
}
@ -716,15 +716,14 @@ class WebFrameRenderer final : public gin::Wrappable<WebFrameRenderer>,
script.Get("url", &url);
if (!script.Get("code", &code)) {
const char error_message[] = "Invalid 'code'";
const char errmsg[] = "Invalid 'code'";
if (!completion_callback.is_null()) {
std::move(completion_callback)
.Run(v8::Undefined(isolate),
v8::Exception::Error(
v8::String::NewFromUtf8(isolate, error_message)
.ToLocalChecked()));
v8::String::NewFromUtf8Literal(isolate, errmsg)));
}
promise.RejectWithErrorMessage(error_message);
promise.RejectWithErrorMessage(errmsg);
return handle;
}