A number of performance improvements in the loader.

Reviewed by me.
This commit is contained in:
Francisco Ryan Tolmasky I committed 2010-03-07 03:58:21 -08:00
1 parent 276e8b948d
commit 0881d86998
6 files changed
+226 -205

No files matched your search

+8 -13
View File
@@ -206,21 +206,19 @@ CFBundle.prototype.load = function(/*BOOL*/ shouldExecute)
this._loadStatus = CFBundleLoading | CFBundleLoadingInfoPlist;
var self = this;
var self = this,
bundleURL = this.bundleURL(),
parentURL = new CFURL("..", bundleURL);
var parentURL = new CFURL("..", this.bundleURL());
//???
if (parentURL.absoluteString() === this.bundleURL().absoluteString())
if (parentURL.absoluteString() === bundleURL.absoluteString())
parentURL = parentURL.schemeAndAuthority();
StaticResource.resolveResourceAtURL(parentURL, YES, function(aStaticResource)
{
var resourceName = self.bundleURL().absoluteURL().lastPathComponent();
var resourceName = bundleURL.absoluteURL().lastPathComponent();
self._staticResource = aStaticResource._children[resourceName] ||
new StaticResource(resourceName, aStaticResource, YES, NO);
new StaticResource(bundleURL, aStaticResource, YES, NO);
function onsuccess(/*Event*/ anEvent)
{
@@ -575,13 +573,12 @@ function decompileStaticFile(/*Bundle*/ aBundle, /*String*/ aString, /*String*/
var fileURL = new CFURL(text, bundleURL),
parent = StaticResource.resourceAtURL(new CFURL(".", fileURL), YES);
file = new StaticResource(fileURL.lastPathComponent(), parent, NO, YES);
file = new StaticResource(fileURL, parent, NO, YES);
}
else if (marker === MARKER_URI)
{
var URL = new CFURL(text, bundleURL),
mappedURL,
mappedURLString = stream.getString();
if (mappedURLString.indexOf("mhtml:") === 0)
@@ -596,8 +593,6 @@ function decompileStaticFile(/*Bundle*/ aBundle, /*String*/ aString, /*String*/
mappedURLString = firstPart + "?" + CFCacheBuster + lastPart;
}
mappedURL = new CFURL(mappedURLString);
}
CFURL.setMappedURLForURL(URL, new CFURL(mappedURLString));
@@ -605,7 +600,7 @@ function decompileStaticFile(/*Bundle*/ aBundle, /*String*/ aString, /*String*/
// The unresolved directories must not be bundles.
var parent = StaticResource.resourceAtURL(new CFURL(".", URL), YES);
new StaticResource(URL.lastPathComponent(), parent, NO, YES);
new StaticResource(URL, parent, NO, YES);
}
else if (marker === MARKER_TEXT)
+29 -17
View File
@@ -241,7 +241,10 @@ CFURL.prototype.schemeAndAuthority = function()
CFURL.prototype.absoluteString = function()
{
return this.absoluteURL().string();
if (this._absoluteString === undefined)
this._absoluteString = this.absoluteURL().string();
return this._absoluteString;
}
CFURL.prototype.toString = function()
@@ -300,7 +303,7 @@ function resolveURL(aURL)
// If this doesn't start with a "..", then we're simply appending to already standardized paths.
if (pathComponents.length && pathComponents[0] === "..")
standardizePathComponents(resolvedPathComponents);
standardizePathComponents(resolvedPathComponents, YES);
resolvedParts.pathComponents = resolvedPathComponents;
resolvedParts.path = pathFromPathComponents(resolvedPathComponents, pathComponents.length <= 0 || aURL.hasDirectoryPath());
@@ -312,7 +315,9 @@ function resolveURL(aURL)
resolvedURL._parts = resolvedParts;
resolvedURL._standardizedURL = resolvedURL;
resolvedURL._standardizedString = resolvedString;
resolvedURL._absoluteURL = resolvedURL;
resolvedURL._absoluteString = resolvedString;
return resolvedURL;
}
@@ -330,11 +335,12 @@ function pathFromPathComponents(/*Array*/ pathComponents, /*BOOL*/ isDirectoryPa
return path;
}
function standardizePathComponents(/*Array*/ pathComponents)
function standardizePathComponents(/*Array*/ pathComponents, /*BOOL*/ inPlace)
{
var index = 0,
resultIndex = 0,
count = pathComponents.length;
count = pathComponents.length,
result = inPlace ? pathComponents : [];
for (; index < count; ++index)
{
@@ -343,21 +349,23 @@ function standardizePathComponents(/*Array*/ pathComponents)
if (component === "" || component === ".")
continue;
if (component !== ".." || resultIndex === 0 || pathComponents[resultIndex - 1] === "..")
if (component !== ".." || resultIndex === 0 || result[resultIndex - 1] === "..")
{
//if (resultIndex !== index)
pathComponents[resultIndex] = component;
result[resultIndex] = component;
resultIndex++;
continue;
}
if (resultIndex > 0 && pathComponents[resultIndex - 1] !== "/")
if (resultIndex > 0 && result[resultIndex - 1] !== "/")
--resultIndex;
}
pathComponents.length = resultIndex;
result.length = resultIndex;
return result;
}
function URLStringFromParts(/*Object*/ parts)
@@ -402,9 +410,7 @@ CFURL.prototype.standardizedURL = function()
{
var parts = PARTS(this),
pathComponents = parts.pathComponents,
standardizedPathComponents = pathComponents.slice();
standardizePathComponents(standardizedPathComponents);
standardizedPathComponents = standardizePathComponents(pathComponents, NO);
var standardizedPath = pathFromPathComponents(standardizedPathComponents, this.hasDirectoryPath());
@@ -421,7 +427,7 @@ CFURL.prototype.standardizedURL = function()
var standardizedURL = new CFURL(URLStringFromParts(standardizedParts), this.baseURL());
standardizedURL._parts = standardizedParts;
standardizedURL._standardizedParts = standardizedURL;
standardizedURL._standardizedURL = standardizedURL;
this._standardizedURL = standardizedURL;
}
@@ -497,13 +503,19 @@ CFURL.prototype.fragment = function()
CFURL.prototype.lastPathComponent = function()
{
var pathComponents = this.pathComponents(),
pathComponentCount = pathComponents.length;
if (this._lastPathComponent === undefined)
{
var pathComponents = this.pathComponents(),
pathComponentCount = pathComponents.length;
if (!pathComponentCount)
return "";
if (!pathComponentCount)
this._lastPathComponent = "";
return pathComponents[pathComponentCount - 1];
else
this._lastPathComponent = pathComponents[pathComponentCount - 1];
}
return this._lastPathComponent;
}
CFURL.prototype.path = function()
+171 -164
View File
@@ -36,9 +36,14 @@ function Executable(/*String*/ aCode, /*Array*/ fileDependencies, /*CFURL|String
this._URL = makeAbsoluteURL(aURL || new CFURL("(Anonymous" + (AnonymousExecutableCount++) + ")"));
this._fileDependencies = fileDependencies;
this._fileDependencyLoadStatus = ExecutableUnloadedFileDependencies;
this._eventDispatcher = new EventDispatcher(this);
if (fileDependencies.length)
{
this._fileDependencyStatus = ExecutableUnloadedFileDependencies;
this._fileDependencyCallbacks = [];
}
else
this._fileDependencyStatus = ExecutableLoadedFileDependencies;
if (this._function)
return;
@@ -190,228 +195,230 @@ Executable.prototype.scope = function()
Executable.prototype.hasLoadedFileDependencies = function()
{
return this._fileDependencyLoadStatus === ExecutableLoadedFileDependencies;
return this._fileDependencyStatus === ExecutableLoadedFileDependencies;
}
var globalIteration = 0;
Executable.prototype.loadFileDependencies = function()
var fileDependencyLoadCount = 0,
fileDependencyExecutables = [],
fileDependencyMarkers = { };
Executable.prototype.loadFileDependencies = function(aCallback)
{
#if DEPENDENCY_LOGGING
CPLog("DEPENDENCY: initiated by " + this.scope());
#endif
if (this._fileDependencyLoadStatus !== ExecutableUnloadedFileDependencies)
return;
var status = this._fileDependencyStatus;
this._fileDependencyLoadStatus = ExecutableLoadingFileDependencies;
if (status === ExecutableLoadedFileDependencies)
return aCallback();
var searchedURLStrings = [{ }, { }],
fileExecutableSearches = new CFMutableDictionary(),
incompleteFileExecutableSearches = new CFMutableDictionary(),
loadingExecutables = { };
this._fileDependencyCallbacks.push(aCallback)
function searchForFileDependencies(/*Executable*/ anExecutable)
if (status === ExecutableUnloadedFileDependencies)
{
var executables = [anExecutable],
executableIndex = 0,
executableCount = executables.length;
if (fileDependencyLoadCount)
throw "Can't load";
for (; executableIndex < executableCount; ++executableIndex)
{
var executable = executables[executableIndex];
if (executable.hasLoadedFileDependencies())
continue;
var executableURLString = executable.URL().absoluteString();
loadingExecutables[executableURLString] = executable;
var referenceURL = new CFURL(".", executable.URL()),
fileDependencies = executable.fileDependencies(),
fileDependencyIndex = 0,
fileDependencyCount = fileDependencies.length;
for (; fileDependencyIndex < fileDependencyCount; ++fileDependencyIndex)
{
var fileDependency = fileDependencies[fileDependencyIndex],
isLocal = fileDependency.isLocal(),
URL = fileDependency.URL();
if (isLocal)
URL = new CFURL(URL, referenceURL);
var URLString = URL.absoluteString();
if (searchedURLStrings[isLocal ? 1 : 0][URLString])
continue;
searchedURLStrings[isLocal ? 1 : 0][URLString] = YES;
var fileExecutableSearch = new FileExecutableSearch(URL, isLocal),
fileExecutableSearchUID = fileExecutableSearch.UID();
if (fileExecutableSearches.containsKey(fileExecutableSearchUID))
continue;
fileExecutableSearches.setValueForKey(fileExecutableSearchUID, fileExecutableSearch);
if (fileExecutableSearch.isComplete())
{
executables.push(fileExecutableSearch.result());
++executableCount;
}
else
{
incompleteFileExecutableSearches.setValueForKey(fileExecutableSearchUID, fileExecutableSearch);
fileExecutableSearch.addEventListener("complete", function( anEvent)
{
var fileExecutableSearch = anEvent.fileExecutableSearch;
incompleteFileExecutableSearches.removeValueForKey(fileExecutableSearch.UID());
searchForFileDependencies(fileExecutableSearch.result());
});
}
}
}
if (incompleteFileExecutableSearches.count() > 0)
#if !DEPENDENCY_LOGGING
return;
#else
{
CPLog("DEPENDENCY: more dependencies: ");
CPLog(incompleteFileExecutableSearches.toString());
return;
}
CPLog("DEPENDENCY: Ended");
#endif
for (var URLString in loadingExecutables)
if (hasOwnProperty.call(loadingExecutables, URLString))
loadingExecutables[URLString]._fileDependencyLoadStatus = ExecutableLoadedFileDependencies;
for (var URLString in loadingExecutables)
if (hasOwnProperty.call(loadingExecutables, URLString))
{
var executable = loadingExecutables[URLString];
executable._eventDispatcher.dispatchEvent(
{
type:"dependenciesload",
executable:executable
});
}
loadFileDependenciesForExecutable(this);
}
searchForFileDependencies(this);
}
Executable.prototype.addEventListener = function(/*String*/ anEventName, /*Function*/ aListener)
function loadFileDependenciesForExecutable(/*Executable*/ anExecutable)
{
this._eventDispatcher.addEventListener(anEventName, aListener);
fileDependencyExecutables.push(anExecutable);
anExecutable._fileDependencyStatus = ExecutableLoadingFileDependencies;
var fileDependencies = anExecutable.fileDependencies(),
index = 0,
count = fileDependencies.length,
referenceURL = anExecutable.referenceURL(),
referenceURLString = referenceURL.absoluteString(),
fileExecutableSearcher = anExecutable.fileExecutableSearcher();
fileDependencyLoadCount += count;
for (; index < count; ++index)
{
var fileDependency = fileDependencies[index],
isQuoted = fileDependency.isLocal(),
URL = fileDependency.URL(),
marker = (isQuoted && (referenceURLString + " ") || "") + URL;
if (fileDependencyMarkers[marker])
{
if (--fileDependencyLoadCount === 0)
fileExecutableDependencyLoadFinished();
continue;
}
fileDependencyMarkers[marker] = YES;
fileExecutableSearcher(URL, isQuoted, fileExecutableSearchFinished);
}
}
Executable.prototype.removeEventListener = function(/*String*/ anEventName, /*Function*/ aListener)
function fileExecutableSearchFinished(/*FileExecutable*/ aFileExecutable)
{
this._eventDispatcher.removeEventListener(anEventName, aListener);
--fileDependencyLoadCount;
if (aFileExecutable._fileDependencyStatus === ExecutableUnloadedFileDependencies)
loadFileDependenciesForExecutable(aFileExecutable);
else if (fileDependencyLoadCount === 0)
fileExecutableDependencyLoadFinished();
}
function fileExecutableDependencyLoadFinished()
{
var index = 0,
count = fileDependencyExecutables.length;
for (; index < count; ++index)
fileDependencyExecutables[index]._fileDependencyStatus = ExecutableLoadedFileDependencies;
for (index = 0; index < count; ++index)
{
var executable = fileDependencyExecutables[index],
callbacks = executable._fileDependencyCallbacks,
callbackIndex = 0,
callbackCount = callbacks.length;
for (; callbackIndex < callbackCount; ++callbackIndex)
callbacks[callbackIndex]();
executable._fileDependencyCallbacks = [];
}
}
Executable.prototype.referenceURL = function()
{
if (this._referenceURL === undefined)
this._referenceURL = new CFURL(".", this.URL());
return this._referenceURL;
}
Executable.prototype.fileImporter = function()
{
return Executable.fileImporterForURL(new CFURL(".", this.URL()));
return Executable.fileImporterForURL(this.referenceURL());
}
Executable.prototype.fileExecuter = function()
{
return Executable.fileExecuterForURL(new CFURL(".", this.URL()));
return Executable.fileExecuterForURL(this.referenceURL());
}
var cachedFileExecutersForURLStrings = { };
Executable.prototype.fileExecutableSearcher = function()
{
return Executable.fileExecutableSearcherForURL(this.referenceURL());
}
var cachedFileExecuters = { };
Executable.fileExecuterForURL = function(/*CFURL|String*/ aURL)
{
var referenceURL = makeAbsoluteURL(aURL),
referenceURLString = referenceURL.absoluteString(),
cachedFileExecuter = cachedFileExecutersForURLStrings[referenceURLString];
cachedFileExecuter = cachedFileExecuters[referenceURLString];
if (!cachedFileExecuter)
{
cachedFileExecuter = function(/*CFURL*/ aURL, /*BOOL*/ isQuoted, /*BOOL*/ shouldForce)
{
aURL = new CFURL(aURL, isQuoted ? referenceURL : NULL);
Executable.fileExecutableSearcherForURL(referenceURL)(aURL, isQuoted,
function(/*FileExecutable*/ aFileExecutable)
{
if (!aFileExecutable.hasLoadedFileDependencies())
throw "No executable loaded for file at URL " + aURL;
var fileExecutableSearch = new FileExecutableSearch(aURL, isQuoted),
fileExecutable = fileExecutableSearch.result();
if (!fileExecutable.hasLoadedFileDependencies())
throw "No executable loaded for file at URL " + aURL;
fileExecutable.execute(shouldForce);
aFileExecutable.execute(shouldForce);
});
}
cachedFileExecutersForURLStrings[referenceURLString] = cachedFileExecuter;
cachedFileExecuters[referenceURLString] = cachedFileExecuter;
}
return cachedFileExecuter;
}
var cachedImportersForURLStrings = { };
var cachedFileImporters = { };
Executable.fileImporterForURL = function(/*CFURL|String*/ aURL)
{
var referenceURL = makeAbsoluteURL(aURL),
referenceURLString = referenceURL.absoluteString(),
cachedImporter = cachedImportersForURLStrings[referenceURLString];
cachedFileImporter = cachedFileImporters[referenceURLString];
if (!cachedImporter)
if (!cachedFileImporter)
{
cachedImporter = function(/*CFURL*/ aURL, /*BOOL*/ isQuoted, /*Function*/ aCallback)
cachedFileImporter = function(/*CFURL*/ aURL, /*BOOL*/ isQuoted, /*Function*/ aCallback)
{
// We make heavy use of URLs throughout this process, so cache them!
enableCFURLCaching();
aURL = new CFURL(aURL, isQuoted ? referenceURL : NULL);
var fileExecutableSearch = new FileExecutableSearch(aURL, isQuoted);
function searchComplete(/*FileExecutableSearch*/ aFileExecutableSearch)
Executable.fileExecutableSearcherForURL(referenceURL)(aURL, isQuoted,
function(/*FileExecutable*/ aFileExecutable)
{
var fileExecutable = aFileExecutableSearch.result(),
executeAndCallback = function ()
{
fileExecutable.execute();
// No more need to cache these.
disableCFURLCaching();
if (aCallback)
aCallback();
}
if (!fileExecutable.hasLoadedFileDependencies())
aFileExecutable.loadFileDependencies(function()
{
fileExecutable.addEventListener("dependenciesload", executeAndCallback);
fileExecutable.loadFileDependencies();
}
else
executeAndCallback();
}
aFileExecutable.execute();
if (fileExecutableSearch.isComplete())
searchComplete(fileExecutableSearch);
else
fileExecutableSearch.addEventListener("complete", function(/*Event*/ anEvent)
{
searchComplete(anEvent.fileExecutableSearch);
// No more need to cache these.
disableCFURLCaching();
if (aCallback)
aCallback();
});
});
}
cachedImportersForURLStrings[referenceURLString] = cachedImporter;
cachedFileImporters[referenceURLString] = cachedFileImporter;
}
return cachedImporter;
return cachedFileImporter;
}
var cachedFileExecutableSearchers = { },
cachedFileExecutableSearchResults = { };
Executable.fileExecutableSearcherForURL = function(/*CFURL*/ referenceURL)
{
var referenceURLString = referenceURL.absoluteString(),
cachedFileExecutableSearcher = cachedFileExecutableSearchers[referenceURLString],
cachedSearchResults = { };
if (!cachedFileExecutableSearcher)
{
cachedFileExecutableSearcher = function(/*CFURL*/ aURL, /*BOOL*/ isQuoted, /*Function*/ success)
{
var cacheUID = (isQuoted && referenceURL || "") + aURL,
cachedResult = cachedFileExecutableSearchResults[cacheUID];
if (cachedResult)
return completed(cachedResult);
var isAbsoluteURL = (aURL instanceof CFURL) && aURL.scheme();
if (isQuoted || isAbsoluteURL)
{
if (!isAbsoluteURL)
aURL = new CFURL(aURL, referenceURL);
StaticResource.resolveResourceAtURL(aURL, NO, completed);
}
else
StaticResource.resolveResourceAtURLSearchingIncludeURLs(aURL, completed);
function completed(/*StaticResource*/ aStaticResource)
{
if (!aStaticResource)
throw new Error("Could not load file at " + aURL);
cachedFileExecutableSearchResults[cacheUID] = aStaticResource;
success(new FileExecutable(aStaticResource.URL()));
}
};
cachedFileExecutableSearchers[referenceURLString] = cachedFileExecutableSearcher;
}
return cachedFileExecutableSearcher;
}
-1
View File
@@ -40,7 +40,6 @@
#include "FileDependency.js"
#include "Executable.js"
#include "FileExecutable.js"
#include "FileExecutableSearch.js"
#include "Runtime.js"
#if defined(DEBUG) || defined(COMMONJS)
#include "Debug.js"
+8 -2
View File
@@ -157,8 +157,10 @@ GLOBAL(class_addMethod) = function(/*Class*/ aClass, /*SEL*/ aName, /*IMP*/ anIm
aClass.method_list.push(method);
aClass.method_dtable[aName] = method;
#if DEBUG
// Give this function a "pretty" name for the console.
method.method_imp.displayName = METHOD_DISPLAY_NAME(aClass, method);
#endif
// FIXME: Should this be done here?
// If this is a root class...
@@ -186,8 +188,10 @@ GLOBAL(class_addMethods) = function(/*Class*/ aClass, /*Array*/ methods)
method_list.push(method);
method_dtable[method.name] = method;
#if DEBUG
// Give this function a "pretty" name for the console.
method.method_imp.displayName = METHOD_DISPLAY_NAME(aClass, method);
#endif
}
// If this is a root class...
@@ -455,8 +459,10 @@ GLOBAL(objj_msgSend) = function(/*id*/ aReceiver, /*SEL*/ aSelector)
{
if (aReceiver == nil)
return nil;
CLASS_GET_METHOD_IMPLEMENTATION(var implementation, aReceiver.isa, aSelector);
var isa = aReceiver.isa;
CLASS_GET_METHOD_IMPLEMENTATION(var implementation, isa, aSelector);
switch(arguments.length)
{
+10 -8
View File
@@ -1,26 +1,28 @@
var rootResources = { };
function StaticResource(/*String*/ aName, /*StaticResource*/ aParent, /*BOOL*/ isDirectory, /*BOOL*/ isResolved)
function StaticResource(/*CFURL*/ aURL, /*StaticResource*/ aParent, /*BOOL*/ isDirectory, /*BOOL*/ isResolved)
{
this._parent = aParent;
this._eventDispatcher = new EventDispatcher(this);
this._name = aName;
var name = aURL.absoluteURL().lastPathComponent() || aURL.schemeAndAuthority();
this._name = name;
this._URL = aURL; //new CFURL(aName, aParent && aParent.URL().asDirectoryPathURL());
this._isResolved = !!isResolved;
this._URL = new CFURL(aName, aParent && aParent.URL().asDirectoryPathURL());
if (isDirectory)
this._URL = this._URL.asDirectoryPathURL();
if (!aParent)
rootResources[aName] = this;
rootResources[name] = this;
this._isDirectory = !!isDirectory;
this._isNotFound = NO;
if (aParent)
aParent._children[aName] = this;
aParent._children[name] = this;
if (isDirectory)
this._children = { };
@@ -119,7 +121,7 @@ function rootResourceForAbsoluteURL(/*CFURL*/ anAbsoluteURL)
resource = rootResources[schemeAndAuthority];
if (!resource)
resource = new StaticResource(schemeAndAuthority, NULL, YES, YES);
resource = new StaticResource(new CFURL(schemeAndAuthority), NULL, YES, YES);
return resource;
}
@@ -141,7 +143,7 @@ StaticResource.resourceAtURL = function(/*CFURL|String*/ aURL, /*BOOL*/ resolveA
resource = resource._children[name];
else if (resolveAsDirectoriesIfNecessary)
resource = new StaticResource(name, resource, YES, YES);
resource = new StaticResource(new CFURL(name, resource.URL()), resource, YES, YES);
else
throw new Error("Static Resource at " + aURL + " is not resolved (\"" + name + "\")");
@@ -179,7 +181,7 @@ function resolveResourceComponents(/*StaticResource*/ aResource, /*BOOL*/ isDire
// If the child doesn't exist, create and resolve it.
if (!child)
{
child = new StaticResource(name, aResource, index + 1 < count || isDirectory , NO);
child = new StaticResource(new CFURL(name, aResource.URL()), aResource, index + 1 < count || isDirectory , NO);
child.resolve();
}