From ad54b2d53bbe83c309e853d890fceeebf21d00f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A3o=20Gottwald?= Date: Wed, 24 Jul 2013 09:38:45 +0200 Subject: [PATCH] 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 | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/netwerk/protocol/http/UserAgentOverrides.jsm b/netwerk/protocol/http/UserAgentOverrides.jsm index b1a21c7f8d57..d05f55693009 100644 --- a/netwerk/protocol/http/UserAgentOverrides.jsm +++ b/netwerk/protocol/http/UserAgentOverrides.jsm @@ -15,9 +15,11 @@ 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) @@ -54,14 +56,27 @@ this.UserAgentOverrides = { 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() { @@ -79,6 +94,7 @@ this.UserAgentOverrides = { function buildOverrides() { gOverrides = {}; + gOverrideForHostCache.clear(); if (!Services.prefs.getBoolPref(PREF_OVERRIDES_ENABLED)) return;