Skip to content

Commit 5664791

Browse files
committed
src: use libuv to get env vars
This allows us to remove OS-dependent code. confidence improvement accuracy (*) (**) (***) process/bench-env.js operation='delete' n=1000000 3.57 % ±10.86% ±14.46% ±18.85% process/bench-env.js operation='enumerate' n=1000000 *** -14.06 % ±7.46% ±9.94% ±12.96% process/bench-env.js operation='get' n=1000000 -7.97 % ±11.80% ±15.70% ±20.45% process/bench-env.js operation='query' n=1000000 -1.32 % ±8.38% ±11.17% ±14.58% process/bench-env.js operation='set' n=1000000 -0.98 % ±9.63% ±12.81% ±16.68% The drop in enumeration performance is likely due to the large number of extra allocations that libuv performs. However, enumerating process.env should generally not be a hot path in most applications. PR-URL: #29188 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent 82ebcb3 commit 5664791

File tree

1 file changed

+28
-56
lines changed

1 file changed

+28
-56
lines changed

src/node_env_var.cc

Lines changed: 28 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,6 @@
44

55
#include <time.h> // tzset(), _tzset()
66

7-
#ifdef __APPLE__
8-
#include <crt_externs.h>
9-
#define environ (*_NSGetEnviron())
10-
#elif !defined(_MSC_VER)
11-
extern char** environ;
12-
#endif
13-
147
namespace node {
158
using v8::Array;
169
using v8::Boolean;
@@ -123,12 +116,6 @@ int32_t RealEnvStore::Query(Isolate* isolate, Local<String> property) const {
123116
Mutex::ScopedLock lock(per_process::env_var_mutex);
124117

125118
node::Utf8Value key(isolate, property);
126-
#ifdef _WIN32
127-
if (key[0] == L'=')
128-
return static_cast<int32_t>(v8::ReadOnly) |
129-
static_cast<int32_t>(v8::DontDelete) |
130-
static_cast<int32_t>(v8::DontEnum);
131-
#endif
132119

133120
char val[2];
134121
size_t init_sz = sizeof(val);
@@ -138,6 +125,14 @@ int32_t RealEnvStore::Query(Isolate* isolate, Local<String> property) const {
138125
return -1;
139126
}
140127

128+
#ifdef _WIN32
129+
if (key[0] == L'=') {
130+
return static_cast<int32_t>(v8::ReadOnly) |
131+
static_cast<int32_t>(v8::DontDelete) |
132+
static_cast<int32_t>(v8::DontEnum);
133+
}
134+
#endif
135+
141136
return 0;
142137
}
143138

@@ -151,54 +146,31 @@ void RealEnvStore::Delete(Isolate* isolate, Local<String> property) {
151146

152147
Local<Array> RealEnvStore::Enumerate(Isolate* isolate) const {
153148
Mutex::ScopedLock lock(per_process::env_var_mutex);
154-
#ifdef __POSIX__
155-
int env_size = 0;
156-
while (environ[env_size]) {
157-
env_size++;
158-
}
159-
std::vector<Local<Value>> env_v(env_size);
160-
161-
for (int i = 0; i < env_size; ++i) {
162-
const char* var = environ[i];
163-
const char* s = strchr(var, '=');
164-
const int length = s ? s - var : strlen(var);
165-
env_v[i] = String::NewFromUtf8(isolate, var, NewStringType::kNormal, length)
166-
.ToLocalChecked();
167-
}
168-
#else // _WIN32
169-
std::vector<Local<Value>> env_v;
170-
WCHAR* environment = GetEnvironmentStringsW();
171-
if (environment == nullptr)
172-
return Array::New(isolate); // This should not happen.
173-
WCHAR* p = environment;
174-
while (*p) {
175-
WCHAR* s;
176-
if (*p == L'=') {
177-
// If the key starts with '=' it is a hidden environment variable.
178-
p += wcslen(p) + 1;
179-
continue;
180-
} else {
181-
s = wcschr(p, L'=');
182-
}
183-
if (!s) {
184-
s = p + wcslen(p);
185-
}
186-
const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(p);
187-
const size_t two_byte_buffer_len = s - p;
188-
v8::MaybeLocal<String> rc = String::NewFromTwoByte(
189-
isolate, two_byte_buffer, NewStringType::kNormal, two_byte_buffer_len);
190-
if (rc.IsEmpty()) {
149+
uv_env_item_t* items;
150+
int count;
151+
152+
OnScopeLeave cleanup([&]() { uv_os_free_environ(items, count); });
153+
CHECK_EQ(uv_os_environ(&items, &count), 0);
154+
155+
MaybeStackBuffer<Local<Value>, 256> env_v(count);
156+
int env_v_index = 0;
157+
for (int i = 0; i < count; i++) {
158+
#ifdef _WIN32
159+
// If the key starts with '=' it is a hidden environment variable.
160+
// The '\0' check is a workaround for the bug behind
161+
// https://github.com/libuv/libuv/pull/2473 and can be removed later.
162+
if (items[i].name[0] == '=' || items[i].name[0] == '\0') continue;
163+
#endif
164+
MaybeLocal<String> str = String::NewFromUtf8(
165+
isolate, items[i].name, NewStringType::kNormal);
166+
if (str.IsEmpty()) {
191167
isolate->ThrowException(ERR_STRING_TOO_LONG(isolate));
192-
FreeEnvironmentStringsW(environment);
193168
return Local<Array>();
194169
}
195-
env_v.push_back(rc.ToLocalChecked());
196-
p = s + wcslen(s) + 1;
170+
env_v[env_v_index++] = str.ToLocalChecked();
197171
}
198-
FreeEnvironmentStringsW(environment);
199-
#endif
200172

201-
return Array::New(isolate, env_v.data(), env_v.size());
173+
return Array::New(isolate, env_v.out(), env_v_index);
202174
}
203175

204176
std::shared_ptr<KVStore> KVStore::Clone(v8::Isolate* isolate) const {

0 commit comments

Comments
 (0)