Explorar el Código

Optimize Enumerable::ToArray

Marek Safar hace 15 años
padre
commit
0bed23d764
Se han modificado 1 ficheros con 22 adiciones y 2 borrados
  1. 22 2
      mcs/class/System.Core/System.Linq/Enumerable.cs

+ 22 - 2
mcs/class/System.Core/System.Linq/Enumerable.cs

@@ -2791,14 +2791,34 @@ namespace System.Linq
 		{
 			Check.Source (source);
 
+			TSource[] array;
 			var collection = source as ICollection<TSource>;
 			if (collection != null) {
-				var array = new TSource [collection.Count];
+				if (collection.Count == 0)
+					return EmptyOf<TSource>.Instance;
+				
+				array = new TSource [collection.Count];
 				collection.CopyTo (array, 0);
 				return array;
 			}
+			
+			int pos = 0;
+			array = EmptyOf<TSource>.Instance;
+			foreach (var element in source) {
+				if (pos == array.Length) {
+					if (pos == 0)
+						array = new TSource [4];
+					else
+						Array.Resize (ref array, pos * 2);
+				}
 
-			return new List<TSource> (source).ToArray ();
+				array[pos++] = element;
+			}
+
+			if (pos != array.Length)
+				Array.Resize (ref array, pos);
+			
+			return array;
 		}
 
 		#endregion