Skip to content

Commit 955be86

Browse files
committed
src: use object to pass Environment to functions
Use a `v8::Object` with an internal field, rather than a `v8::External`. On a `GetReturnValue().Set(Environment::GetCurrent(args) == nullptr)` noop function, this benchmarks as a ~60 % speedup, as calls to `obj->GetAlignedPointerFromInternalField()` can be inlined and the field is stored with one level of indirection less. This also makes breaking up some pieces of the `Environment` class into per-native-binding data easier, if we want to pursue that path in the future. PR-URL: #26382 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 1bdcfa7 commit 955be86

File tree

11 files changed

+39
-20
lines changed

11 files changed

+39
-20
lines changed

src/env-inl.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -316,16 +316,23 @@ inline Environment* Environment::GetCurrent(v8::Local<v8::Context> context) {
316316

317317
inline Environment* Environment::GetCurrent(
318318
const v8::FunctionCallbackInfo<v8::Value>& info) {
319-
CHECK(info.Data()->IsExternal());
320-
return static_cast<Environment*>(info.Data().As<v8::External>()->Value());
319+
return GetFromCallbackData(info.Data());
321320
}
322321

323322
template <typename T>
324323
inline Environment* Environment::GetCurrent(
325324
const v8::PropertyCallbackInfo<T>& info) {
326-
CHECK(info.Data()->IsExternal());
327-
return static_cast<Environment*>(
328-
info.Data().template As<v8::External>()->Value());
325+
return GetFromCallbackData(info.Data());
326+
}
327+
328+
inline Environment* Environment::GetFromCallbackData(v8::Local<v8::Value> val) {
329+
DCHECK(val->IsObject());
330+
v8::Local<v8::Object> obj = val.As<v8::Object>();
331+
DCHECK_GE(obj->InternalFieldCount(), 1);
332+
Environment* env =
333+
static_cast<Environment*>(obj->GetAlignedPointerFromInternalField(0));
334+
DCHECK(env->as_callback_data_template()->HasInstance(obj));
335+
return env;
329336
}
330337

331338
inline Environment* Environment::GetThreadLocalEnv() {
@@ -857,7 +864,7 @@ inline v8::Local<v8::FunctionTemplate>
857864
v8::Local<v8::Signature> signature,
858865
v8::ConstructorBehavior behavior,
859866
v8::SideEffectType side_effect_type) {
860-
v8::Local<v8::External> external = as_external();
867+
v8::Local<v8::Object> external = as_callback_data();
861868
return v8::FunctionTemplate::New(isolate(), callback, external,
862869
signature, 0, behavior, side_effect_type);
863870
}

src/env.cc

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ using v8::ArrayBuffer;
2525
using v8::Boolean;
2626
using v8::Context;
2727
using v8::EmbedderGraph;
28-
using v8::External;
2928
using v8::Function;
29+
using v8::FunctionTemplate;
3030
using v8::HandleScope;
3131
using v8::Integer;
3232
using v8::Isolate;
@@ -195,7 +195,16 @@ Environment::Environment(IsolateData* isolate_data,
195195
// We'll be creating new objects so make sure we've entered the context.
196196
HandleScope handle_scope(isolate());
197197
Context::Scope context_scope(context);
198-
set_as_external(External::New(isolate(), this));
198+
{
199+
Local<FunctionTemplate> templ = FunctionTemplate::New(isolate());
200+
templ->InstanceTemplate()->SetInternalFieldCount(1);
201+
Local<Object> obj =
202+
templ->GetFunction(context).ToLocalChecked()->NewInstance(
203+
context).ToLocalChecked();
204+
obj->SetAlignedPointerInInternalField(0, this);
205+
set_as_callback_data(obj);
206+
set_as_callback_data_template(templ);
207+
}
199208

200209
// We create new copies of the per-Environment option sets, so that it is
201210
// easier to modify them after Environment creation. The defaults are

src/env.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -321,7 +321,8 @@ constexpr size_t kFsStatsBufferLength = kFsStatsFieldsNumber * 2;
321321
V(zero_return_string, "ZERO_RETURN")
322322

323323
#define ENVIRONMENT_STRONG_PERSISTENT_PROPERTIES(V) \
324-
V(as_external, v8::External) \
324+
V(as_callback_data, v8::Object) \
325+
V(as_callback_data_template, v8::FunctionTemplate) \
325326
V(async_hooks_after_function, v8::Function) \
326327
V(async_hooks_before_function, v8::Function) \
327328
V(async_hooks_binding, v8::Object) \
@@ -663,6 +664,8 @@ class Environment {
663664
static inline Environment* GetCurrent(
664665
const v8::PropertyCallbackInfo<T>& info);
665666

667+
static inline Environment* GetFromCallbackData(v8::Local<v8::Value> val);
668+
666669
static uv_key_t thread_local_env;
667670
static inline Environment* GetThreadLocalEnv();
668671

src/fs_event_wrap.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ void FSEventWrap::Initialize(Local<Object> target,
111111
Local<FunctionTemplate> get_initialized_templ =
112112
FunctionTemplate::New(env->isolate(),
113113
GetInitialized,
114-
env->as_external(),
114+
env->as_callback_data(),
115115
Signature::New(env->isolate(), t));
116116

117117
t->PrototypeTemplate()->SetAccessorProperty(

src/node_crypto.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ void SecureContext::Initialize(Environment* env, Local<Object> target) {
371371
Local<FunctionTemplate> ctx_getter_templ =
372372
FunctionTemplate::New(env->isolate(),
373373
CtxGetter,
374-
env->as_external(),
374+
env->as_callback_data(),
375375
Signature::New(env->isolate(), t));
376376

377377

@@ -4789,7 +4789,7 @@ void DiffieHellman::Initialize(Environment* env, Local<Object> target) {
47894789
Local<FunctionTemplate> verify_error_getter_templ =
47904790
FunctionTemplate::New(env->isolate(),
47914791
DiffieHellman::VerifyErrorGetter,
4792-
env->as_external(),
4792+
env->as_callback_data(),
47934793
Signature::New(env->isolate(), t),
47944794
/* length */ 0,
47954795
ConstructorBehavior::kThrow,

src/node_env_var.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
212212

213213
MaybeLocal<Object> CreateEnvVarProxy(Local<Context> context,
214214
Isolate* isolate,
215-
Local<Value> data) {
215+
Local<Object> data) {
216216
EscapableHandleScope scope(isolate);
217217
Local<ObjectTemplate> env_proxy_template = ObjectTemplate::New(isolate);
218218
env_proxy_template->SetHandler(NamedPropertyHandlerConfiguration(

src/node_process.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ namespace node {
99

1010
v8::MaybeLocal<v8::Object> CreateEnvVarProxy(v8::Local<v8::Context> context,
1111
v8::Isolate* isolate,
12-
v8::Local<v8::Value> data);
12+
v8::Local<v8::Object> data);
1313

1414
// Most of the time, it's best to use `console.error` to write
1515
// to the process.stderr stream. However, in some cases, such as

src/node_process_object.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ MaybeLocal<Object> CreateProcessObject(
9292
title_string,
9393
ProcessTitleGetter,
9494
env->owns_process_state() ? ProcessTitleSetter : nullptr,
95-
env->as_external(),
95+
env->as_callback_data(),
9696
DEFAULT,
9797
None,
9898
SideEffectType::kHasNoSideEffect)
@@ -152,7 +152,7 @@ MaybeLocal<Object> CreateProcessObject(
152152
.ToLocalChecked()).FromJust();
153153

154154
Local<Object> env_var_proxy;
155-
if (!CreateEnvVarProxy(context, isolate, env->as_external())
155+
if (!CreateEnvVarProxy(context, isolate, env->as_callback_data())
156156
.ToLocal(&env_var_proxy))
157157
return MaybeLocal<Object>();
158158

@@ -310,7 +310,7 @@ MaybeLocal<Object> CreateProcessObject(
310310
debug_port_string,
311311
DebugPortGetter,
312312
env->owns_process_state() ? DebugPortSetter : nullptr,
313-
env->as_external())
313+
env->as_callback_data())
314314
.FromJust());
315315

316316
// process._rawDebug: may be overwritten later in JS land, but should be

src/stream_wrap.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ Local<FunctionTemplate> LibuvStreamWrap::GetConstructorTemplate(
137137
Local<FunctionTemplate> get_write_queue_size =
138138
FunctionTemplate::New(env->isolate(),
139139
GetWriteQueueSize,
140-
env->as_external(),
140+
env->as_callback_data(),
141141
Signature::New(env->isolate(), tmpl));
142142
tmpl->PrototypeTemplate()->SetAccessorProperty(
143143
env->write_queue_size_string(),

src/tls_wrap.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ void TLSWrap::Initialize(Local<Object> target,
962962
Local<FunctionTemplate> get_write_queue_size =
963963
FunctionTemplate::New(env->isolate(),
964964
GetWriteQueueSize,
965-
env->as_external(),
965+
env->as_callback_data(),
966966
Signature::New(env->isolate(), t));
967967
t->PrototypeTemplate()->SetAccessorProperty(
968968
env->write_queue_size_string(),

0 commit comments

Comments
 (0)