Skip to content

Commit 58a8eb4

Browse files
authored
src: improve node:os userInfo performance
PR-URL: #55719 Reviewed-By: Robert Nagy <[email protected]> Reviewed-By: Antoine du Hamel <[email protected]> Reviewed-By: Richard Lau <[email protected]>
1 parent a243225 commit 58a8eb4

File tree

2 files changed

+28
-24
lines changed

2 files changed

+28
-24
lines changed

lib/os.js

-5
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,6 @@ function userInfo(options) {
358358
if (user === undefined)
359359
throw new ERR_SYSTEM_ERROR(ctx);
360360

361-
if (isWindows) {
362-
user.uid |= 0;
363-
user.gid |= 0;
364-
}
365-
366361
return user;
367362
}
368363

src/node_os.cc

+28-19
Original file line numberDiff line numberDiff line change
@@ -287,21 +287,29 @@ static void GetUserInfo(const FunctionCallbackInfo<Value>& args) {
287287
encoding = UTF8;
288288
}
289289

290-
const int err = uv_os_get_passwd(&pwd);
291-
292-
if (err) {
290+
if (const int err = uv_os_get_passwd(&pwd)) {
293291
CHECK_GE(args.Length(), 2);
294292
env->CollectUVExceptionInfo(args[args.Length() - 1], err,
295293
"uv_os_get_passwd");
296294
return args.GetReturnValue().SetUndefined();
297295
}
298296

299-
auto free_passwd = OnScopeLeave([&]() { uv_os_free_passwd(&pwd); });
297+
auto free_passwd = OnScopeLeave([&] { uv_os_free_passwd(&pwd); });
300298

301299
Local<Value> error;
302300

301+
#ifdef _WIN32
302+
Local<Value> uid = Number::New(
303+
env->isolate(),
304+
static_cast<double>(static_cast<int32_t>(pwd.uid & 0xFFFFFFFF)));
305+
Local<Value> gid = Number::New(
306+
env->isolate(),
307+
static_cast<double>(static_cast<int32_t>(pwd.gid & 0xFFFFFFFF)));
308+
#else
303309
Local<Value> uid = Number::New(env->isolate(), pwd.uid);
304310
Local<Value> gid = Number::New(env->isolate(), pwd.gid);
311+
#endif
312+
305313
MaybeLocal<Value> username = StringBytes::Encode(env->isolate(),
306314
pwd.username,
307315
encoding,
@@ -323,21 +331,22 @@ static void GetUserInfo(const FunctionCallbackInfo<Value>& args) {
323331
return;
324332
}
325333

326-
Local<Object> entry = Object::New(env->isolate());
327-
328-
entry->Set(env->context(), env->uid_string(), uid).Check();
329-
entry->Set(env->context(), env->gid_string(), gid).Check();
330-
entry->Set(env->context(),
331-
env->username_string(),
332-
username.ToLocalChecked()).Check();
333-
entry->Set(env->context(),
334-
env->homedir_string(),
335-
homedir.ToLocalChecked()).Check();
336-
entry->Set(env->context(),
337-
env->shell_string(),
338-
shell.ToLocalChecked()).Check();
339-
340-
args.GetReturnValue().Set(entry);
334+
constexpr size_t kRetLength = 5;
335+
std::array<Local<v8::Name>, kRetLength> names = {env->uid_string(),
336+
env->gid_string(),
337+
env->username_string(),
338+
env->homedir_string(),
339+
env->shell_string()};
340+
std::array values = {uid,
341+
gid,
342+
username.ToLocalChecked(),
343+
homedir.ToLocalChecked(),
344+
shell.ToLocalChecked()};
345+
args.GetReturnValue().Set(Object::New(env->isolate(),
346+
Null(env->isolate()),
347+
names.data(),
348+
values.data(),
349+
kRetLength));
341350
}
342351

343352

0 commit comments

Comments
 (0)