Skip to content

Commit 963553d

Browse files
ArrayPool Optimization and cleanup.
1 parent 0b7c7ac commit 963553d

10 files changed

Lines changed: 28 additions & 35 deletions

File tree

benchmarking/Program.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System;
77
using System.Collections.Generic;
88
using System.Diagnostics;
9-
using System.Linq;
109
using System.Threading.Tasks;
1110

1211
internal class Program

source/Extensions.Combinations.cs

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,10 @@ static IEnumerable<T[]> CombinationsCore<T>(IReadOnlyList<T> source, int length,
2323
if (count == 1) yield break;
2424
}
2525

26-
var pool = ArrayPool<int>.Shared;
27-
var indexes = pool.Rent(length);
26+
var pool = count > 128 ? ArrayPool<int>.Shared : null;
27+
var indexes = pool?.Rent(count) ?? new int[count];
2828
try
2929
{
30-
3130
for (var i = 0; i < length; i++) indexes[i] = 0;
3231

3332
var lastIndex = length - 1;
@@ -68,25 +67,25 @@ bool GetNext()
6867
}
6968
finally
7069
{
71-
pool.Return(indexes);
70+
pool?.Return(indexes);
7271
}
7372
}
7473

7574
static IEnumerable<T[]> CombinationsCore<T>(IReadOnlyList<T> source, int length, bool distinctSet)
7675
{
77-
var pool = ArrayPool<T>.Shared;
78-
var buffer = pool.Rent(length);
76+
var pool = length > 128 ? ArrayPool<T>.Shared : null;
77+
var buffer = pool?.Rent(length) ?? new T[length];
7978
try
8079
{
81-
foreach (var b in CombinationsCore(source, length,distinctSet, buffer))
80+
foreach (var b in CombinationsCore(source, length, distinctSet, buffer))
8281
yield return b;
8382
}
8483
finally
8584
{
86-
pool.Return(buffer, true);
85+
pool?.Return(buffer, true);
8786
}
8887
}
89-
88+
9089

9190
/// <inheritdoc cref="Combinations{T}(IEnumerable{T}, int)"/>
9291
/// <param name="buffer">A buffer that is filled with the values and returned as the yielded value instead of a new array.</param>
@@ -134,7 +133,7 @@ public static IEnumerable<T[]> Combinations<T>(this IEnumerable<T> elements, int
134133
if (count == 0) return Enumerable.Empty<T[]>();
135134

136135
if (uniqueOnly) return source.Subsets(length);
137-
return uniqueOnly ? source.Subsets(length) : CombinationsCore(source, length, true).Select(e=>e.AsCopy(length));
136+
return uniqueOnly ? source.Subsets(length) : CombinationsCore(source, length, true).Select(e => e.AsCopy(length));
138137
}
139138

140139
/// <inheritdoc cref="Combinations{T}(IEnumerable{T}, int)"/>
@@ -150,8 +149,8 @@ public static IEnumerable<ReadOnlyMemory<T>> CombinationsBuffered<T>(this IEnume
150149

151150
if (length == 0) yield break;
152151

153-
var pool = ArrayPool<T>.Shared;
154-
var buffer = pool.Rent(length);
152+
var pool = length > 128 ? ArrayPool<T>.Shared : null;
153+
var buffer = pool?.Rent(length) ?? new T[length];
155154
var readBuffer = new ReadOnlyMemory<T>(buffer, 0, length);
156155
try
157156
{
@@ -160,7 +159,7 @@ public static IEnumerable<ReadOnlyMemory<T>> CombinationsBuffered<T>(this IEnume
160159
}
161160
finally
162161
{
163-
pool.Return(buffer, true);
162+
pool?.Return(buffer, true);
164163
}
165164
}
166165

@@ -178,8 +177,8 @@ public static IEnumerable<ReadOnlyMemory<T>> CombinationsDistinctBuffered<T>(thi
178177

179178
if (length == 0) yield break;
180179

181-
var pool = ArrayPool<T>.Shared;
182-
var buffer = pool.Rent(length);
180+
var pool = length > 128 ? ArrayPool<T>.Shared : null;
181+
var buffer = pool?.Rent(length) ?? new T[length];
183182
var readBuffer = new ReadOnlyMemory<T>(buffer, 0, length);
184183
try
185184
{
@@ -188,7 +187,7 @@ public static IEnumerable<ReadOnlyMemory<T>> CombinationsDistinctBuffered<T>(thi
188187
}
189188
finally
190189
{
191-
pool.Return(buffer, true);
190+
pool?.Return(buffer, true);
192191
}
193192
}
194193

source/Extensions.Permutations.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,8 +77,8 @@ public static IEnumerable<ReadOnlyMemory<T>> PermutationsBuffered<T>(this IReadO
7777
var count = elements.Count;
7878
if (count == 0) yield break;
7979

80-
var pool = ArrayPool<T>.Shared;
81-
var buffer = pool.Rent(count);
80+
var pool = count > 128 ? ArrayPool<T>.Shared : null;
81+
var buffer = pool?.Rent(count) ?? new T[count];
8282
var readBuffer = new ReadOnlyMemory<T>(buffer, 0, count);
8383
try
8484
{
@@ -87,7 +87,7 @@ public static IEnumerable<ReadOnlyMemory<T>> PermutationsBuffered<T>(this IReadO
8787
}
8888
finally
8989
{
90-
pool.Return(buffer, true);
90+
pool?.Return(buffer, true);
9191
}
9292
}
9393

source/Extensions.Stream.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,8 @@ public static void CopyTo(this Stream source, Stream target, int bufferSize = 40
1616
if (target is null)
1717
throw new ArgumentNullException(nameof(target));
1818

19-
var pool = ArrayPool<byte>.Shared;
20-
var bytes = pool.Rent(bufferSize);
21-
19+
var pool = bufferSize > 128 ? ArrayPool<byte>.Shared : null;
20+
var bytes = pool?.Rent(bufferSize) ?? new byte[bufferSize];
2221
try
2322
{
2423
int cnt;
@@ -27,7 +26,7 @@ public static void CopyTo(this Stream source, Stream target, int bufferSize = 40
2726
}
2827
finally
2928
{
30-
pool.Return(bytes, clearBufferAfter);
29+
pool?.Return(bytes, clearBufferAfter);
3130
}
3231
}
3332
}

source/Extensions.Subsets.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static IEnumerable<T[]> Subsets<T>(this IReadOnlyList<T> source, int coun
3434
}
3535

3636
var diff = source.Count - count;
37-
var pool = count>128 ? ArrayPool<int>.Shared : null;
37+
var pool = count > 128 ? ArrayPool<int>.Shared : null;
3838
var indices = pool?.Rent(count) ?? new int[count];
3939
try
4040
{

source/LazyList.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Partly based on: http://www.fallingcanbedeadly.com/posts/crazy-extention-methods-tolazylist/
44
*/
55

6-
using Open.Disposable;
76
using Open.Threading;
87
using System;
98
using System.Collections.Generic;
@@ -44,7 +43,7 @@ protected override void OnDispose()
4443
/// <inheritdoc/>
4544
public override int IndexOf(T item)
4645
{
47-
const string MESSAGE = MARKED_ENDLESS+" Use an enumerator, then Take(x).IndexOf().";
46+
const string MESSAGE = MARKED_ENDLESS + " Use an enumerator, then Take(x).IndexOf().";
4847
if (IsEndless) throw new InvalidOperationException(MESSAGE);
4948

5049
return base.IndexOf(item);

source/Subsets.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,8 @@ public static IEnumerable<int[]> Indexes(int sourceLength, int subsetLength, int
6868
/// <remarks>Values are yielded as read only memory buffer that should not be retained as its array is returned to pool afterwards.</remarks>
6969
public static IEnumerable<ReadOnlyMemory<int>> IndexesBuffered(int sourceLength, int subsetLength)
7070
{
71-
var pool = ArrayPool<int>.Shared;
72-
var buffer = pool.Rent(subsetLength);
71+
var pool = subsetLength > 128 ? ArrayPool<int>.Shared : null;
72+
var buffer = pool?.Rent(subsetLength) ?? new int[subsetLength];
7373
var readBuffer = new ReadOnlyMemory<int>(buffer, 0, subsetLength);
7474
try
7575
{
@@ -78,7 +78,7 @@ public static IEnumerable<ReadOnlyMemory<int>> IndexesBuffered(int sourceLength,
7878
}
7979
finally
8080
{
81-
pool.Return(buffer, true);
81+
pool?.Return(buffer, true);
8282
}
8383
}
8484

source/Synchronized/ReadWriteSynchronizedCollectionWrapper.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public override Span<T> CopyTo(Span<T> span)
8484
using var read = Sync.ReadLock();
8585
return InternalSource.CopyToSpan(span);
8686
}
87-
87+
8888
#endregion
8989

9090
#region Dispose

testing/Open.Collections.Tests/LazyListTests.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Linq;
4-
using System.Text;
5-
using System.Threading.Tasks;
63
using Xunit;
74

85
namespace Open.Collections.Tests

testing/Open.Collections.Tests/SubsetTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public void TestSubset4_3()
129129
[InlineData(16, 4)]
130130
public void LargerProgressiveCheck(int size, int count)
131131
{
132-
132+
133133
var FullSet = Enumerable.Range(1, size).ToImmutableArray();
134134
var buffer = new int[count];
135135
var s1 = FullSet.Subsets(count, buffer).ToImmutableArray();

0 commit comments

Comments
 (0)