Repository: electron/electron
Pull request: perf: avoid redundant Promise.GetContext calls

Backport of #43609

See that PR for details.


Notes: none.

Changed files:
filename: shell/common/gin_helper/promise.cc
patch: @@ -29,35 +29,38 @@ PromiseBase& PromiseBase::operator=(PromiseBase&&) = default;
 
 v8::Maybe<bool> PromiseBase::Reject() {
   v8::HandleScope handle_scope(isolate());
+  v8::Local<v8::Context> 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<bool> PromiseBase::Reject(v8::Local<v8::Value> except) {
   v8::HandleScope handle_scope(isolate());
+  v8::Local<v8::Context> 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<bool> PromiseBase::RejectWithErrorMessage(
     const std::string_view message) {
   v8::HandleScope handle_scope(isolate());
+  v8::Local<v8::Context> 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<v8::Value> error =
       v8::Exception::Error(gin::StringToV8(isolate(), message));
-  return GetInner()->Reject(GetContext(), (error));
+  return GetInner()->Reject(context, (error));
 }
 
 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::HandleScope handle_scope(isolate());
+  v8::Local<v8::Context> 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

filename: shell/common/gin_helper/promise.h
patch: @@ -122,13 +122,13 @@ class Promise : public PromiseBase {
   v8::Maybe<bool> Resolve(const RT& value) {
     gin_helper::Locker locker(isolate());
     v8::HandleScope handle_scope(isolate());
+    v8::Local<v8::Context> 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 <typename... ResolveType>
@@ -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<v8::Context> context = GetContext();
+    v8::Context::Scope context_scope(context);
 
     v8::Local<v8::Value> value = gin::ConvertToV8(isolate(), std::move(cb));
     v8::Local<v8::Function> handler = value.As<v8::Function>();
 
-    return GetHandle()->Then(GetContext(), handler);
+    return GetHandle()->Then(context, handler);
   }
 };