Skip to content

Commit a8b8b02

Browse files
author
Oren (electricessence)
committed
Updated for .NET Standard 2.1
Extended benchmarks.
1 parent cc94abd commit a8b8b02

File tree

8 files changed

+88
-24
lines changed

8 files changed

+88
-24
lines changed

benchmarking/Benchmarks/CollectionBenchmark.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class CollectionBenchmark<T> : BenchmarkBase<Func<ICollection<T>>>
99
{
1010
public CollectionBenchmark(uint size, uint repeat, Func<ICollection<T>> factory, Func<int, T> itemFactory) : base(size, repeat, factory)
1111
{
12-
_items = Enumerable.Range(0, (int)TestSize).Select(itemFactory).ToArray();
12+
_items = Enumerable.Range(0, (int)TestSize * 2).Select(itemFactory).ToArray();
1313
}
1414

1515
protected readonly T[] _items;
@@ -40,6 +40,17 @@ protected override IEnumerable<TimedResult> TestOnceInternal()
4040
}
4141
});
4242

43+
if (c is IList<T> list)
44+
{
45+
yield return TimedResult.Measure("IList<T> Read Access", () =>
46+
{
47+
for (var i = 0; i < TestSize; i += 2)
48+
{
49+
var _ = list[i];
50+
}
51+
});
52+
}
53+
4354
yield return TimedResult.Measure("Empty Backwards (.Remove(last))", () =>
4455
{
4556
for (var i = 0; i < TestSize; i++) c.Remove(_items[TestSize - i - 1]);

benchmarking/Benchmarks/CollectionParallelBenchmark.cs

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using System.Collections.Generic;
44
using System.Diagnostics.CodeAnalysis;
5+
using System.Linq;
56
using System.Threading.Tasks;
67

78
namespace Open.Collections
@@ -24,17 +25,56 @@ protected override IEnumerable<TimedResult> TestOnceInternal()
2425
Parallel.For(0, TestSize, i => c.Add(_items[i]));
2526
});
2627

28+
yield return TimedResult.Measure("Enumerate", () =>
29+
{
30+
// ReSharper disable once NotAccessedVariable
31+
var x = 0;
32+
// ReSharper disable once LoopCanBeConvertedToQuery
33+
foreach (var _ in c) { x++; }
34+
});
35+
2736
// It's obvious to note that you have to 'lock' a collection or acquire a 'snapshot' before enumerating.
28-
//yield return TimedResult.Measure("Enumerate", () =>
29-
//{
30-
// Parallel.ForEach(c, i => { });
31-
//});
37+
yield return TimedResult.Measure("Enumerate (In Parallel)", () =>
38+
{
39+
Parallel.ForEach(c, i => { });
40+
});
3241

3342
yield return TimedResult.Measure(".Contains(item) (In Parallel)", () =>
3443
{
35-
Parallel.For(0, TestSize, i => { var _ = c.Contains(_items[i]); });
44+
Parallel.For(0, TestSize * 2, i => { var _ = c.Contains(_items[i]); });
3645
});
3746

47+
if (c is IList<T> list)
48+
{
49+
yield return TimedResult.Measure("IList<T> Read Access", () =>
50+
{
51+
for (var i = 0; i < TestSize; i += 2)
52+
{
53+
var _ = list[i];
54+
}
55+
});
56+
57+
yield return TimedResult.Measure("IList<T> Read Access (In Parallel)", () =>
58+
{
59+
Parallel.For(0, (int)TestSize, i => { var _ = list[i]; });
60+
});
61+
}
62+
63+
if (c is ISynchronizedCollection<T> syncList)
64+
{
65+
yield return TimedResult.Measure(".Snapshot()", () =>
66+
{
67+
var _ = syncList.Snapshot();
68+
});
69+
}
70+
else
71+
{
72+
yield return TimedResult.Measure(".Snapshot()", () =>
73+
{
74+
var _ = c.ToArray();
75+
});
76+
}
77+
3878
yield return TimedResult.Measure("Empty Backwards (.Remove(last)) (In Parallel)", () =>
3979
{
4080
Parallel.For(0, TestSize, i => { c.Remove(_items[TestSize - i - 1]); });

source/Open.Collections.csproj

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ 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.6.0</Version>
19-
<AssemblyVersion>2.6.0.0</AssemblyVersion>
20-
<FileVersion>2.6.0.0</FileVersion>
18+
<Version>2.6.1</Version>
19+
<AssemblyVersion>2.6.1.0</AssemblyVersion>
20+
<FileVersion>2.6.1.0</FileVersion>
2121
<PackageReleaseNotes></PackageReleaseNotes>
2222
<PackageLicenseExpression>MIT</PackageLicenseExpression>
2323
</PropertyGroup>

source/Queue/IQueue.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
namespace Open.Collections
44
{
5-
[SuppressMessage("ReSharper", "UnusedMemberInSuper.Global")]
65
public interface IQueue<T>
76
{
87
/// <inheritdoc cref="System.Collections.Generic.Queue&lt;T&gt;"/>

source/Queue/Queue.Concurrent.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ public static partial class Queue
66
{
77
public class Concurrent<T> : ConcurrentQueue<T>, IQueue<T>
88
{
9+
#if NETSTANDARD2_0
910
/// <inheritdoc />
1011
public void Clear()
1112
{
1213
while (TryDequeue(out _)) { }
1314
}
15+
#endif
1416
}
1517
}
1618
}

source/Queue/Queue.Standard.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public Standard(IEnumerable<T> initial) : base(initial)
1616

1717
}
1818

19+
#if NETSTANDARD2_0
1920
/// <inheritdoc />
2021
public virtual bool TryDequeue(out T item)
2122
{
@@ -31,6 +32,17 @@ public virtual bool TryPeek(out T item)
3132
item = ok ? Peek() : default;
3233
return ok;
3334
}
35+
#else
36+
/// <inheritdoc />
37+
public new virtual bool TryDequeue(out T item)
38+
=> base.TryDequeue(out item);
39+
40+
/// <inheritdoc />
41+
public new virtual bool TryPeek(out T item)
42+
=> base.TryPeek(out item);
43+
#endif
44+
45+
3446
}
3547
}
3648
}

source/ReadOnlyCollectionWrapper.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System;
33
using System.Collections;
44
using System.Collections.Generic;
5-
using System.Threading;
65

76
namespace Open.Collections
87
{

source/Synchronized/Readme.md

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,25 @@
1-
# Synchronized Classes
1+
# Synchronized Collections
22

33
Keeping in mind that without an exclusive lock of some kind, the data can change from one operation to the next.
44

55
These classes are provided to help with simple operations that don't rely on lengthy exclusive locking.
66

77
Some methods are available that assist with exclusive access:
88

9-
* ```.IfContains(item, action)```
10-
* ```.IfNotContains(item, action)```
11-
* ```.Snapshot()```
12-
* ```.ForEach(action, useSnapshot)```
13-
* ```.Export(destination)```
14-
* ```.CopyTo(array, arrayIndex)```
15-
* ```.Modify(action)```
9+
* `.IfContains(item, action)`
10+
* `.IfNotContains(item, action)`
11+
* `.Snapshot()`
12+
* `.ForEach(action, useSnapshot)`
13+
* `.Export(destination)`
14+
* `.CopyTo(array, arrayIndex)`
15+
* `.Modify(action)`
1616

17-
A snapshot is simply a copy of the collection at a given moment that can then be operated on while the underlying collection can continute to mutate.
17+
A "snapshot" is simply a copy of the collection at a given moment that can then be operated on while the underlying collection can continue to mutate.
1818

1919
### Reads
2020

21-
Any operation that the collection cannot be changing (adding/removing) while executing. ie. ```.Contains(item)```
21+
Any operation that the collection cannot be changing (adding/removing) while executing.
22+
ie. ```.Contains(item)```
2223

2324
### Writes
2425

@@ -32,14 +33,14 @@ Best to use these classes when expecting a mix of read/write but with a majority
3233

3334
## Read-Write Synchronized
3435

35-
These classes use a ReaderWriterLockSlim to synchronize read-write access and can be faster in some specific cases.
36+
These classes use a `ReaderWriterLockSlim` to synchronize read-write access and can be faster in some specific cases.
3637

37-
Best to use these classes when expecting a mix of read/write but with a majority of reads.
38+
Best to use these classes when expecting a mix of read/write but with a majority of reads and typically of a smaller size.
3839

3940
## Benchmarks
4041

4142
*Important Observations:*
4243

4344
* Be sure to benchmark the 'Release' configuration as the difference in performance versus 'Debug' can be dramatic.
4445
* Observe results on different machines with different core counts.
45-
* As collection sizes increase, performance behavior changes. For example, for smaller collection sizes (~1000 or less) Read-Write synchronization wins most of the time. But larger than that, a 'lock' for writes begins to out perform.
46+
* As collection sizes increase, performance behavior changes. For example, for smaller collection sizes (~1000 or less) Read-Write synchronization wins most of the time. But larger than that, a 'lock' for writes begins to out perform.

0 commit comments

Comments
 (0)