Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
src: simplify adding fast APIs to ExternalReferenceRegistry
PR-URL: #58896
Backport-PR-URL: #59065
Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com>
Reviewed-By: Tobias Nießen <tniessen@tnie.de>
Reviewed-By: Chengzhong Wu <legendecas@gmail.com>
Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com>
  • Loading branch information
Renegade334 authored and aduh95 committed Jul 28, 2025
commit 690525881e8d257b149dc84a1c0ef0b862457da3
13 changes: 8 additions & 5 deletions src/crypto/crypto_timing.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

namespace node {

using v8::CFunction;
using v8::FastApiCallbackOptions;
using v8::FastApiTypedArray;
using v8::FunctionCallbackInfo;
Expand Down Expand Up @@ -67,16 +68,18 @@ bool FastTimingSafeEqual(Local<Value> receiver,
return CRYPTO_memcmp(data_a, data_b, a.length()) == 0;
}

static v8::CFunction fast_equal(v8::CFunction::Make(FastTimingSafeEqual));
static CFunction fast_timing_safe_equal(CFunction::Make(FastTimingSafeEqual));

void Initialize(Environment* env, Local<Object> target) {
SetFastMethodNoSideEffect(
env->context(), target, "timingSafeEqual", TimingSafeEqual, &fast_equal);
SetFastMethodNoSideEffect(env->context(),
target,
"timingSafeEqual",
TimingSafeEqual,
&fast_timing_safe_equal);
}
void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(TimingSafeEqual);
registry->Register(FastTimingSafeEqual);
registry->Register(fast_equal.GetTypeInfo());
registry->Register(fast_timing_safe_equal);
}
} // namespace Timing

Expand Down
36 changes: 12 additions & 24 deletions src/histogram.cc
Original file line number Diff line number Diff line change
Expand Up @@ -116,22 +116,14 @@ void HistogramImpl::RegisterExternalReferences(
registry->Register(GetPercentiles);
registry->Register(GetPercentilesBigInt);
registry->Register(DoReset);
registry->Register(fast_reset_.GetTypeInfo());
registry->Register(fast_get_count_.GetTypeInfo());
registry->Register(fast_get_min_.GetTypeInfo());
registry->Register(fast_get_max_.GetTypeInfo());
registry->Register(fast_get_mean_.GetTypeInfo());
registry->Register(fast_get_exceeds_.GetTypeInfo());
registry->Register(fast_get_stddev_.GetTypeInfo());
registry->Register(fast_get_percentile_.GetTypeInfo());
registry->Register(FastReset);
registry->Register(FastGetCount);
registry->Register(FastGetMin);
registry->Register(FastGetMax);
registry->Register(FastGetMean);
registry->Register(FastGetExceeds);
registry->Register(FastGetStddev);
registry->Register(FastGetPercentile);
registry->Register(fast_reset_);
registry->Register(fast_get_count_);
registry->Register(fast_get_min_);
registry->Register(fast_get_max_);
registry->Register(fast_get_mean_);
registry->Register(fast_get_exceeds_);
registry->Register(fast_get_stddev_);
registry->Register(fast_get_percentile_);
is_registered = true;
}

Expand Down Expand Up @@ -300,10 +292,8 @@ void HistogramBase::RegisterExternalReferences(
registry->Register(Add);
registry->Register(Record);
registry->Register(RecordDelta);
registry->Register(fast_record_.GetTypeInfo());
registry->Register(fast_record_delta_.GetTypeInfo());
registry->Register(FastRecord);
registry->Register(FastRecordDelta);
registry->Register(fast_record_);
registry->Register(fast_record_delta_);
HistogramImpl::RegisterExternalReferences(registry);
}

Expand Down Expand Up @@ -354,10 +344,8 @@ void IntervalHistogram::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(Start);
registry->Register(Stop);
registry->Register(fast_start_.GetTypeInfo());
registry->Register(fast_stop_.GetTypeInfo());
registry->Register(FastStart);
registry->Register(FastStop);
registry->Register(fast_start_);
registry->Register(fast_stop_);
HistogramImpl::RegisterExternalReferences(registry);
}

Expand Down
21 changes: 7 additions & 14 deletions src/node_buffer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1608,20 +1608,16 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(SetBufferPrototype);

registry->Register(SlowByteLengthUtf8);
registry->Register(fast_byte_length_utf8.GetTypeInfo());
registry->Register(FastByteLengthUtf8);
registry->Register(fast_byte_length_utf8);
registry->Register(SlowCopy);
registry->Register(fast_copy.GetTypeInfo());
registry->Register(FastCopy);
registry->Register(fast_copy);
registry->Register(Compare);
registry->Register(FastCompare);
registry->Register(fast_compare.GetTypeInfo());
registry->Register(fast_compare);
registry->Register(CompareOffset);
registry->Register(Fill);
registry->Register(IndexOfBuffer);
registry->Register(SlowIndexOfNumber);
registry->Register(FastIndexOfNumber);
registry->Register(fast_index_of_number.GetTypeInfo());
registry->Register(fast_index_of_number);
registry->Register(IndexOfString);

registry->Register(Swap16);
Expand All @@ -1642,12 +1638,9 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(SlowWriteString<ASCII>);
registry->Register(SlowWriteString<LATIN1>);
registry->Register(SlowWriteString<UTF8>);
registry->Register(FastWriteString<ASCII>);
registry->Register(fast_write_string_ascii.GetTypeInfo());
registry->Register(FastWriteString<LATIN1>);
registry->Register(fast_write_string_latin1.GetTypeInfo());
registry->Register(FastWriteString<UTF8>);
registry->Register(fast_write_string_utf8.GetTypeInfo());
registry->Register(fast_write_string_ascii);
registry->Register(fast_write_string_latin1);
registry->Register(fast_write_string_utf8);
registry->Register(StringWrite<ASCII>);
registry->Register(StringWrite<BASE64>);
registry->Register(StringWrite<BASE64URL>);
Expand Down
104 changes: 7 additions & 97 deletions src/node_external_reference.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,110 +10,13 @@

namespace node {

using CFunctionCallbackWithOneByteString =
uint32_t (*)(v8::Local<v8::Value>, const v8::FastOneByteString&);

using CFunctionCallbackReturnBool = bool (*)(v8::Local<v8::Value> unused,
v8::Local<v8::Value> receiver);
using CFunctionCallback = void (*)(v8::Local<v8::Value> unused,
v8::Local<v8::Value> receiver);
using CFunctionCallbackReturnDouble =
double (*)(v8::Local<v8::Object> unused, v8::Local<v8::Object> receiver);
using CFunctionCallbackReturnInt32 =
int32_t (*)(v8::Local<v8::Value> receiver,
v8::Local<v8::Value> input,
// NOLINTNEXTLINE(runtime/references) This is V8 api.
v8::FastApiCallbackOptions& options);
using CFunctionCallbackValueReturnDouble =
double (*)(v8::Local<v8::Value> receiver);
using CFunctionCallbackValueReturnDoubleUnusedReceiver =
double (*)(v8::Local<v8::Value> unused, v8::Local<v8::Value> receiver);
using CFunctionCallbackWithInt64 = void (*)(v8::Local<v8::Object> unused,
v8::Local<v8::Object> receiver,
int64_t);
using CFunctionCallbackWithBool = void (*)(v8::Local<v8::Object> unused,
v8::Local<v8::Object> receiver,
bool);
using CFunctionCallbackWithString =
bool (*)(v8::Local<v8::Value>, const v8::FastOneByteString& input);
using CFunctionCallbackWithStrings =
bool (*)(v8::Local<v8::Value>,
const v8::FastOneByteString& input,
const v8::FastOneByteString& base);
using CFunctionCallbackWithTwoUint8Arrays =
int32_t (*)(v8::Local<v8::Value>,
const v8::FastApiTypedArray<uint8_t>&,
const v8::FastApiTypedArray<uint8_t>&);
using CFunctionCallbackWithTwoUint8ArraysFallback =
bool (*)(v8::Local<v8::Value>,
const v8::FastApiTypedArray<uint8_t>&,
const v8::FastApiTypedArray<uint8_t>&,
v8::FastApiCallbackOptions&);
using CFunctionCallbackWithUint8ArrayUint32Int64Bool =
int32_t (*)(v8::Local<v8::Value>,
const v8::FastApiTypedArray<uint8_t>&,
uint32_t,
int64_t,
bool);
using CFunctionWithUint32 = uint32_t (*)(v8::Local<v8::Value>,
const uint32_t input);
using CFunctionWithReturnUint32 = uint32_t (*)(v8::Local<v8::Value>);
using CFunctionWithReturnDouble = double (*)(v8::Local<v8::Value>);
using CFunctionWithDoubleReturnDouble = double (*)(v8::Local<v8::Value>,
v8::Local<v8::Value>,
const double);
using CFunctionWithInt64Fallback = void (*)(v8::Local<v8::Value>,
v8::Local<v8::Value>,
const int64_t,
v8::FastApiCallbackOptions&);
using CFunctionWithBool = void (*)(v8::Local<v8::Value>,
v8::Local<v8::Value>,
bool);

using CFunctionWriteString =
uint32_t (*)(v8::Local<v8::Value> receiver,
const v8::FastApiTypedArray<uint8_t>& dst,
const v8::FastOneByteString& src,
uint32_t offset,
uint32_t max_length);

using CFunctionBufferCopy =
uint32_t (*)(v8::Local<v8::Value> receiver,
const v8::FastApiTypedArray<uint8_t>& source,
const v8::FastApiTypedArray<uint8_t>& target,
uint32_t target_start,
uint32_t source_start,
uint32_t to_copy);

// This class manages the external references from the V8 heap
// to the C++ addresses in Node.js.
class ExternalReferenceRegistry {
public:
ExternalReferenceRegistry();

#define ALLOWED_EXTERNAL_REFERENCE_TYPES(V) \
V(CFunctionCallback) \
V(CFunctionCallbackWithOneByteString) \
V(CFunctionCallbackReturnBool) \
V(CFunctionCallbackReturnDouble) \
V(CFunctionCallbackReturnInt32) \
V(CFunctionWithReturnUint32) \
V(CFunctionCallbackValueReturnDouble) \
V(CFunctionCallbackValueReturnDoubleUnusedReceiver) \
V(CFunctionCallbackWithInt64) \
V(CFunctionCallbackWithBool) \
V(CFunctionCallbackWithString) \
V(CFunctionCallbackWithStrings) \
V(CFunctionCallbackWithTwoUint8Arrays) \
V(CFunctionCallbackWithTwoUint8ArraysFallback) \
V(CFunctionCallbackWithUint8ArrayUint32Int64Bool) \
V(CFunctionWithUint32) \
V(CFunctionWithDoubleReturnDouble) \
V(CFunctionWithInt64Fallback) \
V(CFunctionWithBool) \
V(CFunctionBufferCopy) \
V(CFunctionWriteString) \
V(const v8::CFunctionInfo*) \
V(v8::FunctionCallback) \
V(v8::AccessorNameGetterCallback) \
V(v8::AccessorNameSetterCallback) \
Expand All @@ -135,6 +38,13 @@ class ExternalReferenceRegistry {
ALLOWED_EXTERNAL_REFERENCE_TYPES(V)
#undef V

// Registers both the underlying function pointer
// and the corresponding CFunctionInfo.
void Register(const v8::CFunction& c_func) {
RegisterT(c_func.GetAddress());
RegisterT(c_func.GetTypeInfo());
}

// This can be called only once.
const std::vector<intptr_t>& external_references();

Expand Down
9 changes: 3 additions & 6 deletions src/node_os.cc
Original file line number Diff line number Diff line change
Expand Up @@ -467,20 +467,17 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(GetLoadAvg);
registry->Register(GetUptime);
registry->Register(GetTotalMemory);
registry->Register(FastGetTotalMemory);
registry->Register(fast_get_total_memory.GetTypeInfo());
registry->Register(fast_get_total_memory);
registry->Register(GetFreeMemory);
registry->Register(FastGetFreeMemory);
registry->Register(fast_get_free_memory.GetTypeInfo());
registry->Register(fast_get_free_memory);
registry->Register(GetCPUInfo);
registry->Register(GetInterfaceAddresses);
registry->Register(GetHomeDirectory);
registry->Register(GetUserInfo);
registry->Register(SetPriority);
registry->Register(GetPriority);
registry->Register(GetAvailableParallelism);
registry->Register(FastGetAvailableParallelism);
registry->Register(fast_get_available_parallelism.GetTypeInfo());
registry->Register(fast_get_available_parallelism);
registry->Register(GetOSInformation);
}

Expand Down
3 changes: 1 addition & 2 deletions src/node_perf.cc
Original file line number Diff line number Diff line change
Expand Up @@ -410,8 +410,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(MarkBootstrapComplete);
registry->Register(UvMetricsInfo);
registry->Register(SlowPerformanceNow);
registry->Register(FastPerformanceNow);
registry->Register(fast_performance_now.GetTypeInfo());
registry->Register(fast_performance_now);
HistogramBase::RegisterExternalReferences(registry);
IntervalHistogram::RegisterExternalReferences(registry);
}
Expand Down
6 changes: 2 additions & 4 deletions src/node_process_methods.cc
Original file line number Diff line number Diff line change
Expand Up @@ -642,10 +642,8 @@ void BindingData::RegisterExternalReferences(
ExternalReferenceRegistry* registry) {
registry->Register(SlowNumber);
registry->Register(SlowBigInt);
registry->Register(FastNumber);
registry->Register(FastBigInt);
registry->Register(fast_number_.GetTypeInfo());
registry->Register(fast_bigint_.GetTypeInfo());
registry->Register(fast_number_);
registry->Register(fast_bigint_);
}

BindingData* BindingData::FromV8Value(Local<Value> value) {
Expand Down
9 changes: 3 additions & 6 deletions src/node_types.cc
Original file line number Diff line number Diff line change
Expand Up @@ -113,18 +113,15 @@ void InitializeTypes(Local<Object> target,
void RegisterTypesExternalReferences(ExternalReferenceRegistry* registry) {
#define V(type) \
registry->Register(Is##type); \
registry->Register(Is##type##FastApi); \
registry->Register(fast_is_##type##_.GetTypeInfo());
registry->Register(fast_is_##type##_);

VALUE_METHOD_MAP(V)
#undef V

registry->Register(IsAnyArrayBuffer);
registry->Register(IsAnyArrayBufferFastApi);
registry->Register(fast_is_any_array_buffer_.GetTypeInfo());
registry->Register(fast_is_any_array_buffer_);
registry->Register(IsBoxedPrimitive);
registry->Register(IsBoxedPrimitiveFastApi);
registry->Register(fast_is_boxed_primitive_.GetTypeInfo());
registry->Register(fast_is_boxed_primitive_);
}
} // namespace node

Expand Down
5 changes: 1 addition & 4 deletions src/node_url.cc
Original file line number Diff line number Diff line change
Expand Up @@ -516,11 +516,8 @@ void BindingData::RegisterExternalReferences(
registry->Register(PathToFileURL);
registry->Register(Update);
registry->Register(CanParse);
registry->Register(FastCanParse);
registry->Register(FastCanParseWithBase);

for (const CFunction& method : fast_can_parse_methods_) {
registry->Register(method.GetTypeInfo());
registry->Register(method);
}
}

Expand Down
3 changes: 1 addition & 2 deletions src/node_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(Sleep);
registry->Register(ArrayBufferViewHasBuffer);
registry->Register(GuessHandleType);
registry->Register(FastGuessHandleType);
registry->Register(fast_guess_handle_type_.GetTypeInfo());
registry->Register(fast_guess_handle_type_);
registry->Register(ParseEnv);
registry->Register(IsInsideNodeModules);
registry->Register(DefineLazyProperties);
Expand Down
3 changes: 1 addition & 2 deletions src/node_v8.cc
Original file line number Diff line number Diff line change
Expand Up @@ -716,8 +716,7 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(GCProfiler::Stop);
registry->Register(GetCppHeapStatistics);
registry->Register(IsStringOneByteRepresentation);
registry->Register(FastIsStringOneByteRepresentation);
registry->Register(fast_is_string_one_byte_representation_.GetTypeInfo());
registry->Register(fast_is_string_one_byte_representation_);
}

} // namespace v8_utils
Expand Down
16 changes: 6 additions & 10 deletions src/timers.cc
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ void BindingData::Deserialize(Local<Context> context,

v8::CFunction BindingData::fast_get_libuv_now_(
v8::CFunction::Make(FastGetLibuvNow));
v8::CFunction BindingData::fast_schedule_timers_(
v8::CFunction BindingData::fast_schedule_timer_(
v8::CFunction::Make(FastScheduleTimer));
v8::CFunction BindingData::fast_toggle_timer_ref_(
v8::CFunction::Make(FastToggleTimerRef));
Expand All @@ -144,7 +144,7 @@ void BindingData::CreatePerIsolateProperties(IsolateData* isolate_data,
target,
"scheduleTimer",
SlowScheduleTimer,
&fast_schedule_timers_);
&fast_schedule_timer_);
SetFastMethod(isolate,
target,
"toggleTimerRef",
Expand Down Expand Up @@ -185,20 +185,16 @@ void BindingData::RegisterTimerExternalReferences(
registry->Register(SetupTimers);

registry->Register(SlowGetLibuvNow);
registry->Register(FastGetLibuvNow);
registry->Register(fast_get_libuv_now_.GetTypeInfo());
registry->Register(fast_get_libuv_now_);

registry->Register(SlowScheduleTimer);
registry->Register(FastScheduleTimer);
registry->Register(fast_schedule_timers_.GetTypeInfo());
registry->Register(fast_schedule_timer_);

registry->Register(SlowToggleTimerRef);
registry->Register(FastToggleTimerRef);
registry->Register(fast_toggle_timer_ref_.GetTypeInfo());
registry->Register(fast_toggle_timer_ref_);

registry->Register(SlowToggleImmediateRef);
registry->Register(FastToggleImmediateRef);
registry->Register(fast_toggle_immediate_ref_.GetTypeInfo());
registry->Register(fast_toggle_immediate_ref_);
}

} // namespace timers
Expand Down
Loading
Loading