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
13 changes: 8 additions & 5 deletions BitFaster.Caching/Lfu/ConcurrentLfuCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1033,12 +1033,15 @@ internal AlternateLookup(ConcurrentLfuCore<K, V, N, P> lfu)
Debug.Assert(lfu.dictionary.IsCompatibleKey<TAlternateKey, K, N>());
this.Lfu = lfu;
this.Alternate = lfu.dictionary.GetAlternateLookup<TAlternateKey>();
this.Comparer = lfu.dictionary.GetAlternateComparer<TAlternateKey, K, N>();
}

internal ConcurrentLfuCore<K, V, N, P> Lfu { get; }

internal ConcurrentDictionary<K, N>.AlternateLookup<TAlternateKey> Alternate { get; }

internal IAlternateEqualityComparer<TAlternateKey, K> Comparer { get; }

public bool TryGet(TAlternateKey key, [MaybeNullWhen(false)] out V value)
{
if (this.Alternate.TryGetValue(key, out var node))
Expand Down Expand Up @@ -1091,7 +1094,7 @@ public void AddOrUpdate(TAlternateKey key, V value)

if (!hasActualKey)
{
actualKey = this.Lfu.dictionary.GetAlternateComparer<TAlternateKey, K, N>().Create(key);
actualKey = this.Comparer.Create(key);
hasActualKey = true;
}

Expand All @@ -1111,7 +1114,7 @@ public V GetOrAdd(TAlternateKey key, Func<K, V> valueFactory)
return value;
}

K actualKey = this.Lfu.dictionary.GetAlternateComparer<TAlternateKey, K, N>().Create(key);
K actualKey = this.Comparer.Create(key);

value = valueFactory(actualKey);
if (this.Lfu.TryAdd(actualKey, value))
Expand All @@ -1130,7 +1133,7 @@ public V GetOrAdd<TArg>(TAlternateKey key, Func<K, TArg, V> valueFactory, TArg f
return value;
}

K actualKey = this.Lfu.dictionary.GetAlternateComparer<TAlternateKey, K, N>().Create(key);
K actualKey = this.Comparer.Create(key);

value = valueFactory(actualKey, factoryArgument);
if (this.Lfu.TryAdd(actualKey, value))
Expand All @@ -1147,7 +1150,7 @@ public ValueTask<V> GetOrAddAsync(TAlternateKey key, Func<K, Task<V>> valueFacto
return new ValueTask<V>(value);
}

K actualKey = this.Lfu.dictionary.GetAlternateComparer<TAlternateKey, K, N>().Create(key);
K actualKey = this.Comparer.Create(key);
Task<V> task = valueFactory(actualKey);

return GetOrAddAsyncSlow(actualKey, task);
Expand All @@ -1160,7 +1163,7 @@ public ValueTask<V> GetOrAddAsync<TArg>(TAlternateKey key, Func<K, TArg, Task<V>
return new ValueTask<V>(value);
}

K actualKey = this.Lfu.dictionary.GetAlternateComparer<TAlternateKey, K, N>().Create(key);
K actualKey = this.Comparer.Create(key);
Task<V> task = valueFactory(actualKey, factoryArgument);

return GetOrAddAsyncSlow(actualKey, task);
Expand Down
12 changes: 7 additions & 5 deletions BitFaster.Caching/Lru/ClassicLru.cs
Original file line number Diff line number Diff line change
Expand Up @@ -536,10 +536,12 @@ internal AlternateLookup(ClassicLru<K, V> lru)
Debug.Assert(lru.dictionary.IsCompatibleKey<TAlternateKey, K, LinkedListNode<LruItem>>());
this.lru = lru;
this.alternate = lru.dictionary.GetAlternateLookup<TAlternateKey>();
this.comparer = lru.dictionary.GetAlternateComparer<TAlternateKey, K, LinkedListNode<LruItem>>();
}

private readonly ClassicLru<K, V> lru;
private readonly ConcurrentDictionary<K, LinkedListNode<LruItem>>.AlternateLookup<TAlternateKey> alternate;
private readonly IAlternateEqualityComparer<TAlternateKey, K> comparer;

public bool TryGet(TAlternateKey key, [MaybeNullWhen(false)] out V value)
{
Expand Down Expand Up @@ -590,7 +592,7 @@ public void AddOrUpdate(TAlternateKey key, V value)

if (!hasActualKey)
{
actualKey = this.lru.dictionary.GetAlternateComparer<TAlternateKey, K, LinkedListNode<LruItem>>().Create(key);
actualKey = this.comparer.Create(key);
hasActualKey = true;
}

Expand All @@ -610,7 +612,7 @@ public V GetOrAdd(TAlternateKey key, Func<K, V> valueFactory)
return value;
}

K actualKey = this.lru.dictionary.GetAlternateComparer<TAlternateKey, K, LinkedListNode<LruItem>>().Create(key);
K actualKey = this.comparer.Create(key);

value = valueFactory(actualKey);
if (this.lru.TryAdd(actualKey, value))
Expand All @@ -629,7 +631,7 @@ public V GetOrAdd<TArg>(TAlternateKey key, Func<K, TArg, V> valueFactory, TArg f
return value;
}

K actualKey = this.lru.dictionary.GetAlternateComparer<TAlternateKey, K, LinkedListNode<LruItem>>().Create(key);
K actualKey = this.comparer.Create(key);

value = valueFactory(actualKey, factoryArgument);
if (this.lru.TryAdd(actualKey, value))
Expand All @@ -646,7 +648,7 @@ public ValueTask<V> GetOrAddAsync(TAlternateKey key, Func<K, Task<V>> valueFacto
return new ValueTask<V>(value);
}

K actualKey = this.lru.dictionary.GetAlternateComparer<TAlternateKey, K, LinkedListNode<LruItem>>().Create(key);
K actualKey = this.comparer.Create(key);
Task<V> task = valueFactory(actualKey);

return GetOrAddAsyncSlow(actualKey, task);
Expand All @@ -659,7 +661,7 @@ public ValueTask<V> GetOrAddAsync<TArg>(TAlternateKey key, Func<K, TArg, Task<V>
return new ValueTask<V>(value);
}

K actualKey = this.lru.dictionary.GetAlternateComparer<TAlternateKey, K, LinkedListNode<LruItem>>().Create(key);
K actualKey = this.comparer.Create(key);
Task<V> task = valueFactory(actualKey, factoryArgument);

return GetOrAddAsyncSlow(actualKey, task);
Expand Down
13 changes: 8 additions & 5 deletions BitFaster.Caching/Lru/ConcurrentLruCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -967,12 +967,15 @@ internal AlternateLookup(ConcurrentLruCore<K, V, I, P, T> lru)
Debug.Assert(lru.dictionary.IsCompatibleKey<TAlternateKey, K, I>());
this.Lru = lru;
this.Alternate = lru.dictionary.GetAlternateLookup<TAlternateKey>();
this.Comparer = lru.dictionary.GetAlternateComparer<TAlternateKey, K, I>();
}

internal ConcurrentLruCore<K, V, I, P, T> Lru { get; }

internal ConcurrentDictionary<K, I>.AlternateLookup<TAlternateKey> Alternate { get; }

internal IAlternateEqualityComparer<TAlternateKey, K> Comparer { get; }

public bool TryGet(TAlternateKey key, [MaybeNullWhen(false)] out V value)
{
if (this.Alternate.TryGetValue(key, out var item))
Expand Down Expand Up @@ -1023,7 +1026,7 @@ public void AddOrUpdate(TAlternateKey key, V value)

if (!hasActualKey)
{
actualKey = this.Lru.dictionary.GetAlternateComparer<TAlternateKey, K, I>().Create(key);
actualKey = this.Comparer.Create(key);
hasActualKey = true;
}

Expand All @@ -1043,7 +1046,7 @@ public V GetOrAdd(TAlternateKey key, Func<K, V> valueFactory)
return value;
}

K actualKey = this.Lru.dictionary.GetAlternateComparer<TAlternateKey, K, I>().Create(key);
K actualKey = this.Comparer.Create(key);

value = valueFactory(actualKey);
if (this.Lru.TryAdd(actualKey, value))
Expand All @@ -1062,7 +1065,7 @@ public V GetOrAdd<TArg>(TAlternateKey key, Func<K, TArg, V> valueFactory, TArg f
return value;
}

K actualKey = this.Lru.dictionary.GetAlternateComparer<TAlternateKey, K, I>().Create(key);
K actualKey = this.Comparer.Create(key);

value = valueFactory(actualKey, factoryArgument);
if (this.Lru.TryAdd(actualKey, value))
Expand All @@ -1079,7 +1082,7 @@ public ValueTask<V> GetOrAddAsync(TAlternateKey key, Func<K, Task<V>> valueFacto
return new ValueTask<V>(value);
}

K actualKey = this.Lru.dictionary.GetAlternateComparer<TAlternateKey, K, I>().Create(key);
K actualKey = this.Comparer.Create(key);
Task<V> task = valueFactory(actualKey);

return GetOrAddAsyncSlow(actualKey, task);
Expand All @@ -1092,7 +1095,7 @@ public ValueTask<V> GetOrAddAsync<TArg>(TAlternateKey key, Func<K, TArg, Task<V>
return new ValueTask<V>(value);
}

K actualKey = this.Lru.dictionary.GetAlternateComparer<TAlternateKey, K, I>().Create(key);
K actualKey = this.Comparer.Create(key);
Task<V> task = valueFactory(actualKey, factoryArgument);

return GetOrAddAsyncSlow(actualKey, task);
Expand Down
Loading