Skip to content

Commit 8dedaa7

Browse files
authored
code cleanup (#85)
1 parent 043b548 commit 8dedaa7

File tree

4 files changed

+25
-19
lines changed

4 files changed

+25
-19
lines changed

BitFaster.Caching.Benchmarks/Lru/LruJustGetOrAdd.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ namespace BitFaster.Caching.Benchmarks
1616
[MemoryDiagnoser]
1717
public class LruJustGetOrAdd
1818
{
19-
private static readonly ConcurrentDictionary<int, int> dictionary = new ConcurrentDictionary<int, int>(8, 9, EqualityComparer<int>.Default);
19+
private static readonly ConcurrentDictionary<int, int> dictionary = new(8, 9, EqualityComparer<int>.Default);
2020

21-
private static readonly ClassicLru<int, int> classicLru = new ClassicLru<int, int>(8, 9, EqualityComparer<int>.Default);
22-
private static readonly ConcurrentLru<int, int> concurrentLru = new ConcurrentLru<int, int>(8, 9, EqualityComparer<int>.Default);
23-
private static readonly ConcurrentTLru<int, int> concurrentTlru = new ConcurrentTLru<int, int>(8, 9, EqualityComparer<int>.Default, TimeSpan.FromMinutes(10));
24-
private static readonly FastConcurrentLru<int, int> fastConcurrentLru = new FastConcurrentLru<int, int>(8, 9, EqualityComparer<int>.Default);
25-
private static readonly FastConcurrentTLru<int, int> fastConcurrentTLru = new FastConcurrentTLru<int, int>(8, 9, EqualityComparer<int>.Default, TimeSpan.FromMinutes(1));
21+
private static readonly ClassicLru<int, int> classicLru = new(8, 9, EqualityComparer<int>.Default);
22+
private static readonly ConcurrentLru<int, int> concurrentLru = new(8, 9, EqualityComparer<int>.Default);
23+
private static readonly ConcurrentTLru<int, int> concurrentTlru = new(8, 9, EqualityComparer<int>.Default, TimeSpan.FromMinutes(10));
24+
private static readonly FastConcurrentLru<int, int> fastConcurrentLru = new(8, 9, EqualityComparer<int>.Default);
25+
private static readonly FastConcurrentTLru<int, int> fastConcurrentTLru = new(8, 9, EqualityComparer<int>.Default, TimeSpan.FromMinutes(1));
2626

2727
private static readonly int key = 1;
2828
private static System.Runtime.Caching.MemoryCache memoryCache = System.Runtime.Caching.MemoryCache.Default;

BitFaster.Caching/Disposer.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,16 @@
77

88
namespace BitFaster.Caching
99
{
10+
/// <summary>
11+
/// A generic wrapper for object disposal. Enables JIT to inline/remove object disposal if statement reducing code size.
12+
/// </summary>
13+
/// <typeparam name="T">The type of object to dispose</typeparam>
1014
public static class Disposer<T>
1115
{
16+
/// <summary>
17+
/// Dispose value if it implements the IDisposable interface.
18+
/// </summary>
19+
/// <param name="value">The value to dispose.</param>
1220
[MethodImpl(MethodImplOptions.AggressiveInlining)]
1321
public static void Dispose(T value)
1422
{

BitFaster.Caching/Lru/ClassicLru.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public ClassicLru(int concurrencyLevel, int capacity, IEqualityComparer<K> compa
3535
{
3636
if (capacity < 3)
3737
{
38-
throw new ArgumentOutOfRangeException("Capacity must be greater than or equal to 3.");
38+
throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than or equal to 3.");
3939
}
4040

4141
if (comparer == null)
@@ -56,16 +56,15 @@ public bool TryGet(K key, out V value)
5656
{
5757
Interlocked.Increment(ref requestTotalCount);
5858

59-
LinkedListNode<LruItem> node;
60-
if (dictionary.TryGetValue(key, out node))
59+
if (dictionary.TryGetValue(key, out var node))
6160
{
6261
LockAndMoveToEnd(node);
6362
Interlocked.Increment(ref requestHitCount);
6463
value = node.Value.Value;
6564
return true;
6665
}
6766

68-
value = default(V);
67+
value = default;
6968
return false;
7069
}
7170

BitFaster.Caching/Lru/TemplateConcurrentLru.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -62,18 +62,18 @@ public TemplateConcurrentLru(
6262
{
6363
if (capacity < 3)
6464
{
65-
throw new ArgumentOutOfRangeException("Capacity must be greater than or equal to 3.");
65+
throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than or equal to 3.");
6666
}
6767

6868
if (comparer == null)
6969
{
7070
throw new ArgumentNullException(nameof(comparer));
7171
}
7272

73-
var queueCapacity = ComputeQueueCapacity(capacity);
74-
this.hotCapacity = queueCapacity.hot;
75-
this.warmCapacity = queueCapacity.warm;
76-
this.coldCapacity = queueCapacity.cold;
73+
var (hot, warm, cold) = ComputeQueueCapacity(capacity);
74+
this.hotCapacity = hot;
75+
this.warmCapacity = warm;
76+
this.coldCapacity = cold;
7777

7878
this.hotQueue = new ConcurrentQueue<I>();
7979
this.warmQueue = new ConcurrentQueue<I>();
@@ -98,13 +98,12 @@ public TemplateConcurrentLru(
9898
///<inheritdoc/>
9999
public bool TryGet(K key, out V value)
100100
{
101-
I item;
102-
if (dictionary.TryGetValue(key, out item))
101+
if (dictionary.TryGetValue(key, out var item))
103102
{
104103
return GetOrDiscard(item, out value);
105104
}
106105

107-
value = default(V);
106+
value = default;
108107
this.hitCounter.IncrementMiss();
109108
return false;
110109
}
@@ -118,7 +117,7 @@ private bool GetOrDiscard(I item, out V value)
118117
{
119118
this.Move(item, ItemDestination.Remove);
120119
this.hitCounter.IncrementMiss();
121-
value = default(V);
120+
value = default;
122121
return false;
123122
}
124123

0 commit comments

Comments
 (0)