From d897359b82601201dbc2aafbbd07cf92438df8f9 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Wed, 18 Sep 2024 09:40:14 -0500 Subject: [PATCH] perf: hold `Emit()` arg arrays in a `std::array` (#43750) * refactor: CallMethodWithArgs() now takes a span of value handles * perf: use std::array instead of std::vector to hold Emit arg parameter packs * chore: remove unused gin_helper::EmitEvent(iso, obj, name, span) --- .../common/gin_helper/event_emitter_caller.cc | 11 ++++---- .../common/gin_helper/event_emitter_caller.h | 28 +++++-------------- 2 files changed, 13 insertions(+), 26 deletions(-) diff --git a/shell/common/gin_helper/event_emitter_caller.cc b/shell/common/gin_helper/event_emitter_caller.cc index 0c7b055194..2f4c50056e 100644 --- a/shell/common/gin_helper/event_emitter_caller.cc +++ b/shell/common/gin_helper/event_emitter_caller.cc @@ -9,10 +9,11 @@ namespace gin_helper::internal { -v8::Local CallMethodWithArgs(v8::Isolate* isolate, - v8::Local obj, - const char* method, - ValueVector* args) { +v8::Local CallMethodWithArgs( + v8::Isolate* isolate, + v8::Local obj, + const char* method, + const base::span> args) { v8::EscapableHandleScope handle_scope{isolate}; // CallbackScope and MakeCallback both require an active node::Environment @@ -29,7 +30,7 @@ v8::Local CallMethodWithArgs(v8::Isolate* isolate, // node::MakeCallback will also run pending tasks in Node.js. v8::MaybeLocal ret = node::MakeCallback( - isolate, obj, method, args->size(), args->data(), {0, 0}); + isolate, obj, method, args.size(), args.data(), {0, 0}); // If the JS function throws an exception (doesn't return a value) the result // of MakeCallback will be empty and therefore ToLocal will be false, in this diff --git a/shell/common/gin_helper/event_emitter_caller.h b/shell/common/gin_helper/event_emitter_caller.h index 1dc4a2701f..abb73758cf 100644 --- a/shell/common/gin_helper/event_emitter_caller.h +++ b/shell/common/gin_helper/event_emitter_caller.h @@ -5,9 +5,10 @@ #ifndef ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_ #define ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_CALLER_H_ +#include #include -#include +#include "base/containers/span.h" #include "gin/converter.h" #include "gin/wrappable.h" @@ -15,28 +16,13 @@ namespace gin_helper { namespace internal { -using ValueVector = std::vector>; - v8::Local CallMethodWithArgs(v8::Isolate* isolate, v8::Local obj, const char* method, - ValueVector* args); + base::span> args); } // namespace internal -// obj.emit.apply(obj, name, args...); -// The caller is responsible of allocating a HandleScope. -template -v8::Local EmitEvent(v8::Isolate* isolate, - v8::Local obj, - const StringType& name, - const internal::ValueVector& args) { - internal::ValueVector concatenated_args = {gin::StringToV8(isolate, name)}; - concatenated_args.reserve(1 + args.size()); - concatenated_args.insert(concatenated_args.end(), args.begin(), args.end()); - return internal::CallMethodWithArgs(isolate, obj, "emit", &concatenated_args); -} - // obj.emit(name, args...); // The caller is responsible of allocating a HandleScope. template @@ -45,12 +31,12 @@ v8::Local EmitEvent(v8::Isolate* isolate, const StringType& name, Args&&... args) { v8::EscapableHandleScope scope{isolate}; - internal::ValueVector converted_args = { + std::array, 1U + sizeof...(args)> converted_args = { gin::StringToV8(isolate, name), gin::ConvertToV8(isolate, std::forward(args))..., }; return scope.Escape( - internal::CallMethodWithArgs(isolate, obj, "emit", &converted_args)); + internal::CallMethodWithArgs(isolate, obj, "emit", converted_args)); } // obj.custom_emit(args...) @@ -60,11 +46,11 @@ v8::Local CustomEmit(v8::Isolate* isolate, const char* custom_emit, Args&&... args) { v8::EscapableHandleScope scope{isolate}; - internal::ValueVector converted_args = { + std::array, sizeof...(args)> converted_args = { gin::ConvertToV8(isolate, std::forward(args))..., }; return scope.Escape(internal::CallMethodWithArgs(isolate, object, custom_emit, - &converted_args)); + converted_args)); } template