Skip to content

Commit 344e221

Browse files
committed
os: cache homedir, remove getCheckedFunction
1 parent 95b8f5d commit 344e221

File tree

4 files changed

+128
-15
lines changed

4 files changed

+128
-15
lines changed

benchmark/os/homedir.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const homedir = require('os').homedir;
5+
const assert = require('assert');
6+
7+
const bench = common.createBenchmark(main, {
8+
n: [1e6],
9+
});
10+
11+
function main({ n }) {
12+
// Warm up.
13+
const length = 1024;
14+
const array = [];
15+
for (let i = 0; i < length; ++i) {
16+
array.push(homedir());
17+
}
18+
19+
bench.start();
20+
for (let i = 0; i < n; ++i) {
21+
const index = i % length;
22+
array[index] = homedir();
23+
}
24+
bench.end(n);
25+
26+
// Verify the entries to prevent dead code elimination from making
27+
// the benchmark invalid.
28+
for (let i = 0; i < length; ++i) {
29+
assert.strictEqual(typeof array[i], 'string');
30+
}
31+
}

benchmark/os/hostname.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const hostname = require('os').hostname;
5+
const assert = require('assert');
6+
7+
const bench = common.createBenchmark(main, {
8+
n: [1e6],
9+
});
10+
11+
function main({ n }) {
12+
// Warm up.
13+
const length = 1024;
14+
const array = [];
15+
for (let i = 0; i < length; ++i) {
16+
array.push(hostname());
17+
}
18+
19+
bench.start();
20+
for (let i = 0; i < n; ++i) {
21+
const index = i % length;
22+
array[index] = hostname();
23+
}
24+
bench.end(n);
25+
26+
// Verify the entries to prevent dead code elimination from making
27+
// the benchmark invalid.
28+
for (let i = 0; i < length; ++i) {
29+
assert.strictEqual(typeof array[i], 'string');
30+
}
31+
}

benchmark/os/uptime.js

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
'use strict';
2+
3+
const common = require('../common.js');
4+
const uptime = require('os').uptime;
5+
const assert = require('assert');
6+
7+
const bench = common.createBenchmark(main, {
8+
n: [1e5],
9+
});
10+
11+
function main({ n }) {
12+
// Warm up.
13+
const length = 1024;
14+
const array = [];
15+
for (let i = 0; i < length; ++i) {
16+
array.push(uptime());
17+
}
18+
19+
bench.start();
20+
for (let i = 0; i < n; ++i) {
21+
const index = i % length;
22+
array[index] = uptime();
23+
}
24+
bench.end(n);
25+
26+
// Verify the entries to prevent dead code elimination from making
27+
// the benchmark invalid.
28+
for (let i = 0; i < length; ++i) {
29+
assert.strictEqual(typeof array[i], 'number');
30+
}
31+
}

lib/os.js

+35-15
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,48 @@ const {
6060
setPriority: _setPriority,
6161
} = internalBinding('os');
6262

63-
function getCheckedFunction(fn) {
64-
return hideStackFrames(function checkError(...args) {
65-
const ctx = {};
66-
const ret = fn(...args, ctx);
67-
if (ret === undefined) {
68-
throw new ERR_SYSTEM_ERROR(ctx);
69-
}
70-
return ret;
71-
});
72-
}
73-
7463
const {
7564
0: type,
7665
1: version,
7766
2: release,
7867
3: machine,
7968
} = _getOSInformation();
8069

81-
const getHomeDirectory = getCheckedFunction(_getHomeDirectory);
82-
const getHostname = getCheckedFunction(_getHostname);
83-
const getInterfaceAddresses = getCheckedFunction(_getInterfaceAddresses);
84-
const getUptime = getCheckedFunction(_getUptime);
70+
const getHomeDirectory = hideStackFrames(() => {
71+
const ctx = {};
72+
const ret = _getHomeDirectory(ctx);
73+
if (ret === undefined) {
74+
throw new ERR_SYSTEM_ERROR(ctx);
75+
}
76+
return ret;
77+
});
78+
79+
const getHostname = hideStackFrames(() => {
80+
const ctx = {};
81+
const ret = _getHostname(ctx);
82+
if (ret === undefined) {
83+
throw new ERR_SYSTEM_ERROR(ctx);
84+
}
85+
return ret;
86+
});
87+
88+
const getInterfaceAddresses = hideStackFrames(() => {
89+
const ctx = {};
90+
const ret = _getInterfaceAddresses(ctx);
91+
if (ret === undefined) {
92+
throw new ERR_SYSTEM_ERROR(ctx);
93+
}
94+
return ret;
95+
});
96+
97+
const getUptime = hideStackFrames(() => {
98+
const ctx = {};
99+
const ret = _getUptime(ctx);
100+
if (ret === undefined) {
101+
throw new ERR_SYSTEM_ERROR(ctx);
102+
}
103+
return ret;
104+
});
85105

86106
/**
87107
* @returns {string}

0 commit comments

Comments
 (0)