Bug 1314861: Minor optimization: Avoid the more expensive parts of path joining and normalization where possible. r?ochameau
MozReview-Commit-ID: Jh71D8XulVp
--- a/addon-sdk/source/lib/toolkit/loader.js
+++ b/addon-sdk/source/lib/toolkit/loader.js
@@ -27,17 +27,17 @@ const { classes: Cc, Constructor: CC, in
results: Cr, manager: Cm } = Components;
const systemPrincipal = CC('@mozilla.org/systemprincipal;1', 'nsIPrincipal')();
const { loadSubScript } = Cc['@mozilla.org/moz/jssubscript-loader;1'].
getService(Ci.mozIJSSubScriptLoader);
const { addObserver, notifyObservers } = Cc['@mozilla.org/observer-service;1'].
getService(Ci.nsIObserverService);
const { XPCOMUtils } = Cu.import("resource://gre/modules/XPCOMUtils.jsm", {});
const { NetUtil } = Cu.import("resource://gre/modules/NetUtil.jsm", {});
-const { join: pathJoin, normalize, dirname } = Cu.import("resource://gre/modules/osfile/ospath_unix.jsm");
+const { normalize, dirname } = Cu.import("resource://gre/modules/osfile/ospath_unix.jsm");
XPCOMUtils.defineLazyServiceGetter(this, "resProto",
"@mozilla.org/network/protocol;1?name=resource",
"nsIResProtocolHandler");
XPCOMUtils.defineLazyServiceGetter(this, "zipCache",
"@mozilla.org/libjar/zip-reader-cache;1",
"nsIZipReaderCache");
@@ -383,20 +383,20 @@ function readURI(uri) {
}
// Combines all arguments into a resolved, normalized path
function join(base, ...paths) {
// If this is an absolute URL, we need to normalize only the path portion,
// or we wind up stripping too many slashes and producing invalid URLs.
let match = /^((?:resource|file|chrome)\:\/\/[^\/]*|jar:[^!]+!)(.*)/.exec(base);
if (match) {
- return match[1] + normalize(pathJoin(match[2], ...paths));
+ return match[1] + normalize([match[2], ...paths].join("/"));
}
- return normalize(pathJoin(base, ...paths));
+ return normalize([base, ...paths].join("/"));
}
Loader.join = join;
// Function takes set of options and returns a JS sandbox. Function may be
// passed set of options:
// - `name`: A string value which identifies the sandbox in about:memory. Will
// throw exception if omitted.
// - `principal`: String URI or `nsIPrincipal` for the sandbox. Defaults to
@@ -594,17 +594,21 @@ function normalizeExt(uri) {
// `requirer.uri` but in some cases it may be `baseURI`. In order to
// avoid complexity we require `baseURI` with a trailing `/`.
const resolve = iced(function resolve(id, base) {
if (!isRelative(id))
return id;
let baseDir = dirname(base);
- let resolved = join(baseDir, id);
+ let resolved;
+ if (baseDir.includes(":"))
+ resolved = join(baseDir, id);
+ else
+ resolved = normalize(`${baseDir}/${id}`);
// Joining and normalizing removes the './' from relative files.
// We need to ensure the resolution still has the root
if (base.startsWith('./'))
resolved = './' + resolved;
return resolved;
});
@@ -657,17 +661,17 @@ function resolveRelative(rootURI, module
// From `resolve` module
// https://github.com/substack/node-resolve/blob/master/lib/node-modules-paths.js
function* getNodeModulePaths(rootURI, start) {
let moduleDir = 'node_modules';
let parts = start.split('/');
while (parts.length) {
let leaf = parts.pop();
- let path = join(...parts, leaf, moduleDir);
+ let path = [...parts, leaf, moduleDir].join("/");
if (leaf !== moduleDir && urlCache.exists(join(rootURI, path))) {
yield path;
}
}
if (urlCache.exists(join(rootURI, moduleDir))) {
yield moduleDir;
}