Skip to content

Commit 0263f01

Browse files
felixgery
authored andcommitted
Fix test-require-cache-without-stat.js
This path adds an additional cache to the module system for caching the location of previously required modules. Since it is embedded in the loop that iterates over all require.paths, this patch also handles the case where require.paths is being modified. The patch also cleans up some code around it. See: https://groups.google.com/forum/#!topic/nodejs-dev/QGGlrvLDHVs
1 parent f39fdf2 commit 0263f01

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

src/node.js

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@
166166
// modules in thier own context.
167167
Module._contextLoad = (+process.env['NODE_MODULE_CONTEXTS'] > 0);
168168
Module._cache = {};
169+
Module._pathCache = {};
169170
Module._extensions = {};
170171
Module._paths = [];
171172

@@ -216,22 +217,41 @@
216217
// given a path check a the file exists with any of the set extensions
217218
function tryExtensions(p, extension) {
218219
for (var i = 0, EL = exts.length; i < EL; i++) {
219-
f = tryFile(p + exts[i]);
220-
if (f) { return f; }
220+
var filename = tryFile(p + exts[i]);
221+
222+
if (filename) {
223+
return filename;
224+
}
221225
}
222226
return false;
223227
};
224228

229+
var cacheKey = JSON.stringify({request: request, paths: paths});
230+
if (Module._pathCache[cacheKey]) {
231+
return Module._pathCache[cacheKey];
232+
}
233+
225234
// For each path
226235
for (var i = 0, PL = paths.length; i < PL; i++) {
227-
var p = paths[i],
228-
// try to join the request to the path
229-
f = tryFile(path.resolve(p, request)) ||
230-
// try it with each of the extensions
231-
tryExtensions(path.resolve(p, request)) ||
232-
// try it with each of the extensions at "index"
233-
tryExtensions(path.resolve(p, request, 'index'));
234-
if (f) { return f; }
236+
var basePath = path.resolve(paths[i], request);
237+
238+
// try to join the request to the path
239+
var filename = tryFile(basePath);
240+
241+
if (!filename) {
242+
// try it with each of the extensions
243+
filename = tryExtensions(basePath)
244+
}
245+
246+
if (!filename) {
247+
// try it with each of the extensions at "index"
248+
filename = tryExtensions(path.resolve(basePath, 'index'))
249+
}
250+
251+
if (filename) {
252+
Module._pathCache[cacheKey] = filename;
253+
return filename;
254+
}
235255
}
236256
return false;
237257
}

test/simple/test-require-cache-without-stat.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,8 @@ fs.stat = function() {
2424
};
2525

2626
// Load the module 'a' and 'http' once. It should become cached.
27-
28-
var m = common.fixturesDir + '/a';
29-
require(m);
27+
require.paths.push(common.fixturesDir);
28+
require('a');
3029
require('http');
3130

3231
console.log("counterBefore = %d", counter);
@@ -35,7 +34,7 @@ var counterBefore = counter;
3534
// Now load the module a bunch of times.
3635
// stat should not be called.
3736
for (var i = 0; i < 100; i++) {
38-
require(m);
37+
require('a');
3938
}
4039

4140
// Do the same with a built-in module

0 commit comments

Comments
 (0)