summaryrefslogtreecommitdiff
path: root/lib/module.js
diff options
context:
space:
mode:
authorAnna Henningsen <anna@addaleax.net>2016-08-14 18:41:28 +0200
committerAnna Henningsen <anna@addaleax.net>2016-09-30 16:01:59 +0200
commitc084287a608efcd339d085ae1c48457b31e9920a (patch)
treea12c6319a5dd36e9932ab81e81bd4adddd271a2a /lib/module.js
parent7bc6aeac86e6ce09efba4b04190b7792fc72fded (diff)
downloadandroid-node-v8-c084287a608efcd339d085ae1c48457b31e9920a.tar.gz
android-node-v8-c084287a608efcd339d085ae1c48457b31e9920a.tar.bz2
android-node-v8-c084287a608efcd339d085ae1c48457b31e9920a.zip
fs,module: add module-loader-only realpath cache
Reintroduce a realpath cache with the same mechanisms which existed before b488b19eaf2b2e7a3ca5eccd2445e245847a5f76 (`fs: optimize realpath using uv_fs_realpath()`), but only for the synchronous version and with the cache being passed as a hidden option to make sure it is only used internally. The cache is hidden from userland applications because it has been decided that fully reintroducing as part of the public API might stand in the way of future optimizations. PR-URL: https://github.com/nodejs/node/pull/8100 Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com> Reviewed-By: James M Snell <jasnell@gmail.com>
Diffstat (limited to 'lib/module.js')
-rw-r--r--lib/module.js18
1 files changed, 16 insertions, 2 deletions
diff --git a/lib/module.js b/lib/module.js
index 96f1cfc42e..c61a27a9e3 100644
--- a/lib/module.js
+++ b/lib/module.js
@@ -109,6 +109,14 @@ function tryPackage(requestPath, exts, isMain) {
tryExtensions(path.resolve(filename, 'index'), exts, isMain);
}
+// In order to minimize unnecessary lstat() calls,
+// this cache is a list of known-real paths.
+// Set to an empty Map to reset.
+const realpathCache = new Map();
+
+const realpathCacheKey = fs.realpathCacheKey;
+delete fs.realpathCacheKey;
+
// check if the file exists and is not a directory
// if using --preserve-symlinks and isMain is false,
// keep symlinks intact, otherwise resolve to the
@@ -118,7 +126,13 @@ function tryFile(requestPath, isMain) {
if (preserveSymlinks && !isMain) {
return rc === 0 && path.resolve(requestPath);
}
- return rc === 0 && fs.realpathSync(requestPath);
+ return rc === 0 && toRealPath(requestPath);
+}
+
+function toRealPath(requestPath) {
+ return fs.realpathSync(requestPath, {
+ [realpathCacheKey]: realpathCache
+ });
}
// given a path check a the file exists with any of the set extensions
@@ -164,7 +178,7 @@ Module._findPath = function(request, paths, isMain) {
if (preserveSymlinks && !isMain) {
filename = path.resolve(basePath);
} else {
- filename = fs.realpathSync(basePath);
+ filename = toRealPath(basePath);
}
} else if (rc === 1) { // Directory.
if (exts === undefined)