From b51c82f4de0b48a16c02a0c191850093aa0c7937 Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Mon, 1 Nov 2021 18:39:59 -0700 Subject: [PATCH 1/2] cleanup --- .../Lru/LruJustGetOrAdd.cs | 12 ++++++------ BitFaster.Caching/Lru/ClassicLru.cs | 7 +++---- BitFaster.Caching/Lru/TemplateConcurrentLru.cs | 17 ++++++++--------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/BitFaster.Caching.Benchmarks/Lru/LruJustGetOrAdd.cs b/BitFaster.Caching.Benchmarks/Lru/LruJustGetOrAdd.cs index 17702484..7351bf92 100644 --- a/BitFaster.Caching.Benchmarks/Lru/LruJustGetOrAdd.cs +++ b/BitFaster.Caching.Benchmarks/Lru/LruJustGetOrAdd.cs @@ -16,13 +16,13 @@ namespace BitFaster.Caching.Benchmarks [MemoryDiagnoser] public class LruJustGetOrAdd { - private static readonly ConcurrentDictionary dictionary = new ConcurrentDictionary(8, 9, EqualityComparer.Default); + private static readonly ConcurrentDictionary dictionary = new(8, 9, EqualityComparer.Default); - private static readonly ClassicLru classicLru = new ClassicLru(8, 9, EqualityComparer.Default); - private static readonly ConcurrentLru concurrentLru = new ConcurrentLru(8, 9, EqualityComparer.Default); - private static readonly ConcurrentTLru concurrentTlru = new ConcurrentTLru(8, 9, EqualityComparer.Default, TimeSpan.FromMinutes(10)); - private static readonly FastConcurrentLru fastConcurrentLru = new FastConcurrentLru(8, 9, EqualityComparer.Default); - private static readonly FastConcurrentTLru fastConcurrentTLru = new FastConcurrentTLru(8, 9, EqualityComparer.Default, TimeSpan.FromMinutes(1)); + private static readonly ClassicLru classicLru = new(8, 9, EqualityComparer.Default); + private static readonly ConcurrentLru concurrentLru = new(8, 9, EqualityComparer.Default); + private static readonly ConcurrentTLru concurrentTlru = new(8, 9, EqualityComparer.Default, TimeSpan.FromMinutes(10)); + private static readonly FastConcurrentLru fastConcurrentLru = new(8, 9, EqualityComparer.Default); + private static readonly FastConcurrentTLru fastConcurrentTLru = new(8, 9, EqualityComparer.Default, TimeSpan.FromMinutes(1)); private static readonly int key = 1; private static System.Runtime.Caching.MemoryCache memoryCache = System.Runtime.Caching.MemoryCache.Default; diff --git a/BitFaster.Caching/Lru/ClassicLru.cs b/BitFaster.Caching/Lru/ClassicLru.cs index 2f1d90bb..c0f19156 100644 --- a/BitFaster.Caching/Lru/ClassicLru.cs +++ b/BitFaster.Caching/Lru/ClassicLru.cs @@ -35,7 +35,7 @@ public ClassicLru(int concurrencyLevel, int capacity, IEqualityComparer compa { if (capacity < 3) { - throw new ArgumentOutOfRangeException("Capacity must be greater than or equal to 3."); + throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than or equal to 3."); } if (comparer == null) @@ -56,8 +56,7 @@ public bool TryGet(K key, out V value) { Interlocked.Increment(ref requestTotalCount); - LinkedListNode node; - if (dictionary.TryGetValue(key, out node)) + if (dictionary.TryGetValue(key, out var node)) { LockAndMoveToEnd(node); Interlocked.Increment(ref requestHitCount); @@ -65,7 +64,7 @@ public bool TryGet(K key, out V value) return true; } - value = default(V); + value = default; return false; } diff --git a/BitFaster.Caching/Lru/TemplateConcurrentLru.cs b/BitFaster.Caching/Lru/TemplateConcurrentLru.cs index c91dc502..eb4a7861 100644 --- a/BitFaster.Caching/Lru/TemplateConcurrentLru.cs +++ b/BitFaster.Caching/Lru/TemplateConcurrentLru.cs @@ -62,7 +62,7 @@ public TemplateConcurrentLru( { if (capacity < 3) { - throw new ArgumentOutOfRangeException("Capacity must be greater than or equal to 3."); + throw new ArgumentOutOfRangeException(nameof(capacity), "Capacity must be greater than or equal to 3."); } if (comparer == null) @@ -70,10 +70,10 @@ public TemplateConcurrentLru( throw new ArgumentNullException(nameof(comparer)); } - var queueCapacity = ComputeQueueCapacity(capacity); - this.hotCapacity = queueCapacity.hot; - this.warmCapacity = queueCapacity.warm; - this.coldCapacity = queueCapacity.cold; + var (hot, warm, cold) = ComputeQueueCapacity(capacity); + this.hotCapacity = hot; + this.warmCapacity = warm; + this.coldCapacity = cold; this.hotQueue = new ConcurrentQueue(); this.warmQueue = new ConcurrentQueue(); @@ -98,13 +98,12 @@ public TemplateConcurrentLru( /// public bool TryGet(K key, out V value) { - I item; - if (dictionary.TryGetValue(key, out item)) + if (dictionary.TryGetValue(key, out var item)) { return GetOrDiscard(item, out value); } - value = default(V); + value = default; this.hitCounter.IncrementMiss(); return false; } @@ -118,7 +117,7 @@ private bool GetOrDiscard(I item, out V value) { this.Move(item, ItemDestination.Remove); this.hitCounter.IncrementMiss(); - value = default(V); + value = default; return false; } From d9a4aaf0896a6a5bf6c109021e8128ba4ffb5fde Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Mon, 1 Nov 2021 18:44:21 -0700 Subject: [PATCH 2/2] comment --- BitFaster.Caching/Disposer.cs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/BitFaster.Caching/Disposer.cs b/BitFaster.Caching/Disposer.cs index d78fe902..6cd42ce6 100644 --- a/BitFaster.Caching/Disposer.cs +++ b/BitFaster.Caching/Disposer.cs @@ -7,8 +7,16 @@ namespace BitFaster.Caching { + /// + /// A generic wrapper for object disposal. Enables JIT to inline/remove object disposal if statement reducing code size. + /// + /// The type of object to dispose public static class Disposer { + /// + /// Dispose value if it implements the IDisposable interface. + /// + /// The value to dispose. [MethodImpl(MethodImplOptions.AggressiveInlining)] public static void Dispose(T value) {