From a3a595383dd5560a1fce0e811df7c3233c5a039c Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Sun, 8 Sep 2024 10:37:26 -0500 Subject: [PATCH] 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 --- shell/common/gin_helper/promise.cc | 28 ++++++++++++++++------------ shell/common/gin_helper/promise.h | 13 +++++++------ 2 files changed, 23 insertions(+), 18 deletions(-) diff --git a/shell/common/gin_helper/promise.cc b/shell/common/gin_helper/promise.cc index 57e67d6d2e7..5131d1bda58 100644 --- a/shell/common/gin_helper/promise.cc +++ b/shell/common/gin_helper/promise.cc @@ -29,35 +29,38 @@ PromiseBase& PromiseBase::operator=(PromiseBase&&) = default; v8::Maybe PromiseBase::Reject() { v8::HandleScope handle_scope(isolate()); + v8::Local context = GetContext(); gin_helper::MicrotasksScope microtasks_scope{ - isolate(), GetContext()->GetMicrotaskQueue(), false, + isolate(), context->GetMicrotaskQueue(), false, 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 PromiseBase::Reject(v8::Local except) { v8::HandleScope handle_scope(isolate()); + v8::Local context = GetContext(); gin_helper::MicrotasksScope microtasks_scope{ - isolate(), GetContext()->GetMicrotaskQueue(), false, + isolate(), context->GetMicrotaskQueue(), false, 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 PromiseBase::RejectWithErrorMessage( const std::string_view message) { v8::HandleScope handle_scope(isolate()); + v8::Local context = GetContext(); gin_helper::MicrotasksScope microtasks_scope{ - isolate(), GetContext()->GetMicrotaskQueue(), false, + isolate(), context->GetMicrotaskQueue(), false, v8::MicrotasksScope::kRunMicrotasks}; - v8::Context::Scope context_scope(GetContext()); + v8::Context::Scope context_scope(context); v8::Local error = v8::Exception::Error(gin::StringToV8(isolate(), message)); - return GetInner()->Reject(GetContext(), (error)); + return GetInner()->Reject(context, (error)); } v8::Local PromiseBase::GetContext() const { @@ -94,12 +97,13 @@ v8::Local Promise::ResolvedPromise(v8::Isolate* isolate) { v8::Maybe Promise::Resolve() { v8::HandleScope handle_scope(isolate()); + v8::Local context = GetContext(); gin_helper::MicrotasksScope microtasks_scope{ - isolate(), GetContext()->GetMicrotaskQueue(), false, + isolate(), context->GetMicrotaskQueue(), false, 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 diff --git a/shell/common/gin_helper/promise.h b/shell/common/gin_helper/promise.h index ce6e44f9ee8..cc1ae49010f 100644 --- a/shell/common/gin_helper/promise.h +++ b/shell/common/gin_helper/promise.h @@ -122,13 +122,13 @@ class Promise : public PromiseBase { v8::Maybe Resolve(const RT& value) { gin_helper::Locker locker(isolate()); v8::HandleScope handle_scope(isolate()); + v8::Local context = GetContext(); gin_helper::MicrotasksScope microtasks_scope{ - isolate(), GetContext()->GetMicrotaskQueue(), false, + isolate(), context->GetMicrotaskQueue(), false, v8::MicrotasksScope::kRunMicrotasks}; - v8::Context::Scope context_scope(GetContext()); + v8::Context::Scope context_scope(context); - return GetInner()->Resolve(GetContext(), - gin::ConvertToV8(isolate(), value)); + return GetInner()->Resolve(context, gin::ConvertToV8(isolate(), value)); } template @@ -143,12 +143,13 @@ class Promise : public PromiseBase { "promises resolve type"); gin_helper::Locker locker(isolate()); v8::HandleScope handle_scope(isolate()); - v8::Context::Scope context_scope(GetContext()); + v8::Local context = GetContext(); + v8::Context::Scope context_scope(context); v8::Local value = gin::ConvertToV8(isolate(), std::move(cb)); v8::Local handler = value.As(); - return GetHandle()->Then(GetContext(), handler); + return GetHandle()->Then(context, handler); } };