Skip to content

Commit 13eb1d8

Browse files
addaleaxtargos
authored andcommittedMar 27, 2019
src: store onread callback in internal field
This gives a slight performance improvement. At 2000 runs: confidence improvement accuracy (*) (**) (***) net/net-c2s.js dur=5 type='buf' len=64 *** 0.54 % ±0.16% ±0.21% ±0.27% PR-URL: #26837 Reviewed-By: Joyee Cheung <joyeec9h3@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 9a5c149 commit 13eb1d8

14 files changed

+46
-12
lines changed
 

‎src/base_object-inl.h

+16
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,22 @@ BaseObject::MakeLazilyInitializedJSTemplate(Environment* env) {
123123
return t;
124124
}
125125

126+
template <int Field>
127+
void BaseObject::InternalFieldGet(
128+
v8::Local<v8::String> property,
129+
const v8::PropertyCallbackInfo<v8::Value>& info) {
130+
info.GetReturnValue().Set(info.This()->GetInternalField(Field));
131+
}
132+
133+
template <int Field, bool (v8::Value::* typecheck)() const>
134+
void BaseObject::InternalFieldSet(v8::Local<v8::String> property,
135+
v8::Local<v8::Value> value,
136+
const v8::PropertyCallbackInfo<void>& info) {
137+
// This could be e.g. value->IsFunction().
138+
CHECK(((*value)->*typecheck)());
139+
info.This()->SetInternalField(Field, value);
140+
}
141+
126142
} // namespace node
127143

128144
#endif // defined(NODE_WANT_INTERNALS) && NODE_WANT_INTERNALS

‎src/base_object.h

+9
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,15 @@ class BaseObject : public MemoryRetainer {
7373
static inline v8::Local<v8::FunctionTemplate> MakeLazilyInitializedJSTemplate(
7474
Environment* env);
7575

76+
// Setter/Getter pair for internal fields that can be passed to SetAccessor.
77+
template <int Field>
78+
static void InternalFieldGet(v8::Local<v8::String> property,
79+
const v8::PropertyCallbackInfo<v8::Value>& info);
80+
template <int Field, bool (v8::Value::* typecheck)() const>
81+
static void InternalFieldSet(v8::Local<v8::String> property,
82+
v8::Local<v8::Value> value,
83+
const v8::PropertyCallbackInfo<void>& info);
84+
7685
private:
7786
BaseObject();
7887

‎src/env.h

-1
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,6 @@ constexpr size_t kFsStatsBufferLength = kFsStatsFieldsNumber * 2;
235235
V(onmessage_string, "onmessage") \
236236
V(onnewsession_string, "onnewsession") \
237237
V(onocspresponse_string, "onocspresponse") \
238-
V(onread_string, "onread") \
239238
V(onreadstart_string, "onreadstart") \
240239
V(onreadstop_string, "onreadstop") \
241240
V(onshutdown_string, "onshutdown") \

‎src/heap_utils.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ void Initialize(Local<Object> target,
396396
Local<FunctionTemplate> os = FunctionTemplate::New(env->isolate());
397397
os->Inherit(AsyncWrap::GetConstructorTemplate(env));
398398
Local<ObjectTemplate> ost = os->InstanceTemplate();
399-
ost->SetInternalFieldCount(StreamBase::kStreamBaseField + 1);
399+
ost->SetInternalFieldCount(StreamBase::kStreamBaseFieldCount);
400400
os->SetClassName(FIXED_ONE_BYTE_STRING(env->isolate(), "HeapSnapshotStream"));
401401
StreamBase::AddMethods(env, os);
402402
env->set_streambaseoutputstream_constructor_template(ost);

‎src/js_stream.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ void JSStream::Initialize(Local<Object> target,
205205
FIXED_ONE_BYTE_STRING(env->isolate(), "JSStream");
206206
t->SetClassName(jsStreamString);
207207
t->InstanceTemplate()
208-
->SetInternalFieldCount(StreamBase::kStreamBaseField + 1);
208+
->SetInternalFieldCount(StreamBase::kStreamBaseFieldCount);
209209
t->Inherit(AsyncWrap::GetConstructorTemplate(env));
210210

211211
env->SetProtoMethod(t, "finishWrite", Finish<WriteWrap>);

‎src/node_file.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -2231,7 +2231,7 @@ void Initialize(Local<Object> target,
22312231
env->SetProtoMethod(fd, "close", FileHandle::Close);
22322232
env->SetProtoMethod(fd, "releaseFD", FileHandle::ReleaseFD);
22332233
Local<ObjectTemplate> fdt = fd->InstanceTemplate();
2234-
fdt->SetInternalFieldCount(StreamBase::kStreamBaseField + 1);
2234+
fdt->SetInternalFieldCount(StreamBase::kStreamBaseFieldCount);
22352235
Local<String> handleString =
22362236
FIXED_ONE_BYTE_STRING(isolate, "FileHandle");
22372237
fd->SetClassName(handleString);

‎src/node_http2.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -3012,7 +3012,7 @@ void Initialize(Local<Object> target,
30123012
stream->Inherit(AsyncWrap::GetConstructorTemplate(env));
30133013
StreamBase::AddMethods(env, stream);
30143014
Local<ObjectTemplate> streamt = stream->InstanceTemplate();
3015-
streamt->SetInternalFieldCount(StreamBase::kStreamBaseField + 1);
3015+
streamt->SetInternalFieldCount(StreamBase::kStreamBaseFieldCount);
30163016
env->set_http2stream_constructor_template(streamt);
30173017
target->Set(context,
30183018
FIXED_ONE_BYTE_STRING(env->isolate(), "Http2Stream"),

‎src/pipe_wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ void PipeWrap::Initialize(Local<Object> target,
7575
Local<String> pipeString = FIXED_ONE_BYTE_STRING(env->isolate(), "Pipe");
7676
t->SetClassName(pipeString);
7777
t->InstanceTemplate()
78-
->SetInternalFieldCount(StreamBase::kStreamBaseField + 1);
78+
->SetInternalFieldCount(StreamBase::kStreamBaseFieldCount);
7979

8080
t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
8181

‎src/stream_base.cc

+8-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ using v8::Context;
2121
using v8::DontDelete;
2222
using v8::DontEnum;
2323
using v8::External;
24+
using v8::Function;
2425
using v8::FunctionCallbackInfo;
2526
using v8::HandleScope;
2627
using v8::Integer;
@@ -314,7 +315,9 @@ void StreamBase::CallJSOnreadMethod(ssize_t nread,
314315

315316
AsyncWrap* wrap = GetAsyncWrap();
316317
CHECK_NOT_NULL(wrap);
317-
wrap->MakeCallback(env->onread_string(), arraysize(argv), argv);
318+
Local<Value> onread = wrap->object()->GetInternalField(kOnReadFunctionField);
319+
CHECK(onread->IsFunction());
320+
wrap->MakeCallback(onread.As<Function>(), arraysize(argv), argv);
318321
}
319322

320323

@@ -376,6 +379,10 @@ void StreamBase::AddMethods(Environment* env, Local<FunctionTemplate> t) {
376379
t->PrototypeTemplate()->Set(FIXED_ONE_BYTE_STRING(env->isolate(),
377380
"isStreamBase"),
378381
True(env->isolate()));
382+
t->PrototypeTemplate()->SetAccessor(
383+
FIXED_ONE_BYTE_STRING(env->isolate(), "onread"),
384+
BaseObject::InternalFieldGet<kOnReadFunctionField>,
385+
BaseObject::InternalFieldSet<kOnReadFunctionField, &Value::IsFunction>);
379386
}
380387

381388
void StreamBase::GetFD(const FunctionCallbackInfo<Value>& args) {

‎src/stream_base.h

+4
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,11 @@ class StreamResource {
260260

261261
class StreamBase : public StreamResource {
262262
public:
263+
// 0 is reserved for the BaseObject pointer.
263264
static constexpr int kStreamBaseField = 1;
265+
static constexpr int kOnReadFunctionField = 2;
266+
static constexpr int kStreamBaseFieldCount = 3;
267+
264268
static void AddMethods(Environment* env,
265269
v8::Local<v8::FunctionTemplate> target);
266270

‎src/stream_wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ Local<FunctionTemplate> LibuvStreamWrap::GetConstructorTemplate(
136136
FIXED_ONE_BYTE_STRING(env->isolate(), "LibuvStreamWrap"));
137137
tmpl->Inherit(HandleWrap::GetConstructorTemplate(env));
138138
tmpl->InstanceTemplate()->SetInternalFieldCount(
139-
StreamBase::kStreamBaseField + 1);
139+
StreamBase::kStreamBaseFieldCount);
140140
Local<FunctionTemplate> get_write_queue_size =
141141
FunctionTemplate::New(env->isolate(),
142142
GetWriteQueueSize,

‎src/tcp_wrap.cc

+1-2
Original file line numberDiff line numberDiff line change
@@ -80,13 +80,12 @@ void TCPWrap::Initialize(Local<Object> target,
8080
Local<String> tcpString = FIXED_ONE_BYTE_STRING(env->isolate(), "TCP");
8181
t->SetClassName(tcpString);
8282
t->InstanceTemplate()
83-
->SetInternalFieldCount(StreamBase::kStreamBaseField + 1);
83+
->SetInternalFieldCount(StreamBase::kStreamBaseFieldCount);
8484

8585
// Init properties
8686
t->InstanceTemplate()->Set(FIXED_ONE_BYTE_STRING(env->isolate(), "reading"),
8787
Boolean::New(env->isolate(), false));
8888
t->InstanceTemplate()->Set(env->owner_symbol(), Null(env->isolate()));
89-
t->InstanceTemplate()->Set(env->onread_string(), Null(env->isolate()));
9089
t->InstanceTemplate()->Set(env->onconnection_string(), Null(env->isolate()));
9190

9291
t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));

‎src/tls_wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ void TLSWrap::Initialize(Local<Object> target,
924924
FIXED_ONE_BYTE_STRING(env->isolate(), "TLSWrap");
925925
t->SetClassName(tlsWrapString);
926926
t->InstanceTemplate()
927-
->SetInternalFieldCount(StreamBase::kStreamBaseField + 1);
927+
->SetInternalFieldCount(StreamBase::kStreamBaseFieldCount);
928928

929929
Local<FunctionTemplate> get_write_queue_size =
930930
FunctionTemplate::New(env->isolate(),

‎src/tty_wrap.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ void TTYWrap::Initialize(Local<Object> target,
5252
Local<FunctionTemplate> t = env->NewFunctionTemplate(New);
5353
t->SetClassName(ttyString);
5454
t->InstanceTemplate()
55-
->SetInternalFieldCount(StreamBase::kStreamBaseField + 1);
55+
->SetInternalFieldCount(StreamBase::kStreamBaseFieldCount);
5656
t->Inherit(LibuvStreamWrap::GetConstructorTemplate(env));
5757

5858
env->SetProtoMethodNoSideEffect(t, "getWindowSize", TTYWrap::GetWindowSize);

0 commit comments

Comments
 (0)
Please sign in to comment.