Skip to content

Commit d068021

Browse files
authored
counter (#242)
1 parent 0bb038f commit d068021

File tree

6 files changed

+33
-40
lines changed

6 files changed

+33
-40
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,32 @@
11
using System.Threading.Tasks;
2-
using BitFaster.Caching.Concurrent;
2+
using BitFaster.Caching.Counters;
33
using FluentAssertions;
44
using Xunit;
55

6-
namespace BitFaster.Caching.UnitTests.Concurrent
6+
namespace BitFaster.Caching.UnitTests.Counters
77
{
88
public class StripedLongAdderTests
99
{
1010
[Fact]
1111
public void InitialValueIsZero()
1212
{
13-
new LongAdder().Sum().Should().Be(0);
13+
new Counter().Count().Should().Be(0);
1414
}
1515

1616
[Fact]
1717
public void WhenIncrementedOneIsAdded()
1818
{
19-
var adder = new LongAdder();
19+
var adder = new Counter();
2020

2121
adder.Increment();
2222

23-
adder.Sum().Should().Be(1);
23+
adder.Count().Should().Be(1);
2424
}
2525

2626
[Fact]
2727
public async Task WhenAddingConcurrentlySumIsCorrect()
2828
{
29-
var adder = new LongAdder();
29+
var adder = new Counter();
3030

3131
await Threaded.Run(4, () =>
3232
{
@@ -36,7 +36,7 @@ await Threaded.Run(4, () =>
3636
}
3737
});
3838

39-
adder.Sum().Should().Be(400000);
39+
adder.Count().Should().Be(400000);
4040
}
4141
}
4242
}

BitFaster.Caching/Concurrent/LongAdder.cs renamed to BitFaster.Caching/Counters/Counter.cs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,25 @@
22
* Written by Doug Lea with assistance from members of JCP JSR-166
33
* Expert Group and released to the public domain, as explained at
44
* http://creativecommons.org/publicdomain/zero/1.0/
5-
*
6-
* See
7-
* http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/65464a307408/src/java.base/share/classes/java/util/concurrent/atomic/LongAdder.java
85
*/
96

10-
namespace BitFaster.Caching.Concurrent
7+
namespace BitFaster.Caching.Counters
118
{
129
/// <summary>
1310
/// A thread-safe counter suitable for high throuhgput counting across many concurrent threads.
1411
/// </summary>
15-
public sealed class LongAdder : Striped64
12+
public sealed class Counter : Striped64
1613
{
1714
/// <summary>
18-
/// Creates a new LongAdder with an intial sum of zero.
15+
/// Creates a new Counter with an intial sum of zero.
1916
/// </summary>
20-
public LongAdder() { }
17+
public Counter() { }
2118

2219
/// <summary>
23-
/// Computes the current sum.
20+
/// Computes the current count.
2421
/// </summary>
2522
/// <returns>The current sum.</returns>
26-
public long Sum()
23+
public long Count()
2724
{
2825
var @as = this.Cells; Cell a;
2926
var sum = @base.VolatileRead();

BitFaster.Caching/Concurrent/PaddedLong.cs renamed to BitFaster.Caching/Counters/PaddedLong.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
using System.Runtime.InteropServices;
22
using System.Threading;
33

4-
namespace BitFaster.Caching.Concurrent
4+
namespace BitFaster.Caching.Counters
55
{
66
/// <summary>
77
/// A long value padded by the size of a CPU cache line to mitigate false sharing.

BitFaster.Caching/Concurrent/Striped64.cs renamed to BitFaster.Caching/Counters/Striped64.cs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,14 @@
11
using System;
22
using System.Diagnostics.CodeAnalysis;
3-
using System.Runtime.ConstrainedExecution;
43
using System.Threading;
54

65
/*
76
* Written by Doug Lea with assistance from members of JCP JSR-166
87
* Expert Group and released to the public domain, as explained at
98
* http://creativecommons.org/publicdomain/zero/1.0/
10-
*
11-
* See
12-
* http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/65464a307408/src/java.base/share/classes/java/util/concurrent/atomic/Striped64.java
139
*/
1410

15-
namespace BitFaster.Caching.Concurrent
11+
namespace BitFaster.Caching.Counters
1612
{
1713
/*
1814
* This class maintains a lazily-initialized table of atomically

BitFaster.Caching/Lfu/ConcurrentLfu.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
using System.Threading;
99
using System.Threading.Tasks;
1010
using BitFaster.Caching.Buffers;
11-
using BitFaster.Caching.Concurrent;
11+
using BitFaster.Caching.Counters;
1212
using BitFaster.Caching.Lru;
1313
using BitFaster.Caching.Scheduler;
1414

@@ -765,17 +765,17 @@ internal string Format()
765765
internal class CacheMetrics : ICacheMetrics
766766
{
767767
public long requestHitCount;
768-
public LongAdder requestMissCount = new LongAdder();
768+
public Counter requestMissCount = new Counter();
769769
public long updatedCount;
770770
public long evictedCount;
771771

772772
public double HitRatio => (double)requestHitCount / (double)Total;
773773

774-
public long Total => requestHitCount + requestMissCount.Sum();
774+
public long Total => requestHitCount + requestMissCount.Count();
775775

776776
public long Hits => requestHitCount;
777777

778-
public long Misses => requestMissCount.Sum();
778+
public long Misses => requestMissCount.Count();
779779

780780
public long Updated => updatedCount;
781781

BitFaster.Caching/Lru/TelemetryPolicy.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using System;
22
using System.Diagnostics;
3-
using BitFaster.Caching.Concurrent;
3+
using BitFaster.Caching.Counters;
44

55
namespace BitFaster.Caching.Lru
66
{
@@ -12,10 +12,10 @@ namespace BitFaster.Caching.Lru
1212
[DebuggerDisplay("Hit = {Hits}, Miss = {Misses}, Upd = {Updated}, Evict = {Evicted}")]
1313
public struct TelemetryPolicy<K, V> : ITelemetryPolicy<K, V>
1414
{
15-
private LongAdder hitCount;
16-
private LongAdder missCount;
17-
private LongAdder evictedCount;
18-
private LongAdder updatedCount;
15+
private Counter hitCount;
16+
private Counter missCount;
17+
private Counter evictedCount;
18+
private Counter updatedCount;
1919
private object eventSource;
2020

2121
///<inheritdoc/>
@@ -25,19 +25,19 @@ public struct TelemetryPolicy<K, V> : ITelemetryPolicy<K, V>
2525
public double HitRatio => Total == 0 ? 0 : (double)Hits / (double)Total;
2626

2727
///<inheritdoc/>
28-
public long Total => this.hitCount.Sum() + this.missCount.Sum();
28+
public long Total => this.hitCount.Count() + this.missCount.Count();
2929

3030
///<inheritdoc/>
31-
public long Hits => this.hitCount.Sum();
31+
public long Hits => this.hitCount.Count();
3232

3333
///<inheritdoc/>
34-
public long Misses => this.missCount.Sum();
34+
public long Misses => this.missCount.Count();
3535

3636
///<inheritdoc/>
37-
public long Evicted => this.evictedCount.Sum();
37+
public long Evicted => this.evictedCount.Count();
3838

3939
///<inheritdoc/>
40-
public long Updated => this.updatedCount.Sum();
40+
public long Updated => this.updatedCount.Count();
4141

4242
///<inheritdoc/>
4343
public void IncrementMiss()
@@ -72,10 +72,10 @@ public void OnItemUpdated(K key, V value)
7272
///<inheritdoc/>
7373
public void SetEventSource(object source)
7474
{
75-
this.hitCount = new LongAdder();
76-
this.missCount = new LongAdder();
77-
this.evictedCount = new LongAdder();
78-
this.updatedCount = new LongAdder();
75+
this.hitCount = new Counter();
76+
this.missCount = new Counter();
77+
this.evictedCount = new Counter();
78+
this.updatedCount = new Counter();
7979
this.eventSource = source;
8080
}
8181
}

0 commit comments

Comments
 (0)