perf: avoid redundant Promise.GetContext calls (#43618)

refactor: avoid redundant Promise.GetContext calls

Several Promise methods call `GetContext()` multiple times. From looking
at the assembly in obj/electron/electron_lib/promise.o, these redundant
calls are actually being made -- they aren't optmized out.

This PR keeps the return value in a local variable to avoid extra calls.

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] 2024-09-08 10:37:26 -05:00 коммит произвёл GitHub
Родитель 00c0948e7b
Коммит a3a595383d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 23 добавлений и 18 удалений

Просмотреть файл

@ -29,35 +29,38 @@ PromiseBase& PromiseBase::operator=(PromiseBase&&) = default;
v8::Maybe<bool> PromiseBase::Reject() { v8::Maybe<bool> PromiseBase::Reject() {
v8::HandleScope handle_scope(isolate()); v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = GetContext();
gin_helper::MicrotasksScope microtasks_scope{ gin_helper::MicrotasksScope microtasks_scope{
isolate(), GetContext()->GetMicrotaskQueue(), false, isolate(), context->GetMicrotaskQueue(), false,
v8::MicrotasksScope::kRunMicrotasks}; v8::MicrotasksScope::kRunMicrotasks};
v8::Context::Scope context_scope(GetContext()); v8::Context::Scope context_scope(context);
return GetInner()->Reject(GetContext(), v8::Undefined(isolate())); return GetInner()->Reject(context, v8::Undefined(isolate()));
} }
v8::Maybe<bool> PromiseBase::Reject(v8::Local<v8::Value> except) { v8::Maybe<bool> PromiseBase::Reject(v8::Local<v8::Value> except) {
v8::HandleScope handle_scope(isolate()); v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = GetContext();
gin_helper::MicrotasksScope microtasks_scope{ gin_helper::MicrotasksScope microtasks_scope{
isolate(), GetContext()->GetMicrotaskQueue(), false, isolate(), context->GetMicrotaskQueue(), false,
v8::MicrotasksScope::kRunMicrotasks}; v8::MicrotasksScope::kRunMicrotasks};
v8::Context::Scope context_scope(GetContext()); v8::Context::Scope context_scope(context);
return GetInner()->Reject(GetContext(), except); return GetInner()->Reject(context, except);
} }
v8::Maybe<bool> PromiseBase::RejectWithErrorMessage( v8::Maybe<bool> PromiseBase::RejectWithErrorMessage(
const std::string_view message) { const std::string_view message) {
v8::HandleScope handle_scope(isolate()); v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = GetContext();
gin_helper::MicrotasksScope microtasks_scope{ gin_helper::MicrotasksScope microtasks_scope{
isolate(), GetContext()->GetMicrotaskQueue(), false, isolate(), context->GetMicrotaskQueue(), false,
v8::MicrotasksScope::kRunMicrotasks}; v8::MicrotasksScope::kRunMicrotasks};
v8::Context::Scope context_scope(GetContext()); v8::Context::Scope context_scope(context);
v8::Local<v8::Value> error = v8::Local<v8::Value> error =
v8::Exception::Error(gin::StringToV8(isolate(), message)); v8::Exception::Error(gin::StringToV8(isolate(), message));
return GetInner()->Reject(GetContext(), (error)); return GetInner()->Reject(context, (error));
} }
v8::Local<v8::Context> PromiseBase::GetContext() const { v8::Local<v8::Context> PromiseBase::GetContext() const {
@ -94,12 +97,13 @@ v8::Local<v8::Promise> Promise<void>::ResolvedPromise(v8::Isolate* isolate) {
v8::Maybe<bool> Promise<void>::Resolve() { v8::Maybe<bool> Promise<void>::Resolve() {
v8::HandleScope handle_scope(isolate()); v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = GetContext();
gin_helper::MicrotasksScope microtasks_scope{ gin_helper::MicrotasksScope microtasks_scope{
isolate(), GetContext()->GetMicrotaskQueue(), false, isolate(), context->GetMicrotaskQueue(), false,
v8::MicrotasksScope::kRunMicrotasks}; v8::MicrotasksScope::kRunMicrotasks};
v8::Context::Scope context_scope(GetContext()); v8::Context::Scope context_scope(context);
return GetInner()->Resolve(GetContext(), v8::Undefined(isolate())); return GetInner()->Resolve(context, v8::Undefined(isolate()));
} }
} // namespace gin_helper } // namespace gin_helper

Просмотреть файл

@ -122,13 +122,13 @@ class Promise : public PromiseBase {
v8::Maybe<bool> Resolve(const RT& value) { v8::Maybe<bool> Resolve(const RT& value) {
gin_helper::Locker locker(isolate()); gin_helper::Locker locker(isolate());
v8::HandleScope handle_scope(isolate()); v8::HandleScope handle_scope(isolate());
v8::Local<v8::Context> context = GetContext();
gin_helper::MicrotasksScope microtasks_scope{ gin_helper::MicrotasksScope microtasks_scope{
isolate(), GetContext()->GetMicrotaskQueue(), false, isolate(), context->GetMicrotaskQueue(), false,
v8::MicrotasksScope::kRunMicrotasks}; v8::MicrotasksScope::kRunMicrotasks};
v8::Context::Scope context_scope(GetContext()); v8::Context::Scope context_scope(context);
return GetInner()->Resolve(GetContext(), return GetInner()->Resolve(context, gin::ConvertToV8(isolate(), value));
gin::ConvertToV8(isolate(), value));
} }
template <typename... ResolveType> template <typename... ResolveType>
@ -143,12 +143,13 @@ class Promise : public PromiseBase {
"promises resolve type"); "promises resolve type");
gin_helper::Locker locker(isolate()); gin_helper::Locker locker(isolate());
v8::HandleScope handle_scope(isolate()); v8::HandleScope handle_scope(isolate());
v8::Context::Scope context_scope(GetContext()); v8::Local<v8::Context> context = GetContext();
v8::Context::Scope context_scope(context);
v8::Local<v8::Value> value = gin::ConvertToV8(isolate(), std::move(cb)); v8::Local<v8::Value> value = gin::ConvertToV8(isolate(), std::move(cb));
v8::Local<v8::Function> handler = value.As<v8::Function>(); v8::Local<v8::Function> handler = value.As<v8::Function>();
return GetHandle()->Then(GetContext(), handler); return GetHandle()->Then(context, handler);
} }
}; };