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

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:34 -05:00 committed by GitHub
parent 39258d20d4
commit 15aeb87370
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 23 additions and 18 deletions

View file

@ -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);
}
};