Browse Source

* WebConfigurationManager.cs: performance improvement, cached GetSection method

svn path=/trunk/mcs/; revision=96069
Vladimir Krasnov 18 years ago
parent
commit
bfbe00f669

+ 5 - 0
mcs/class/System.Web/System.Web.Configuration_2.0/ChangeLog

@@ -1,3 +1,8 @@
+2008-02-18  Vladimir Krasnov  <[email protected]>
+
+	* WebConfigurationManager.cs: performance improvement, cached
+	GetSection method
+
 2008-02-07  Vladimir Krasnov  <[email protected]>
 
 	* GlobalizationSection.cs: fixed GetSanitizedCulture, performance

+ 47 - 15
mcs/class/System.Web/System.Web.Configuration_2.0/WebConfigurationManager.cs

@@ -47,6 +47,7 @@ namespace System.Web.Configuration {
 #if !TARGET_J2EE
 		static IInternalConfigConfigurationFactory configFactory;
 		static Hashtable configurations = Hashtable.Synchronized (new Hashtable ());
+		static Hashtable sectionCache = new Hashtable (StringComparer.OrdinalIgnoreCase);
 #else
 		const string AppSettingsKey = "WebConfigurationManager.AppSettings";
 		static internal IInternalConfigConfigurationFactory configFactory
@@ -94,6 +95,23 @@ namespace System.Web.Configuration {
 				AppDomain.CurrentDomain.SetData("WebConfigurationManager.configurations.initialized", true);
 			}
 		}
+
+		static Hashtable sectionCache
+		{
+			get
+			{
+				Hashtable sectionCache = (Hashtable) AppDomain.CurrentDomain.GetData ("sectionCache");
+				if (sectionCache == null) {
+					sectionCache = new Hashtable (StringComparer.OrdinalIgnoreCase);
+					AppDomain.CurrentDomain.SetData ("sectionCache", sectionCache);
+				}
+				return sectionCache;
+			}
+			set
+			{
+				AppDomain.CurrentDomain.SetData ("sectionCache", value);
+			}
+		}
 #endif
 
 		static ArrayList extra_assemblies = null;
@@ -266,6 +284,10 @@ namespace System.Web.Configuration {
 
 		public static object GetSection (string sectionName, string path)
 		{
+			object cachedSection = sectionCache [GetSectionCacheKey (sectionName, path)];
+			if (cachedSection != null)
+				return cachedSection;
+
 			_Configuration c = OpenWebConfiguration (path);
 			ConfigurationSection section = c.GetSection (sectionName);
 
@@ -275,26 +297,17 @@ namespace System.Web.Configuration {
 #if TARGET_J2EE
 			object value = get_runtime_object.Invoke (section, new object [0]);
 			if (String.CompareOrdinal ("appSettings", sectionName) == 0) {
-				AppDomain appDomain = AppDomain.CurrentDomain;
-				Hashtable settingsCache = (Hashtable) appDomain.GetData (AppSettingsKey);
-				if (settingsCache == null) {
-					settingsCache = Hashtable.Synchronized (new Hashtable ());
-					appDomain.SetData (AppSettingsKey, settingsCache);
-				}
-
-				NameValueCollection collection = (NameValueCollection) settingsCache [c];
-
-				if (collection == null) {
-					collection = new KeyValueMergedCollection (HttpContext.Current, (NameValueCollection) value);
-					settingsCache [c] = collection;
-				}
-
+				NameValueCollection collection;
+				collection = new KeyValueMergedCollection (HttpContext.Current, (NameValueCollection) value);
 				value = collection;
 			}
 
+			AddSectionToCache (GetSectionCacheKey (sectionName, path), value);
 			return value;
 #else
-			return SettingsMappingManager.MapSection (get_runtime_object.Invoke (section, new object [0]));
+			object value = SettingsMappingManager.MapSection (get_runtime_object.Invoke (section, new object [0]));
+			AddSectionToCache (GetSectionCacheKey (sectionName, path), value);
+			return value;
 #endif
 		}
 
@@ -332,6 +345,25 @@ namespace System.Web.Configuration {
 		internal static IInternalConfigConfigurationFactory ConfigurationFactory {
 			get { return configFactory; }
 		}
+
+		static void AddSectionToCache (string key, object section)
+		{
+			if (sectionCache [key] != null)
+				return;
+
+			Hashtable tmpTable = (Hashtable) sectionCache.Clone ();
+			if (tmpTable [key] != null)
+				return;
+
+			tmpTable.Add (key, section);
+			sectionCache = tmpTable;
+		}
+
+		static string GetSectionCacheKey (string sectionName, string path)
+		{
+			return string.Concat (path, "/", sectionName);
+		}
+
 		
 #region stuff copied from WebConfigurationSettings
 #if TARGET_J2EE