Skip to content

Commit 99876ef

Browse files
author
Oren (electricessence)
committed
Tweaks to use array pool.
1 parent 68f2e83 commit 99876ef

File tree

7 files changed

+40
-49
lines changed

7 files changed

+40
-49
lines changed

benchmarking/Program.cs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -36,34 +36,30 @@ public static void Test1()
3636
{
3737
Console.WriteLine("Beginning LazyList concurrency test.");
3838
var sw = new Stopwatch();
39-
using (var list = new LazyList<TestEntry>(EndlessTest()))
39+
using var list = new LazyList<TestEntry>(EndlessTest());
40+
Parallel.For(0, 10000000, i =>
4041
{
41-
Parallel.For(0, 10000000, i =>
42-
{
43-
// ReSharper disable once AccessToDisposedClosure
44-
var e = list[i];
45-
if (e == null) throw new NullReferenceException();
46-
Debug.Assert(e.Value == i);
47-
});
48-
Console.WriteLine(sw.Elapsed);
49-
Debug.Assert(list.IndexOf(list[10000]) == 10000);
50-
}
42+
// ReSharper disable once AccessToDisposedClosure
43+
var e = list[i];
44+
if (e == null) throw new NullReferenceException();
45+
Debug.Assert(e.Value == i);
46+
});
47+
Console.WriteLine(sw.Elapsed);
48+
Debug.Assert(list.IndexOf(list[10000]) == 10000);
5149

5250
}
5351

5452
public static void Test2()
5553
{
5654
Console.WriteLine("Beginning LazyList.GetEnumerator() concurrency test.");
57-
using (var list = new LazyList<TestEntry>(EndlessTest(10000000)))
55+
using var list = new LazyList<TestEntry>(EndlessTest(10000000));
56+
var sw = new Stopwatch();
57+
Parallel.ForEach(list, e =>
5858
{
59-
var sw = new Stopwatch();
60-
Parallel.ForEach(list, e =>
61-
{
62-
if (e == null) throw new NullReferenceException();
63-
});
64-
Console.WriteLine(sw.Elapsed);
65-
Debug.Assert(list.IndexOf(list[10000]) == 10000);
66-
}
59+
if (e == null) throw new NullReferenceException();
60+
});
61+
Console.WriteLine(sw.Elapsed);
62+
Debug.Assert(list.IndexOf(list[10000]) == 10000);
6763

6864
}
6965

source/ConcurrentHashSet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
namespace Open.Collections
55
{
66
public class ConcurrentHashSet<T> : DictionaryToHashSetWrapper<T>
7-
{
7+
{
88
public ConcurrentHashSet(IEnumerable<T> intialValues = null)
9-
: base(new ConcurrentDictionary<T,bool>())
9+
: base(new ConcurrentDictionary<T, bool>())
1010
{
1111
if (intialValues != null)
1212
UnionWith(intialValues);

source/Extensions.Stream.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ public static void CopyTo(this Stream source, Stream target)
1515
if (target == null)
1616
throw new ArgumentNullException(nameof(target));
1717

18-
var bytes = new byte[4096];
19-
18+
var bytes = System.Buffers.ArrayPool<byte>.Shared.Rent(4096);
2019
int cnt;
2120
while ((cnt = source.Read(bytes, 0, bytes.Length)) != 0)
2221
target.Write(bytes, 0, cnt);
22+
System.Buffers.ArrayPool<byte>.Shared.Return(bytes, true);
2323
}
2424
}
2525
}

source/Extensions.cs

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -248,15 +248,13 @@ public static bool HasAtLeast<T>(this IEnumerable<T> source, int minimum)
248248
return collection.Count >= minimum;
249249
}
250250

251-
using (var e = source.GetEnumerator())
251+
using var e = source.GetEnumerator();
252+
while (e.MoveNext())
252253
{
253-
while (e.MoveNext())
254-
{
255-
if (--minimum == 0)
256-
return true;
257-
}
258-
return false;
254+
if (--minimum == 0)
255+
return true;
259256
}
257+
return false;
260258

261259
}
262260

@@ -300,7 +298,7 @@ static async Task PreCacheWorker<T>(IEnumerator<T> e, Channel<T> queue)
300298
{
301299
var value = e.Current;
302300

303-
retry:
301+
retry:
304302
if (queue.Writer.TryWrite(value)) continue;
305303
if (await queue.Writer.WaitToWriteAsync()) goto retry;
306304

@@ -419,7 +417,7 @@ public static string JoinToString<T>(this IEnumerable<T> source, char separator)
419417
Contract.EndContractBlock();
420418

421419
var sb = new StringBuilder();
422-
using(var enumerator = source.GetEnumerator())
420+
using (var enumerator = source.GetEnumerator())
423421
{
424422
if (enumerator.MoveNext())
425423
sb.Append(enumerator.Current);
@@ -847,14 +845,12 @@ public static IEnumerable<IEnumerable<T>> Combinations<T>(this IEnumerable<T> el
847845
}
848846
else
849847
{
850-
using (var el = elements.Memoize())
851-
{
852-
foreach (var combination in el.SelectMany((e, i) => el
853-
.Skip(i + (uniqueOnly ? 1 : 0))
854-
.Combinations(k - 1, uniqueOnly)
855-
.Select(c => (new[] { e }).Concat(c))))
856-
yield return combination;
857-
}
848+
using var el = elements.Memoize();
849+
foreach (var combination in el.SelectMany((e, i) => el
850+
.Skip(i + (uniqueOnly ? 1 : 0))
851+
.Combinations(k - 1, uniqueOnly)
852+
.Select(c => (new[] { e }).Concat(c))))
853+
yield return combination;
858854
}
859855

860856
}

source/Open.Collections.csproj

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,7 @@ Part of the "Open" set of libraries.</Description>
1515
<PackageProjectUrl>https://github.com/electricessence/Open.Collections/</PackageProjectUrl>
1616
<RepositoryUrl>https://github.com/electricessence/Open.Collections/</RepositoryUrl>
1717
<RepositoryType>git</RepositoryType>
18-
<Version>2.7.0</Version>
19-
<AssemblyVersion>2.7.0.0</AssemblyVersion>
20-
<FileVersion>2.7.0.0</FileVersion>
18+
<Version>2.7.1</Version>
2119
<PackageReleaseNotes></PackageReleaseNotes>
2220
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2321
</PropertyGroup>
@@ -41,6 +39,10 @@ Part of the "Open" set of libraries.</Description>
4139

4240
<ItemGroup>
4341
<PackageReference Include="Open.Threading" Version="1.5.3" />
44-
</ItemGroup>
42+
</ItemGroup>
43+
44+
<ItemGroup Condition=" '$(TargetFramework)' == 'netstandard2.0' ">
45+
<PackageReference Include="System.Buffers" Version="4.5.0" />
46+
</ItemGroup>
4547

4648
</Project>

source/Queue/IQueue.cs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System.Diagnostics.CodeAnalysis;
2-
3-
namespace Open.Collections
1+
namespace Open.Collections
42
{
53
public interface IQueue<T>
64
{

source/Synchronized/TrackedList.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
* Licensing: MIT https://github.com/electricessence/Genetic-Algorithm-Platform/blob/master/LICENSE.md
44
*/
55

6-
using Open.Disposable;
76
using Open.Threading;
87
using System;
98
using System.Collections;

0 commit comments

Comments
 (0)