[linq] Use hashtable lookup instead of linear scan for groupby keys. Fixes #18673
authorMarek Safar <marek.safar@gmail.com>
Wed, 30 Apr 2014 09:53:43 +0000 (11:53 +0200)
committerMarek Safar <marek.safar@gmail.com>
Wed, 30 Apr 2014 09:55:17 +0000 (11:55 +0200)
mcs/class/System.Core/System.Linq/Enumerable.cs

index 23805636c6b6236d00dc7b20fce0feeb83ffd659..80b1e3fa33691c69dfa450388790d9c54b1b3ff9 100644 (file)
@@ -885,17 +885,6 @@ namespace System.Linq
 
                #region GroupBy
 
-               private static List<T> ContainsGroup<K, T> (
-                               Dictionary<K, List<T>> items, K key, IEqualityComparer<K> comparer)
-               {
-                       IEqualityComparer<K> comparerInUse = (comparer ?? EqualityComparer<K>.Default);
-                       foreach (KeyValuePair<K, List<T>> value in items) {
-                               if (comparerInUse.Equals (value.Key, key))
-                                       return value.Value;
-                       }
-                       return null;
-               }
-
                public static IEnumerable<IGrouping<TKey, TSource>> GroupBy<TSource, TKey> (this IEnumerable<TSource> source,
                        Func<TSource, TKey> keySelector)
                {
@@ -913,7 +902,7 @@ namespace System.Linq
                static IEnumerable<IGrouping<TKey, TSource>> CreateGroupByIterator<TSource, TKey> (this IEnumerable<TSource> source,
                        Func<TSource, TKey> keySelector, IEqualityComparer<TKey> comparer)
                {
-                       var groups = new Dictionary<TKey, List<TSource>> ();
+                       var groups = new Dictionary<TKey, List<TSource>> (comparer);
                        var nullList = new List<TSource> ();
                        int counter = 0;
                        int nullCounter = -1;
@@ -927,8 +916,8 @@ namespace System.Linq
                                                counter++;
                                        }
                                } else {
-                                       List<TSource> group = ContainsGroup<TKey, TSource> (groups, key, comparer);
-                                       if (group == null) {
+                                       List<TSource> group;
+                                       if (!groups.TryGetValue (key, out group)) {
                                                group = new List<TSource> ();
                                                groups.Add (key, group);
                                                counter++;
@@ -971,7 +960,7 @@ namespace System.Linq
                static IEnumerable<IGrouping<TKey, TElement>> CreateGroupByIterator<TSource, TKey, TElement> (this IEnumerable<TSource> source,
                        Func<TSource, TKey> keySelector, Func<TSource, TElement> elementSelector, IEqualityComparer<TKey> comparer)
                {
-                       var groups = new Dictionary<TKey, List<TElement>> ();
+                       var groups = new Dictionary<TKey, List<TElement>> (comparer);
                        var nullList = new List<TElement> ();
                        int counter = 0;
                        int nullCounter = -1;
@@ -986,8 +975,8 @@ namespace System.Linq
                                                counter++;
                                        }
                                } else {
-                                       List<TElement> group = ContainsGroup<TKey, TElement> (groups, key, comparer);
-                                       if (group == null) {
+                                       List<TElement> group;
+                                       if (!groups.TryGetValue (key, out group)) {
                                                group = new List<TElement> ();
                                                groups.Add (key, group);
                                                counter++;