Skip to content
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: refactor code to remove duplicate logic #34553

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 13 additions & 12 deletions src/node_process_methods.cc
Expand Up @@ -89,6 +89,16 @@ static void Chdir(const FunctionCallbackInfo<Value>& args) {
}
}

inline Local<ArrayBuffer> get_fields_array_buffer(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be better to name it like get_fields_array_buffer_from_first_argument, or accept an argument specifying the index of the argument array, otherwise this new convention makes the code less easier to grok IMO.

const FunctionCallbackInfo<Value>& args,
size_t index,
size_t array_length) {
CHECK(args[index]->IsFloat64Array());
Local<Float64Array> arr = args[index].As<Float64Array>();
CHECK_EQ(arr->Length(), array_length);
return arr->Buffer();
}

// CPUUsage use libuv's uv_getrusage() this-process resource usage accessor,
// to access ru_utime (user CPU time used) and ru_stime (system CPU time used),
// which are uv_timeval_t structs (long tv_sec, long tv_usec).
Expand All @@ -106,10 +116,7 @@ static void CPUUsage(const FunctionCallbackInfo<Value>& args) {
}

// Get the double array pointer from the Float64Array argument.
CHECK(args[0]->IsFloat64Array());
Local<Float64Array> array = args[0].As<Float64Array>();
CHECK_EQ(array->Length(), 2);
Local<ArrayBuffer> ab = array->Buffer();
Local<ArrayBuffer> ab = get_fields_array_buffer(args, 0, 2);
double* fields = static_cast<double*>(ab->GetBackingStore()->Data());

// Set the Float64Array elements to be user / system values in microseconds.
Expand Down Expand Up @@ -175,10 +182,7 @@ static void MemoryUsage(const FunctionCallbackInfo<Value>& args) {
env->isolate_data()->node_allocator();

// Get the double array pointer from the Float64Array argument.
CHECK(args[0]->IsFloat64Array());
Local<Float64Array> array = args[0].As<Float64Array>();
CHECK_EQ(array->Length(), 5);
Local<ArrayBuffer> ab = array->Buffer();
Local<ArrayBuffer> ab = get_fields_array_buffer(args, 0, 5);
double* fields = static_cast<double*>(ab->GetBackingStore()->Data());

fields[0] = rss;
Expand Down Expand Up @@ -275,10 +279,7 @@ static void ResourceUsage(const FunctionCallbackInfo<Value>& args) {
if (err)
return env->ThrowUVException(err, "uv_getrusage");

CHECK(args[0]->IsFloat64Array());
Local<Float64Array> array = args[0].As<Float64Array>();
CHECK_EQ(array->Length(), 16);
Local<ArrayBuffer> ab = array->Buffer();
Local<ArrayBuffer> ab = get_fields_array_buffer(args, 0, 16);
double* fields = static_cast<double*>(ab->GetBackingStore()->Data());

fields[0] = MICROS_PER_SEC * rusage.ru_utime.tv_sec + rusage.ru_utime.tv_usec;
Expand Down