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<Local>)
This commit is contained in:
Charles Kerr 2024-09-18 09:40:14 -05:00 коммит произвёл GitHub
Родитель 02fd8bbcc1
Коммит d897359b82
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: B5690EEEBB952194
2 изменённых файлов: 13 добавлений и 26 удалений

Просмотреть файл

@ -9,10 +9,11 @@
namespace gin_helper::internal {
v8::Local<v8::Value> CallMethodWithArgs(v8::Isolate* isolate,
v8::Local<v8::Object> obj,
const char* method,
ValueVector* args) {
v8::Local<v8::Value> CallMethodWithArgs(
v8::Isolate* isolate,
v8::Local<v8::Object> obj,
const char* method,
const base::span<v8::Local<v8::Value>> args) {
v8::EscapableHandleScope handle_scope{isolate};
// CallbackScope and MakeCallback both require an active node::Environment
@ -29,7 +30,7 @@ v8::Local<v8::Value> CallMethodWithArgs(v8::Isolate* isolate,
// node::MakeCallback will also run pending tasks in Node.js.
v8::MaybeLocal<v8::Value> 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

Просмотреть файл

@ -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 <array>
#include <utility>
#include <vector>
#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<v8::Value>>;
v8::Local<v8::Value> CallMethodWithArgs(v8::Isolate* isolate,
v8::Local<v8::Object> obj,
const char* method,
ValueVector* args);
base::span<v8::Local<v8::Value>> args);
} // namespace internal
// obj.emit.apply(obj, name, args...);
// The caller is responsible of allocating a HandleScope.
template <typename StringType>
v8::Local<v8::Value> EmitEvent(v8::Isolate* isolate,
v8::Local<v8::Object> 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 <typename StringType, typename... Args>
@ -45,12 +31,12 @@ v8::Local<v8::Value> EmitEvent(v8::Isolate* isolate,
const StringType& name,
Args&&... args) {
v8::EscapableHandleScope scope{isolate};
internal::ValueVector converted_args = {
std::array<v8::Local<v8::Value>, 1U + sizeof...(args)> converted_args = {
gin::StringToV8(isolate, name),
gin::ConvertToV8(isolate, std::forward<Args>(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<v8::Value> CustomEmit(v8::Isolate* isolate,
const char* custom_emit,
Args&&... args) {
v8::EscapableHandleScope scope{isolate};
internal::ValueVector converted_args = {
std::array<v8::Local<v8::Value>, sizeof...(args)> converted_args = {
gin::ConvertToV8(isolate, std::forward<Args>(args))...,
};
return scope.Escape(internal::CallMethodWithArgs(isolate, object, custom_emit,
&converted_args));
converted_args));
}
template <typename T, typename... Args>