Skip to content
This repository was archived by the owner on May 27, 2019. It is now read-only.

Commit 8387613

Browse files
mscdexkevinoid
authored andcommitted
lib: improve module loading performance
This commit improves module loading performance by at least ~25-35% in the module-loader benchmarks. Some optimization strategies include: * Try-finally/try-catch isolation * Replacing regular expressions with manual parsing * Avoiding unnecessary string and array creation * Avoiding constant recompilation of anonymous functions and function definitions within functions PR-URL: nodejs/node#5172 Reviewed-By: James M Snell <[email protected]>
1 parent 0febd9a commit 8387613

File tree

1 file changed

+54
-39
lines changed

1 file changed

+54
-39
lines changed

fs-file-sync-fd.js

Lines changed: 54 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,42 @@ function isFd(path) {
3333
return (path >>> 0) === path;
3434
}
3535

36+
function tryStatSync(fd, isUserFd) {
37+
var threw = true;
38+
var st;
39+
try {
40+
st = fs.fstatSync(fd);
41+
threw = false;
42+
} finally {
43+
if (threw && !isUserFd) fs.closeSync(fd);
44+
}
45+
return st;
46+
}
47+
48+
function tryCreateBuffer(size, fd, isUserFd) {
49+
var threw = true;
50+
var buffer;
51+
try {
52+
buffer = Buffer.allocUnsafe(size);
53+
threw = false;
54+
} finally {
55+
if (threw && !isUserFd) fs.closeSync(fd);
56+
}
57+
return buffer;
58+
}
59+
60+
function tryReadSync(fd, isUserFd, buffer, pos, len) {
61+
var threw = true;
62+
var bytesRead;
63+
try {
64+
bytesRead = fs.readSync(fd, buffer, pos, len);
65+
threw = false;
66+
} finally {
67+
if (threw && !isUserFd) fs.closeSync(fd);
68+
}
69+
return bytesRead;
70+
}
71+
3672
fsFileSyncFD.readFileSync = function(path, options) {
3773
if (!options) {
3874
options = { encoding: null, flag: 'r' };
@@ -49,57 +85,36 @@ fsFileSyncFD.readFileSync = function(path, options) {
4985
var isUserFd = isFd(path); // file descriptor ownership
5086
var fd = isUserFd ? path : fs.openSync(path, flag, 438 /*=0o666*/);
5187

52-
var st;
53-
var size;
54-
var threw = true;
55-
try {
56-
st = fs.fstatSync(fd);
57-
size = st.isFile() ? st.size : 0;
58-
threw = false;
59-
} finally {
60-
if (threw && !isUserFd) fs.closeSync(fd);
61-
}
62-
88+
var st = tryStatSync(fd, isUserFd);
89+
var size = st.isFile() ? st.size : 0;
6390
var pos = 0;
6491
var buffer; // single buffer with file data
6592
var buffers; // list for when size is unknown
6693

6794
if (size === 0) {
6895
buffers = [];
6996
} else {
70-
threw = true;
71-
try {
72-
buffer = new Buffer(size);
73-
threw = false;
74-
} finally {
75-
if (threw && !isUserFd) fs.closeSync(fd);
76-
}
97+
buffer = tryCreateBuffer(size, fd, isUserFd);
7798
}
7899

79-
var done = false;
80100
var bytesRead;
81101

82-
while (!done) {
83-
threw = true;
84-
try {
85-
if (size !== 0) {
86-
bytesRead = fs.readSync(fd, buffer, pos, size - pos);
87-
} else {
88-
// the kernel lies about many files.
89-
// Go ahead and try to read some bytes.
90-
buffer = new Buffer(8192);
91-
bytesRead = fs.readSync(fd, buffer, 0, 8192);
92-
if (bytesRead) {
93-
buffers.push(buffer.slice(0, bytesRead));
94-
}
102+
if (size !== 0) {
103+
do {
104+
bytesRead = tryReadSync(fd, isUserFd, buffer, pos, size - pos);
105+
pos += bytesRead;
106+
} while (bytesRead !== 0 && pos < size);
107+
} else {
108+
do {
109+
// the kernel lies about many files.
110+
// Go ahead and try to read some bytes.
111+
buffer = Buffer.allocUnsafe(8192);
112+
bytesRead = tryReadSync(fd, isUserFd, buffer, 0, 8192);
113+
if (bytesRead !== 0) {
114+
buffers.push(buffer.slice(0, bytesRead));
95115
}
96-
threw = false;
97-
} finally {
98-
if (threw && !isUserFd) fs.closeSync(fd);
99-
}
100-
101-
pos += bytesRead;
102-
done = (bytesRead === 0) || (size !== 0 && pos >= size);
116+
pos += bytesRead;
117+
} while (bytesRead !== 0);
103118
}
104119

105120
if (!isUserFd)

0 commit comments

Comments
 (0)