-
Notifications
You must be signed in to change notification settings - Fork 30.4k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
src: replace pushValueToArray with pure C++ API #24125
Changes from 3 commits
d1fdd28
e02f5a6
72c6b4b
beb263d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -571,10 +571,7 @@ void AfterScanDir(uv_fs_t* req) { | |
Environment* env = req_wrap->env(); | ||
Local<Value> error; | ||
int r; | ||
Local<Array> names = Array::New(env->isolate(), 0); | ||
Local<Function> fn = env->push_values_to_array_function(); | ||
Local<Value> name_argv[NODE_PUSH_VAL_TO_ARRAY_MAX]; | ||
size_t name_idx = 0; | ||
std::vector<Local<Value>> name_v; | ||
|
||
for (int i = 0; ; i++) { | ||
uv_dirent_t ent; | ||
|
@@ -596,24 +593,10 @@ void AfterScanDir(uv_fs_t* req) { | |
if (filename.IsEmpty()) | ||
return req_wrap->Reject(error); | ||
|
||
name_argv[name_idx++] = filename.ToLocalChecked(); | ||
|
||
if (name_idx >= arraysize(name_argv)) { | ||
MaybeLocal<Value> ret = fn->Call(env->context(), names, name_idx, | ||
name_argv); | ||
if (ret.IsEmpty()) { | ||
return; | ||
} | ||
name_idx = 0; | ||
} | ||
} | ||
|
||
if (name_idx > 0) { | ||
fn->Call(env->context(), names, name_idx, name_argv) | ||
.ToLocalChecked(); | ||
name_v.push_back(filename.ToLocalChecked()); | ||
} | ||
|
||
req_wrap->Resolve(names); | ||
req_wrap->Resolve(Array::New(env->isolate(), name_v.data(), name_v.size())); | ||
} | ||
|
||
void AfterScanDirWithTypes(uv_fs_t* req) { | ||
|
@@ -628,13 +611,9 @@ void AfterScanDirWithTypes(uv_fs_t* req) { | |
Isolate* isolate = env->isolate(); | ||
Local<Value> error; | ||
int r; | ||
Local<Array> names = Array::New(isolate, 0); | ||
Local<Function> fn = env->push_values_to_array_function(); | ||
Local<Value> name_argv[NODE_PUSH_VAL_TO_ARRAY_MAX]; | ||
size_t name_idx = 0; | ||
Local<Value> types = Array::New(isolate, 0); | ||
Local<Value> type_argv[NODE_PUSH_VAL_TO_ARRAY_MAX]; | ||
size_t type_idx = 0; | ||
|
||
std::vector<Local<Value>> name_v; | ||
std::vector<Local<Value>> type_v; | ||
|
||
for (int i = 0; ; i++) { | ||
uv_dirent_t ent; | ||
|
@@ -656,48 +635,13 @@ void AfterScanDirWithTypes(uv_fs_t* req) { | |
if (filename.IsEmpty()) | ||
return req_wrap->Reject(error); | ||
|
||
name_argv[name_idx++] = filename.ToLocalChecked(); | ||
|
||
if (name_idx >= arraysize(name_argv)) { | ||
MaybeLocal<Value> ret = fn->Call(env->context(), names, name_idx, | ||
name_argv); | ||
if (ret.IsEmpty()) { | ||
return; | ||
} | ||
name_idx = 0; | ||
} | ||
|
||
type_argv[type_idx++] = Integer::New(isolate, ent.type); | ||
|
||
if (type_idx >= arraysize(type_argv)) { | ||
MaybeLocal<Value> ret = fn->Call(env->context(), types, type_idx, | ||
type_argv); | ||
if (ret.IsEmpty()) { | ||
return; | ||
} | ||
type_idx = 0; | ||
} | ||
} | ||
|
||
if (name_idx > 0) { | ||
MaybeLocal<Value> ret = fn->Call(env->context(), names, name_idx, | ||
name_argv); | ||
if (ret.IsEmpty()) { | ||
return; | ||
} | ||
} | ||
|
||
if (type_idx > 0) { | ||
MaybeLocal<Value> ret = fn->Call(env->context(), types, type_idx, | ||
type_argv); | ||
if (ret.IsEmpty()) { | ||
return; | ||
} | ||
name_v.push_back(filename.ToLocalChecked()); | ||
type_v.push_back(Integer::New(isolate, ent.type)); | ||
} | ||
|
||
Local<Array> result = Array::New(isolate, 2); | ||
result->Set(0, names); | ||
result->Set(1, types); | ||
result->Set(0, Array::New(isolate, name_v.data(), name_v.size())); | ||
result->Set(1, Array::New(isolate, type_v.data(), type_v.size())); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You could improve on this a little by returning a single names+types array. You also need to update There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @bnoordhuis Good idea, though I'd rather leave that as a follow-up |
||
req_wrap->Resolve(result); | ||
} | ||
|
||
|
@@ -1497,18 +1441,8 @@ static void ReadDir(const FunctionCallbackInfo<Value>& args) { | |
|
||
CHECK_GE(req_wrap_sync.req.result, 0); | ||
int r; | ||
Local<Array> names = Array::New(isolate, 0); | ||
Local<Function> fn = env->push_values_to_array_function(); | ||
Local<Value> name_v[NODE_PUSH_VAL_TO_ARRAY_MAX]; | ||
size_t name_idx = 0; | ||
|
||
Local<Value> types; | ||
Local<Value> type_v[NODE_PUSH_VAL_TO_ARRAY_MAX]; | ||
size_t type_idx; | ||
if (with_types) { | ||
types = Array::New(isolate, 0); | ||
type_idx = 0; | ||
} | ||
std::vector<Local<Value>> name_v; | ||
std::vector<Local<Value>> type_v; | ||
|
||
for (int i = 0; ; i++) { | ||
uv_dirent_t ent; | ||
|
@@ -1537,49 +1471,19 @@ static void ReadDir(const FunctionCallbackInfo<Value>& args) { | |
return; | ||
} | ||
|
||
name_v[name_idx++] = filename.ToLocalChecked(); | ||
|
||
if (name_idx >= arraysize(name_v)) { | ||
MaybeLocal<Value> ret = fn->Call(env->context(), names, name_idx, | ||
name_v); | ||
if (ret.IsEmpty()) { | ||
return; | ||
} | ||
name_idx = 0; | ||
} | ||
name_v.push_back(filename.ToLocalChecked()); | ||
|
||
if (with_types) { | ||
type_v[type_idx++] = Integer::New(isolate, ent.type); | ||
|
||
if (type_idx >= arraysize(type_v)) { | ||
MaybeLocal<Value> ret = fn->Call(env->context(), types, type_idx, | ||
type_v); | ||
if (ret.IsEmpty()) { | ||
return; | ||
} | ||
type_idx = 0; | ||
} | ||
type_v.push_back(Integer::New(isolate, ent.type)); | ||
} | ||
} | ||
|
||
if (name_idx > 0) { | ||
MaybeLocal<Value> ret = fn->Call(env->context(), names, name_idx, name_v); | ||
if (ret.IsEmpty()) { | ||
return; | ||
} | ||
} | ||
|
||
if (with_types && type_idx > 0) { | ||
MaybeLocal<Value> ret = fn->Call(env->context(), types, type_idx, type_v); | ||
if (ret.IsEmpty()) { | ||
return; | ||
} | ||
} | ||
|
||
Local<Array> names = Array::New(isolate, name_v.data(), name_v.size()); | ||
if (with_types) { | ||
Local<Array> result = Array::New(isolate, 2); | ||
result->Set(0, names); | ||
result->Set(1, types); | ||
result->Set(1, Array::New(isolate, type_v.data(), type_v.size())); | ||
args.GetReturnValue().Set(result); | ||
} else { | ||
args.GetReturnValue().Set(names); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe something for a separate commit or PR but you could move this to before the
bench.start()
call. The fewer tangents the benchmark has, the better.(Ditto in the other file.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, thanks for spotting this! (Pretty sure I made a change for that before, somehow it got lost before I opened the PR..)