Bug 896382 - Cache the override (or that there's no override) for each host to avoid doing the same iterative string matching for every HTTP request. r=hurley
authorDão Gottwald <dao@mozilla.com>
Wed, 24 Jul 2013 09:38:45 +0200
changeset 139713 b02a7e62805086a40da88725bdef8ebc7bfe9ccd
parent 139712 356034879b0bb87d07119bc7443dcde10f4d1a49
child 139714 6f21c0bcd747fdd2fbca0a9c4c37f80fa1f49224
push id31465
push userdgottwald@mozilla.com
push dateWed, 24 Jul 2013 07:39:37 +0000
treeherdermozilla-inbound@b02a7e628050 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewershurley
bugs896382
milestone25.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 896382 - Cache the override (or that there's no override) for each host to avoid doing the same iterative string matching for every HTTP request. r=hurley
netwerk/protocol/http/UserAgentOverrides.jsm
--- a/netwerk/protocol/http/UserAgentOverrides.jsm
+++ b/netwerk/protocol/http/UserAgentOverrides.jsm
@@ -10,19 +10,21 @@ const Ci = Components.interfaces;
 const Cc = Components.classes;
 
 Components.utils.import("resource://gre/modules/Services.jsm");
 
 const PREF_OVERRIDES_ENABLED = "general.useragent.site_specific_overrides";
 const DEFAULT_UA = Cc["@mozilla.org/network/protocol;1?name=http"]
                      .getService(Ci.nsIHttpProtocolHandler)
                      .userAgent;
+const MAX_OVERRIDE_FOR_HOST_CACHE_SIZE = 250;
 
 var gPrefBranch;
 var gOverrides;
+var gOverrideForHostCache = new Map;
 var gInitialized = false;
 var gOverrideFunctions = [
   function (aHttpChannel) UserAgentOverrides.getOverrideForURI(aHttpChannel.URI)
 ];
 
 this.UserAgentOverrides = {
   init: function uao_init() {
     if (gInitialized)
@@ -49,24 +51,37 @@ this.UserAgentOverrides = {
 
   getOverrideForURI: function uao_getOverrideForURI(aURI) {
     if (!gInitialized)
       return null;
     if (!(aURI instanceof Ci.nsIStandardURL))
       return null;
 
     let host = aURI.asciiHost;
+
+    let override = gOverrideForHostCache.get(host);
+    if (override !== undefined)
+      return override;
+
+    override = null;
+
     for (let domain in gOverrides) {
       if (host == domain ||
           host.endsWith("." + domain)) {
-        return gOverrides[domain];
+        override = gOverrides[domain];
+        break;
       }
     }
 
-    return null;
+    if (gOverrideForHostCache.size >= MAX_OVERRIDE_FOR_HOST_CACHE_SIZE) {
+      gOverrideForHostCache.clear();
+    }
+    gOverrideForHostCache.set(host, override);
+
+    return override;
   },
 
   uninit: function uao_uninit() {
     if (!gInitialized)
       return;
     gInitialized = false;
 
     gPrefBranch.removeObserver("", buildOverrides);
@@ -74,16 +89,17 @@ this.UserAgentOverrides = {
     Services.prefs.removeObserver(PREF_OVERRIDES_ENABLED, buildOverrides);
 
     Services.obs.removeObserver(HTTP_on_modify_request, "http-on-modify-request");
   }
 };
 
 function buildOverrides() {
   gOverrides = {};
+  gOverrideForHostCache.clear();
 
   if (!Services.prefs.getBoolPref(PREF_OVERRIDES_ENABLED))
     return;
 
   let domains = gPrefBranch.getChildList("");
 
   for (let domain of domains) {
     let override = gPrefBranch.getCharPref(domain);