This repository was archived by the owner on Jan 23, 2023. It is now read-only.
- Notifications
You must be signed in to change notification settings - Fork 4.9k
Enumerable.ToList() Optimization #3429
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
| @@ -529,7 +529,7 @@ public override IEnumerable<TResult2> Select<TResult2>(Func<TResult, TResult2> s | |
} | ||
| ||
| ||
internal sealed class SelectArrayIterator<TSource, TResult> : Iterator<TResult>, IArrayProvider<TResult> | ||
internal sealed class SelectArrayIterator<TSource, TResult> : Iterator<TResult>, IArrayProvider<TResult>, IListProvider<TResult> | ||
{ | ||
private readonly TSource[] _source; | ||
private readonly Func<TSource, TResult> _selector; | ||
| @@ -573,9 +573,20 @@ public TResult[] ToArray() | |
} | ||
return results; | ||
} | ||
| ||
public List<TResult> ToList() | ||
{ | ||
TSource[] source = _source; | ||
var results = new List<TResult>(source.Length); | ||
for (int i = 0; i < source.Length; i++) | ||
{ | ||
results.Add(_selector(source[i])); | ||
} | ||
return results; | ||
} | ||
} | ||
| ||
internal sealed class SelectListIterator<TSource, TResult> : Iterator<TResult>, IArrayProvider<TResult> | ||
internal sealed class SelectListIterator<TSource, TResult> : Iterator<TResult>, IArrayProvider<TResult>, IListProvider<TResult> | ||
{ | ||
private readonly List<TSource> _source; | ||
private readonly Func<TSource, TResult> _selector; | ||
| @@ -628,9 +639,20 @@ public TResult[] ToArray() | |
} | ||
return results; | ||
} | ||
| ||
public List<TResult> ToList() | ||
{ | ||
int count = _source.Count; | ||
var results = new List<TResult>(count); | ||
for (int i = 0; i < count; i++) | ||
{ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for (int i = 0, cnt = _source.Count; i < cnt; i++) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. (You probably want to move the setting of cnt to before constructing the list so you can use the cnt in the ctor call as well.) | ||
results.Add(_selector(_source[i])); | ||
} | ||
return results; | ||
} | ||
} | ||
| ||
internal sealed class SelectIListIterator<TSource, TResult> : Iterator<TResult>, IArrayProvider<TResult> | ||
internal sealed class SelectIListIterator<TSource, TResult> : Iterator<TResult>, IArrayProvider<TResult>, IListProvider<TResult> | ||
{ | ||
private readonly IList<TSource> _source; | ||
private readonly Func<TSource, TResult> _selector; | ||
| @@ -693,6 +715,17 @@ public TResult[] ToArray() | |
} | ||
return results; | ||
} | ||
| ||
public List<TResult> ToList() | ||
{ | ||
int count = _source.Count; | ||
var results = new List<TResult>(count); | ||
for (int i = 0; i < count; i++) | ||
{ | ||
results.Add(_selector(_source[i])); | ||
} | ||
return results; | ||
} | ||
} | ||
| ||
//public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate) { | ||
| @@ -1223,7 +1256,8 @@ public static TSource[] ToArray<TSource>(this IEnumerable<TSource> source) | |
public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source) | ||
{ | ||
if (source == null) throw Error.ArgumentNull("source"); | ||
return new List<TSource>(source); | ||
IListProvider<TSource> listProvider = source as IListProvider<TSource>; | ||
return listProvider != null ? listProvider.ToList() : new List<TSource>(source); | ||
} | ||
| ||
public static Dictionary<TKey, TSource> ToDictionary<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector) | ||
| @@ -1710,7 +1744,7 @@ public static IEnumerable<int> Range(int start, int count) | |
return new RangeIterator(start, count); | ||
} | ||
| ||
private sealed class RangeIterator : Iterator<int>, IArrayProvider<int> | ||
private sealed class RangeIterator : Iterator<int>, IArrayProvider<int>, IListProvider<int> | ||
{ | ||
private readonly int _start; | ||
private readonly int _end; | ||
| @@ -1760,6 +1794,17 @@ public int[] ToArray() | |
| ||
return array; | ||
} | ||
| ||
public List<int> ToList() | ||
{ | ||
List<int> list = new List<int>(_end - _start); | ||
for (int cur = _start; cur != _end; cur++) | ||
{ | ||
list.Add(cur); | ||
} | ||
| ||
return list; | ||
} | ||
} | ||
| ||
public static IEnumerable<TResult> Repeat<TResult>(TResult element, int count) | ||
| @@ -1768,7 +1813,7 @@ public static IEnumerable<TResult> Repeat<TResult>(TResult element, int count) | |
return new RepeatIterator<TResult>(element, count); | ||
} | ||
| ||
private sealed class RepeatIterator<TResult> : Iterator<TResult>, IArrayProvider<TResult> | ||
private sealed class RepeatIterator<TResult> : Iterator<TResult>, IArrayProvider<TResult>, IListProvider<TResult> | ||
{ | ||
private readonly int _count; | ||
private int _sent; | ||
| @@ -1811,6 +1856,14 @@ public TResult[] ToArray() | |
| ||
return array; | ||
} | ||
| ||
public List<TResult> ToList() | ||
{ | ||
List<TResult> list = new List<TResult>(_count); | ||
for (int i = 0; i != _count; ++i) list.Add(current); | ||
| ||
return list; | ||
} | ||
} | ||
| ||
public static IEnumerable<TResult> Empty<TResult>() | ||
| @@ -3137,6 +3190,18 @@ internal interface IArrayProvider<TElement> | |
TElement[] ToArray(); | ||
} | ||
| ||
/// <summary> | ||
/// An iterator that can produce a <see cref="List{TElement}"/> through an optimized path. | ||
/// </summary> | ||
internal interface IListProvider<TElement> | ||
{ | ||
/// <summary> | ||
/// Produce a <see cref="List{TElement}"/> of the sequence through an optimized path. | ||
/// </summary> | ||
/// <returns>The <see cref="List{TElement}"/>.</returns> | ||
List<TElement> ToList(); | ||
} | ||
| ||
internal class IdentityFunction<TElement> | ||
{ | ||
public static Func<TElement, TElement> Instance | ||
| @@ -3162,7 +3227,7 @@ public interface ILookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement> | |
bool Contains(TKey key); | ||
} | ||
| ||
public class Lookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>, ILookup<TKey, TElement>, IArrayProvider<IGrouping<TKey, TElement>> | ||
public class Lookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>, ILookup<TKey, TElement>, IArrayProvider<IGrouping<TKey, TElement>>, IListProvider<IGrouping<TKey, TElement>> | ||
{ | ||
private IEqualityComparer<TKey> _comparer; | ||
private Grouping<TKey, TElement>[] _groupings; | ||
| @@ -3250,6 +3315,21 @@ IGrouping<TKey, TElement>[] IArrayProvider<IGrouping<TKey, TElement>>.ToArray() | |
return array; | ||
} | ||
| ||
List<IGrouping<TKey, TElement>> IListProvider<IGrouping<TKey, TElement>>.ToList() | ||
{ | ||
List<IGrouping<TKey, TElement>> list = new List<IGrouping<TKey, TElement>>(_count); | ||
Grouping<TKey, TElement> g = _lastGrouping; | ||
if (g != null) | ||
{ | ||
do | ||
{ | ||
g = g.next; | ||
list.Add(g); | ||
} while (g != _lastGrouping); | ||
} | ||
return list; | ||
} | ||
| ||
public IEnumerable<TResult> ApplyResultSelector<TResult>(Func<TKey, IEnumerable<TElement>, TResult> resultSelector) | ||
{ | ||
Grouping<TKey, TElement> g = _lastGrouping; | ||
| @@ -3575,7 +3655,7 @@ IEnumerator IEnumerable.GetEnumerator() | |
} | ||
} | ||
| ||
internal class GroupedEnumerable<TSource, TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>, IArrayProvider<IGrouping<TKey, TElement>> | ||
internal class GroupedEnumerable<TSource, TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>, IArrayProvider<IGrouping<TKey, TElement>>, IListProvider<IGrouping<TKey, TElement>> | ||
{ | ||
private IEnumerable<TSource> _source; | ||
private Func<TSource, TKey> _keySelector; | ||
| @@ -3608,9 +3688,15 @@ public IGrouping<TKey, TElement>[] ToArray() | |
IArrayProvider<IGrouping<TKey, TElement>> lookup = Lookup<TKey, TElement>.Create<TSource>(_source, _keySelector, _elementSelector, _comparer); | ||
return lookup.ToArray(); | ||
} | ||
| ||
public List<IGrouping<TKey, TElement>> ToList() | ||
{ | ||
IListProvider<IGrouping<TKey, TElement>> lookup = Lookup<TKey, TElement>.Create<TSource>(_source, _keySelector, _elementSelector, _comparer); | ||
return lookup.ToList(); | ||
} | ||
} | ||
| ||
internal abstract class OrderedEnumerable<TElement> : IOrderedEnumerable<TElement>, IArrayProvider<TElement> | ||
internal abstract class OrderedEnumerable<TElement> : IOrderedEnumerable<TElement>, IArrayProvider<TElement>, IListProvider<TElement> | ||
{ | ||
internal IEnumerable<TElement> source; | ||
| ||
| @@ -3643,6 +3729,20 @@ public TElement[] ToArray() | |
return array; | ||
} | ||
| ||
public List<TElement> ToList() | ||
{ | ||
Buffer<TElement> buffer = new Buffer<TElement>(source); | ||
int count = buffer.count; | ||
List<TElement> list = new List<TElement>(count); | ||
if (count > 0) | ||
{ | ||
int[] map = SortedMap(buffer); | ||
for (int i = 0; i != count; i++) list.Add(buffer.items[map[i]]); | ||
} | ||
| ||
return list; | ||
} | ||
| ||
internal abstract EnumerableSorter<TElement> GetEnumerableSorter(EnumerableSorter<TElement> next); | ||
| ||
IEnumerator IEnumerable.GetEnumerator() | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You need to capture _source into a local here to let JIT optimize the range check. Otherwise JIT must assume that _source may change on every iteration and will not optimize.