Bug 835596: Speed up getAppByLocalId from O(n) to O(1) r=ferjm

This commit is contained in:
Fabrice Desré 2013-01-30 22:50:28 -08:00
parent 3b7eea0211
commit 07d0055f08
2 changed files with 25 additions and 3 deletions

View File

@ -33,6 +33,14 @@ this.DOMApplicationRegistry = {
// We need to prime the cache with the list of apps.
// XXX shoud we do this async and block callers if it's not yet there?
this.webapps = this.cpmm.sendSyncMessage("Webapps:GetList", { })[0];
// We need a fast mapping from localId -> app, so we add an index.
this.localIdIndex = { };
for (let id in this.webapps) {
let app = this.webapps[id];
this.localIdIndex[app.localId] = app;
}
Services.obs.addObserver(this, "xpcom-shutdown", false);
},
@ -51,8 +59,10 @@ this.DOMApplicationRegistry = {
switch (aMessage.name) {
case "Webapps:AddApp":
this.webapps[msg.id] = msg.app;
this.localIdIndex[msg.app.localId] = msg.app;
break;
case "Webapps:RemoveApp":
delete this.localIdIndex[this.webapps[msg.id].localId];
delete this.webapps[msg.id];
break;
}
@ -75,7 +85,13 @@ this.DOMApplicationRegistry = {
getAppByLocalId: function getAppByLocalId(aLocalId) {
debug("getAppByLocalId " + aLocalId);
return AppsUtils.getAppByLocalId(this.webapps, aLocalId);
let app = this.localIdIndex[aLocalId];
if (!app) {
debug("Ouch, No app!");
return null;
}
return AppsUtils.cloneAsMozIApplication(app);
},
getManifestURLByLocalId: function getManifestURLByLocalId(aLocalId) {

View File

@ -81,8 +81,14 @@ this.AppsUtils = {
aPermission);
return (perm === Ci.nsIPermissionManager.ALLOW_ACTION);
};
res.QueryInterface = XPCOMUtils.generateQI([Ci.mozIDOMApplication,
Ci.mozIApplication]);
res.QueryInterface = function(aIID) {
if (aIID.equals(Ci.mozIDOMApplication) ||
aIID.equals(Ci.mozIApplication) ||
aIID.equals(Ci.nsISupports))
return this;
throw Cr.NS_ERROR_NO_INTERFACE;
}
return res;
},