Skip to content

Commit f26183c

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

File tree

4 files changed

+133
-15
lines changed

4 files changed

+133
-15
lines changed

benchmark/os/homedir.js

Lines changed: 31 additions & 0 deletions
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

Lines changed: 31 additions & 0 deletions
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

Lines changed: 31 additions & 0 deletions
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

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -60,28 +60,53 @@ 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+
let homedirCached = null;
71+
const getHomeDirectory = hideStackFrames(() => {
72+
if (homedirCached) {
73+
return homedirCached;
74+
}
75+
const ctx = {};
76+
const ret = _getHomeDirectory(ctx);
77+
if (ret === undefined) {
78+
throw new ERR_SYSTEM_ERROR(ctx);
79+
}
80+
homedirCached = ret;
81+
return ret;
82+
});
83+
84+
const getHostname = hideStackFrames(() => {
85+
const ctx = {};
86+
const ret = _getHostname(ctx);
87+
if (ret === undefined) {
88+
throw new ERR_SYSTEM_ERROR(ctx);
89+
}
90+
return ret;
91+
});
92+
93+
const getInterfaceAddresses = hideStackFrames(() => {
94+
const ctx = {};
95+
const ret = _getInterfaceAddresses(ctx);
96+
if (ret === undefined) {
97+
throw new ERR_SYSTEM_ERROR(ctx);
98+
}
99+
return ret;
100+
});
101+
102+
const getUptime = hideStackFrames(() => {
103+
const ctx = {};
104+
const ret = _getUptime(ctx);
105+
if (ret === undefined) {
106+
throw new ERR_SYSTEM_ERROR(ctx);
107+
}
108+
return ret;
109+
});
85110

86111
/**
87112
* @returns {string}

0 commit comments

Comments
 (0)