Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions BitFaster.Caching.Benchmarks/Lru/LruJustGetOrAdd.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ namespace BitFaster.Caching.Benchmarks
[MemoryDiagnoser]
public class LruJustGetOrAdd
{
private static readonly ConcurrentDictionary<int, int> dictionary = new ConcurrentDictionary<int, int>(8, 9, EqualityComparer<int>.Default);
private static readonly ConcurrentDictionary<int, int> dictionary = new(8, 9, EqualityComparer<int>.Default);

private static readonly ClassicLru<int, int> classicLru = new ClassicLru<int, int>(8, 9, EqualityComparer<int>.Default);
private static readonly ConcurrentLru<int, int> concurrentLru = new ConcurrentLru<int, int>(8, 9, EqualityComparer<int>.Default);
private static readonly ConcurrentTLru<int, int> concurrentTlru = new ConcurrentTLru<int, int>(8, 9, EqualityComparer<int>.Default, TimeSpan.FromMinutes(10));
private static readonly FastConcurrentLru<int, int> fastConcurrentLru = new FastConcurrentLru<int, int>(8, 9, EqualityComparer<int>.Default);
private static readonly FastConcurrentTLru<int, int> fastConcurrentTLru = new FastConcurrentTLru<int, int>(8, 9, EqualityComparer<int>.Default, TimeSpan.FromMinutes(1));
private static readonly ClassicLru<int, int> classicLru = new(8, 9, EqualityComparer<int>.Default);
private static readonly ConcurrentLru<int, int> concurrentLru = new(8, 9, EqualityComparer<int>.Default);
private static readonly ConcurrentTLru<int, int> concurrentTlru = new(8, 9, EqualityComparer<int>.Default, TimeSpan.FromMinutes(10));
private static readonly FastConcurrentLru<int, int> fastConcurrentLru = new(8, 9, EqualityComparer<int>.Default);
private static readonly FastConcurrentTLru<int, int> fastConcurrentTLru = new(8, 9, EqualityComparer<int>.Default, TimeSpan.FromMinutes(1));

private static readonly int key = 1;
private static System.Runtime.Caching.MemoryCache memoryCache = System.Runtime.Caching.MemoryCache.Default;
Expand Down
8 changes: 8 additions & 0 deletions BitFaster.Caching/Disposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@

namespace BitFaster.Caching
{
/// <summary>
/// A generic wrapper for object disposal. Enables JIT to inline/remove object disposal if statement reducing code size.
/// </summary>
/// <typeparam name="T">The type of object to dispose</typeparam>
public static class Disposer<T>
{
/// <summary>
/// Dispose value if it implements the IDisposable interface.
/// </summary>
/// <param name="value">The value to dispose.</param>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static void Dispose(T value)
{
Expand Down
7 changes: 3 additions & 4 deletions BitFaster.Caching/Lru/ClassicLru.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public ClassicLru(int concurrencyLevel, int capacity, IEqualityComparer<K> 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)
Expand All @@ -56,16 +56,15 @@ public bool TryGet(K key, out V value)
{
Interlocked.Increment(ref requestTotalCount);

LinkedListNode<LruItem> node;
if (dictionary.TryGetValue(key, out node))
if (dictionary.TryGetValue(key, out var node))
{
LockAndMoveToEnd(node);
Interlocked.Increment(ref requestHitCount);
value = node.Value.Value;
return true;
}

value = default(V);
value = default;
return false;
}

Expand Down
17 changes: 8 additions & 9 deletions BitFaster.Caching/Lru/TemplateConcurrentLru.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ 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)
{
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<I>();
this.warmQueue = new ConcurrentQueue<I>();
Expand All @@ -98,13 +98,12 @@ public TemplateConcurrentLru(
///<inheritdoc/>
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;
}
Expand All @@ -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;
}

Expand Down