lib: improve method of function calling

Using a more "direct" method of function calling yields better
performance.

PR-URL: https://github.com/nodejs/node/pull/10789
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Michael Dawson <michael_dawson@ca.ibm.com>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
This commit is contained in:
Brian White 2017-01-13 05:16:46 -05:00
parent 403b89e72b
commit 28dc848e70
No known key found for this signature in database
GPG Key ID: 606D7358F94DA209
3 changed files with 10 additions and 11 deletions

View File

@ -8,23 +8,22 @@ exports = module.exports = {
exports.requireDepth = 0;
// Invoke with makeRequireFunction.call(module) where |module| is the
// Module object to use as the context for the require() function.
function makeRequireFunction() {
const Module = this.constructor;
const self = this;
// Invoke with makeRequireFunction(module) where |module| is the Module object
// to use as the context for the require() function.
function makeRequireFunction(mod) {
const Module = mod.constructor;
function require(path) {
try {
exports.requireDepth += 1;
return self.require(path);
return mod.require(path);
} finally {
exports.requireDepth -= 1;
}
}
function resolve(request) {
return Module._resolveFilename(request, self);
return Module._resolveFilename(request, mod);
}
require.resolve = resolve;

View File

@ -577,11 +577,11 @@ Module.prototype._compile = function(content, filename) {
}
}
var dirname = path.dirname(filename);
var require = internalModule.makeRequireFunction.call(this);
var args = [this.exports, require, this, filename, dirname];
var require = internalModule.makeRequireFunction(this);
var depth = internalModule.requireDepth;
if (depth === 0) stat.cache = new Map();
var result = compiledWrapper.apply(this.exports, args);
var result = compiledWrapper.call(this.exports, this.exports, require, this,
filename, dirname);
if (depth === 0) stat.cache = null;
return result;
};

View File

@ -714,7 +714,7 @@ REPLServer.prototype.createContext = function() {
const module = new Module('<repl>');
module.paths = Module._resolveLookupPaths('<repl>', parentModule)[1];
const require = internalModule.makeRequireFunction.call(module);
const require = internalModule.makeRequireFunction(module);
context.module = module;
context.require = require;