From d122cd6e09e420ff56b2e8bbc5ae31445ae6a630 Mon Sep 17 00:00:00 2001 From: "trop[bot]" <37223003+trop[bot]@users.noreply.github.com> Date: Mon, 6 Oct 2025 12:10:08 -0500 Subject: [PATCH] perf: avoid a little extra work in `InvokeIpcCallback()` (#48466) perf: two minor perf refactors in InvokeIpcCallback() 1. Allocate the CallbackScope on the stack instead of the heap 2. Skip a redundant call to node::Environment::GetCurrent() Co-authored-by: trop[bot] <37223003+trop[bot]@users.noreply.github.com> Co-authored-by: Charles Kerr --- shell/renderer/electron_ipc_native.cc | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/shell/renderer/electron_ipc_native.cc b/shell/renderer/electron_ipc_native.cc index 36396ad2ff48..7fac851f9e2e 100644 --- a/shell/renderer/electron_ipc_native.cc +++ b/shell/renderer/electron_ipc_native.cc @@ -4,6 +4,8 @@ #include "electron/shell/renderer/electron_ipc_native.h" +#include + #include "base/trace_event/trace_event.h" #include "shell/common/gin_converters/blink_converter.h" #include "shell/common/gin_converters/value_converter.h" @@ -45,10 +47,9 @@ void InvokeIpcCallback(v8::Isolate* const isolate, // Only set up the node::CallbackScope if there's a node environment. // Sandboxed renderers don't have a node environment. - std::unique_ptr callback_scope; - if (node::Environment::GetCurrent(context)) { - callback_scope = std::make_unique( - isolate, ipcNative, node::async_context{0, 0}); + std::optional callback_scope; + if (auto* env = node::Environment::GetCurrent(context)) { + callback_scope.emplace(env, ipcNative, node::async_context{0, 0}); } auto callback_key = gin::ConvertToV8(isolate, callback_name)