Skip to content

Commit

Permalink
node: improve accessor perf of process.env
Browse files Browse the repository at this point in the history
Set process.env array entries in JS.

PR-URL: nodejs#3780
Reviewed-By: Fedor Indutny <fedor@indutny.com>
  • Loading branch information
trevnorris authored and Fishrock123 committed Jan 6, 2016
1 parent 89f056b commit ecef817
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 13 deletions.
18 changes: 18 additions & 0 deletions benchmark/misc/bench-env.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';

const common = require('../common');

const bench = common.createBenchmark(main, {
n: [1e5],
});


function main(conf) {
const n = conf.n >>> 0;
bench.start();
for (var i = 0; i < n; i++) {
// Access every item in object to process values.
Object.keys(process.env);
}
bench.end(n);
}
43 changes: 30 additions & 13 deletions src/node.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2560,31 +2560,42 @@ static void EnvDeleter(Local<String> property,


static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
Isolate* isolate = info.GetIsolate();
Environment* env = Environment::GetCurrent(info);
Isolate* isolate = env->isolate();
Local<Context> ctx = env->context();
Local<Function> fn = env->push_values_to_array_function();
Local<Value> argv[NODE_PUSH_VAL_TO_ARRAY_MAX];
size_t idx = 0;

#ifdef __POSIX__
int size = 0;
while (environ[size])
size++;

Local<Array> envarr = Array::New(isolate, size);
Local<Array> envarr = Array::New(isolate);

for (int i = 0; i < size; ++i) {
const char* var = environ[i];
const char* s = strchr(var, '=');
const int length = s ? s - var : strlen(var);
Local<String> name = String::NewFromUtf8(isolate,
var,
String::kNormalString,
length);
envarr->Set(i, name);
argv[idx] = String::NewFromUtf8(isolate,
var,
String::kNormalString,
length);
if (++idx >= ARRAY_SIZE(argv)) {
fn->Call(ctx, envarr, idx, argv).ToLocalChecked();
idx = 0;
}
}
if (idx > 0) {
fn->Call(ctx, envarr, idx, argv).ToLocalChecked();
}
#else // _WIN32
WCHAR* environment = GetEnvironmentStringsW();
if (environment == nullptr)
return; // This should not happen.
Local<Array> envarr = Array::New(isolate);
WCHAR* p = environment;
int i = 0;
while (*p) {
WCHAR *s;
if (*p == L'=') {
Expand All @@ -2599,13 +2610,19 @@ static void EnvEnumerator(const PropertyCallbackInfo<Array>& info) {
}
const uint16_t* two_byte_buffer = reinterpret_cast<const uint16_t*>(p);
const size_t two_byte_buffer_len = s - p;
Local<String> value = String::NewFromTwoByte(isolate,
two_byte_buffer,
String::kNormalString,
two_byte_buffer_len);
envarr->Set(i++, value);
argv[idx] = String::NewFromTwoByte(isolate,
two_byte_buffer,
String::kNormalString,
two_byte_buffer_len);
if (++idx >= ARRAY_SIZE(argv)) {
fn->Call(ctx, envarr, idx, argv).ToLocalChecked();
idx = 0;
}
p = s + wcslen(s) + 1;
}
if (idx > 0) {
fn->Call(ctx, envarr, idx, argv).ToLocalChecked();
}
FreeEnvironmentStringsW(environment);
#endif

Expand Down

0 comments on commit ecef817

Please sign in to comment.