AsyncEnumerableHelpers.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Licensed to the .NET Foundation under one or more agreements.
  2. // The .NET Foundation licenses this file to you under the Apache 2.0 License.
  3. // See the LICENSE file in the project root for more information.
  4. using System;
  5. using System.Collections.Generic;
  6. using System.Linq;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. namespace System.Collections.Generic
  10. {
  11. // Based on https://github.com/dotnet/corefx/blob/ec2685715b01d12f16b08d0dfa326649b12db8ec/src/Common/src/System/Collections/Generic/EnumerableHelpers.cs
  12. internal static class AsyncEnumerableHelpers
  13. {
  14. internal static async Task<T[]> ToArray<T>(IAsyncEnumerable<T> source, CancellationToken cancellationToken)
  15. {
  16. var result = await ToArrayWithLength(source, cancellationToken)
  17. .ConfigureAwait(false);
  18. Array.Resize(ref result.array, result.length);
  19. return result.array;
  20. }
  21. internal static async Task<ArrayWithLength<T>> ToArrayWithLength<T>(IAsyncEnumerable<T> source, CancellationToken cancellationToken)
  22. {
  23. var result = new ArrayWithLength<T>();
  24. // Check for short circuit optimizations. This one is very unlikely
  25. // but could be here as a group
  26. var ic = source as ICollection<T>;
  27. if (ic != null)
  28. {
  29. var count = ic.Count;
  30. if (count != 0)
  31. {
  32. // Allocate an array of the desired size, then copy the elements into it. Note that this has the same
  33. // issue regarding concurrency as other existing collections like List<T>. If the collection size
  34. // concurrently changes between the array allocation and the CopyTo, we could end up either getting an
  35. // exception from overrunning the array (if the size went up) or we could end up not filling as many
  36. // items as 'count' suggests (if the size went down). This is only an issue for concurrent collections
  37. // that implement ICollection<T>, which as of .NET 4.6 is just ConcurrentDictionary<TKey, TValue>.
  38. result.array = new T[count];
  39. ic.CopyTo(result.array, 0);
  40. result.length = count;
  41. return result;
  42. }
  43. }
  44. else
  45. {
  46. using (var en = source.GetEnumerator())
  47. {
  48. if (await en.MoveNext(cancellationToken)
  49. .ConfigureAwait(false))
  50. {
  51. const int DefaultCapacity = 4;
  52. var arr = new T[DefaultCapacity];
  53. arr[0] = en.Current;
  54. var count = 1;
  55. while (await en.MoveNext(cancellationToken)
  56. .ConfigureAwait(false))
  57. {
  58. if (count == arr.Length)
  59. {
  60. // MaxArrayLength is defined in Array.MaxArrayLength and in gchelpers in CoreCLR.
  61. // It represents the maximum number of elements that can be in an array where
  62. // the size of the element is greater than one byte; a separate, slightly larger constant,
  63. // is used when the size of the element is one.
  64. const int MaxArrayLength = 0x7FEFFFFF;
  65. // This is the same growth logic as in List<T>:
  66. // If the array is currently empty, we make it a default size. Otherwise, we attempt to
  67. // double the size of the array. Doubling will overflow once the size of the array reaches
  68. // 2^30, since doubling to 2^31 is 1 larger than Int32.MaxValue. In that case, we instead
  69. // constrain the length to be MaxArrayLength (this overflow check works because of the
  70. // cast to uint). Because a slightly larger constant is used when T is one byte in size, we
  71. // could then end up in a situation where arr.Length is MaxArrayLength or slightly larger, such
  72. // that we constrain newLength to be MaxArrayLength but the needed number of elements is actually
  73. // larger than that. For that case, we then ensure that the newLength is large enough to hold
  74. // the desired capacity. This does mean that in the very rare case where we've grown to such a
  75. // large size, each new element added after MaxArrayLength will end up doing a resize.
  76. var newLength = count << 1;
  77. if ((uint)newLength > MaxArrayLength)
  78. {
  79. newLength = MaxArrayLength <= count ? count + 1 : MaxArrayLength;
  80. }
  81. Array.Resize(ref arr, newLength);
  82. }
  83. arr[count++] = en.Current;
  84. }
  85. result.length = count;
  86. result.array = arr;
  87. return result;
  88. }
  89. }
  90. }
  91. result.length = 0;
  92. #if NO_ARRAY_EMPTY
  93. result.array = EmptyArray<T>.Value;
  94. #else
  95. result.array = Array.Empty<T>();
  96. #endif
  97. return result;
  98. }
  99. internal struct ArrayWithLength<T>
  100. {
  101. public T[] array;
  102. public int length;
  103. }
  104. }
  105. }