@@ -54,6 +54,7 @@ using v8::Function;
54
54
using v8::FunctionCallbackInfo;
55
55
using v8::Int32;
56
56
using v8::Integer;
57
+ using v8::Isolate;
57
58
using v8::Local;
58
59
using v8::MaybeLocal;
59
60
using v8::Null;
@@ -148,52 +149,33 @@ static void GetOSRelease(const FunctionCallbackInfo<Value>& args) {
148
149
149
150
static void GetCPUInfo (const FunctionCallbackInfo<Value>& args) {
150
151
Environment* env = Environment::GetCurrent (args);
152
+ Isolate* isolate = env->isolate ();
153
+
151
154
uv_cpu_info_t * cpu_infos;
152
- int count, i, field_idx ;
155
+ int count;
153
156
154
157
int err = uv_cpu_info (&cpu_infos, &count);
155
158
if (err)
156
159
return ;
157
160
158
- CHECK (args[0 ]->IsFunction ());
159
- Local<Function> addfn = args[0 ].As <Function>();
160
-
161
- CHECK (args[1 ]->IsFloat64Array ());
162
- Local<Float64Array> array = args[1 ].As <Float64Array>();
163
- CHECK_EQ (array->Length (), 6 * NODE_PUSH_VAL_TO_ARRAY_MAX);
164
- Local<ArrayBuffer> ab = array->Buffer ();
165
- double * fields = static_cast <double *>(ab->GetContents ().Data ());
166
-
167
- CHECK (args[2 ]->IsArray ());
168
- Local<Array> cpus = args[2 ].As <Array>();
169
-
170
- Local<Value> model_argv[NODE_PUSH_VAL_TO_ARRAY_MAX];
171
- int model_idx = 0 ;
172
-
173
- for (i = 0 , field_idx = 0 ; i < count; i++) {
161
+ // It's faster to create an array packed with all the data and
162
+ // assemble them into objects in JS than to call Object::Set() repeatedly
163
+ // The array is in the format
164
+ // [model, speed, (5 entries of cpu_times), model2, speed2, ...]
165
+ std::vector<Local<Value>> result (count * 7 );
166
+ for (size_t i = 0 ; i < count; i++) {
174
167
uv_cpu_info_t * ci = cpu_infos + i;
175
-
176
- fields[field_idx++] = ci->speed ;
177
- fields[field_idx++] = ci->cpu_times .user ;
178
- fields[field_idx++] = ci->cpu_times .nice ;
179
- fields[field_idx++] = ci->cpu_times .sys ;
180
- fields[field_idx++] = ci->cpu_times .idle ;
181
- fields[field_idx++] = ci->cpu_times .irq ;
182
- model_argv[model_idx++] = OneByteString (env->isolate (), ci->model );
183
-
184
- if (model_idx >= NODE_PUSH_VAL_TO_ARRAY_MAX) {
185
- addfn->Call (env->context (), cpus, model_idx, model_argv).ToLocalChecked ();
186
- model_idx = 0 ;
187
- field_idx = 0 ;
188
- }
189
- }
190
-
191
- if (model_idx > 0 ) {
192
- addfn->Call (env->context (), cpus, model_idx, model_argv).ToLocalChecked ();
168
+ result[i * 7 ] = OneByteString (isolate, ci->model );
169
+ result[i * 7 + 1 ] = Number::New (isolate, ci->speed );
170
+ result[i * 7 + 2 ] = Number::New (isolate, ci->cpu_times .user );
171
+ result[i * 7 + 3 ] = Number::New (isolate, ci->cpu_times .nice );
172
+ result[i * 7 + 4 ] = Number::New (isolate, ci->cpu_times .sys );
173
+ result[i * 7 + 5 ] = Number::New (isolate, ci->cpu_times .idle );
174
+ result[i * 7 + 6 ] = Number::New (isolate, ci->cpu_times .irq );
193
175
}
194
176
195
177
uv_free_cpu_info (cpu_infos, count);
196
- args.GetReturnValue ().Set (cpus );
178
+ args.GetReturnValue ().Set (Array::New (isolate, result. data (), result. size ()) );
197
179
}
198
180
199
181
0 commit comments