perf: prefer NewFromUtf8Literal() over NewFromUtf8() for string literals (#44427)
* perf: prefer NewFromUtf8Literal() over NewFromUtf8() for string literals the string length is known at compile time and no need to call ToLocalChecked() Co-authored-by: Charles Kerr <charles@charleskerr.com> * perf: string length is known when calling NewFromUtf8(), so use it Co-authored-by: Charles Kerr <charles@charleskerr.com> * perf: remove unnecessary calls to c_str() these just force the code being called to have to recalculate the string length Co-authored-by: Charles Kerr <charles@charleskerr.com> --------- 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:
parent
2138b5a3e0
commit
1a2015c87d
4 changed files with 22 additions and 24 deletions
|
@ -18,8 +18,7 @@ class Archive : public node::ObjectWrap {
|
|||
static v8::Local<v8::FunctionTemplate> CreateFunctionTemplate(
|
||||
v8::Isolate* isolate) {
|
||||
auto tpl = v8::FunctionTemplate::New(isolate, Archive::New);
|
||||
tpl->SetClassName(
|
||||
v8::String::NewFromUtf8(isolate, "Archive").ToLocalChecked());
|
||||
tpl->SetClassName(v8::String::NewFromUtf8Literal(isolate, "Archive"));
|
||||
tpl->InstanceTemplate()->SetInternalFieldCount(1);
|
||||
|
||||
NODE_SET_PROTOTYPE_METHOD(tpl, "getFileInfo", &Archive::GetFileInfo);
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
@ -121,11 +121,11 @@ void ElectronRendererClient::DidCreateScriptContext(
|
|||
"Headers"};
|
||||
for (const auto& key : keys) {
|
||||
v8::MaybeLocal<v8::Value> value =
|
||||
global->Get(renderer_context, gin::StringToV8(isolate, key.c_str()));
|
||||
global->Get(renderer_context, gin::StringToV8(isolate, key));
|
||||
if (!value.IsEmpty()) {
|
||||
std::string blink_key = "blink" + key;
|
||||
global
|
||||
->Set(renderer_context, gin::StringToV8(isolate, blink_key.c_str()),
|
||||
->Set(renderer_context, gin::StringToV8(isolate, blink_key),
|
||||
value.ToLocalChecked())
|
||||
.Check();
|
||||
}
|
||||
|
|
|
@ -60,7 +60,7 @@ v8::Local<v8::Value> GetBinding(v8::Isolate* isolate,
|
|||
std::string binding_key = gin::V8ToString(isolate, key);
|
||||
gin_helper::Dictionary cache(isolate, GetBindingCache(isolate));
|
||||
|
||||
if (cache.Get(binding_key.c_str(), &exports)) {
|
||||
if (cache.Get(binding_key, &exports)) {
|
||||
return exports;
|
||||
}
|
||||
|
||||
|
@ -79,7 +79,7 @@ v8::Local<v8::Value> GetBinding(v8::Isolate* isolate,
|
|||
DCHECK_NE(mod->nm_context_register_func, nullptr);
|
||||
mod->nm_context_register_func(exports, v8::Null(isolate),
|
||||
isolate->GetCurrentContext(), mod->nm_priv);
|
||||
cache.Set(binding_key.c_str(), exports);
|
||||
cache.Set(binding_key, exports);
|
||||
return exports;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue