Optimizations

Topics: Developer Forum, Project Management Forum
May 5, 2008 at 3:37 PM

I was browsing the code, and came across this in MergeSystem.Indexus.WinServiceCommon.Cache

		/// <summary>
		/// Retrive all key's
		/// </summary>
		/// <returns>returns a list with all key's as a <see cref="string"/> objects.</returns>
		public List<string> GetAllKeys()
		{
			Dictionary<string, byte[]> hd = null;
			lock (dict)
			{
				hd = new Dictionary<string, byte[]>(dict);
			}
			return new List<string>(hd.Keys);
		}

I am wondering why we need to create a whole new Dictionary. I am not sure how often this method is called. From what I can see it is called the Regex and GetAll keys actions. I think it could be optimized as follows.

		public List<string> GetAllKeys()
		{
		    List<string> keys;
		    lock (dict)
		    {
		        keys = new List<string>(hd.Keys);
		    }
		    return keys;
		}

This will create less temporary objects (the internal array to hold values in the dictionary) and put less pressure on the garbage collector. Thoughts?
Coordinator
May 9, 2008 at 12:11 AM
thanks for your detailed post, we will make some prototyping during the next few day's to evaluate what are the
differences between both and post the results. Did you do already any kind of tests? Do you have some results
you able to share?

regards,
roni