Skip to content

Optimize maintenance family of methods #496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
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
5 changes: 4 additions & 1 deletion BitFaster.Caching/Lfu/CmSketchCore.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;

using System.Runtime.CompilerServices;

#if !NETSTANDARD2_0
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
Expand Down Expand Up @@ -83,6 +84,7 @@ public int EstimateFrequency(T value)
/// Increment the count of the specified value.
/// </summary>
/// <param name="value">The value.</param>
[MethodImpl((MethodImplOptions)512)]
public void Increment(T value)
{
#if NETSTANDARD2_0
Expand Down Expand Up @@ -269,6 +271,7 @@ private unsafe int EstimateFrequencyAvx(T value)
}
}

[MethodImpl((MethodImplOptions)512)]
private unsafe void IncrementAvx(T value)
{
int blockHash = Spread(comparer.GetHashCode(value));
Expand Down
11 changes: 11 additions & 0 deletions BitFaster.Caching/Lfu/ConcurrentLfu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -512,6 +512,7 @@ IEnumerator IEnumerable.GetEnumerator()
return ((ConcurrentLfu<K, V>)this).GetEnumerator();
}

[MethodImpl((MethodImplOptions)512)]
private void TryScheduleDrain()
{
if (this.drainStatus.NonVolatileRead() >= DrainStatus.ProcessingToIdle)
Expand Down Expand Up @@ -570,6 +571,7 @@ private void DrainBuffers()
}
}

[MethodImpl((MethodImplOptions)512)]
private bool Maintenance(LfuNode<K, V> droppedWrite = null)
{
this.drainStatus.VolatileWrite(DrainStatus.ProcessingToIdle);
Expand Down Expand Up @@ -623,6 +625,7 @@ private bool Maintenance(LfuNode<K, V> droppedWrite = null)
return done;
}

[MethodImpl((MethodImplOptions)512)]
private void OnAccess(LfuNode<K, V> node)
{
// there was a cache hit even if the item was removed or is not yet added.
Expand All @@ -648,6 +651,7 @@ private void OnAccess(LfuNode<K, V> node)
}
}

[MethodImpl((MethodImplOptions)512)]
private void OnWrite(LfuNode<K, V> node)
{
// Nodes can be removed while they are in the write buffer, in which case they should
Expand Down Expand Up @@ -695,6 +699,7 @@ private void OnWrite(LfuNode<K, V> node)
}
}

[MethodImpl((MethodImplOptions)512)]
private void PromoteProbation(LfuNode<K, V> node)
{
if (node.list == null)
Expand All @@ -718,12 +723,14 @@ private void PromoteProbation(LfuNode<K, V> node)
}
}

[MethodImpl((MethodImplOptions)512)]
private void EvictEntries()
{
var candidate = EvictFromWindow();
EvictFromMain(candidate);
}

[MethodImpl((MethodImplOptions)512)]
private LfuNode<K, V> EvictFromWindow()
{
LfuNode<K, V> first = null;
Expand Down Expand Up @@ -768,6 +775,7 @@ public void Next()
}
}

[MethodImpl((MethodImplOptions)512)]
private void EvictFromMain(LfuNode<K, V> candidateNode)
{
var victim = new EvictIterator(this.cmSketch, this.probationLru.First); // victims are LRU position in probation
Expand Down Expand Up @@ -827,6 +835,7 @@ private void EvictFromMain(LfuNode<K, V> candidateNode)
}
}

[MethodImpl((MethodImplOptions)512)]
private bool AdmitCandidate(K candidateKey, K victimKey)
{
int victimFreq = this.cmSketch.EstimateFrequency(victimKey);
Expand All @@ -838,6 +847,7 @@ private bool AdmitCandidate(K candidateKey, K victimKey)
return candidateFreq > victimFreq;
}

[MethodImpl((MethodImplOptions)512)]
private void Evict(LfuNode<K, V> evictee)
{
this.dictionary.TryRemove(evictee.Key, out var _);
Expand All @@ -846,6 +856,7 @@ private void Evict(LfuNode<K, V> evictee)
this.metrics.evictedCount++;
}

[MethodImpl((MethodImplOptions)512)]
private void ReFitProtected()
{
// If hill climbing decreased protected, there may be too many items
Expand Down
4 changes: 4 additions & 0 deletions BitFaster.Caching/Lru/ConcurrentLruCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -565,6 +565,7 @@ private void TrimWarmOrHot(ItemRemovedReason reason)
}
}

[MethodImpl((MethodImplOptions)512)]
private void Cycle(int hotCount)
{
if (isWarm)
Expand Down Expand Up @@ -630,6 +631,7 @@ private void CycleDuringWarmup(int hotCount)
}
}

[MethodImpl((MethodImplOptions)512)]
private (ItemDestination, int) CycleHot(int hotCount)
{
if (hotCount > this.capacity.Hot)
Expand Down Expand Up @@ -657,6 +659,7 @@ private void CycleDuringWarmup(int hotCount)
}
}

[MethodImpl((MethodImplOptions)512)]
private (ItemDestination, int) CycleWarm(int count)
{
if (count > this.capacity.Warm)
Expand Down Expand Up @@ -695,6 +698,7 @@ private void CycleDuringWarmup(int hotCount)
}
}

[MethodImpl((MethodImplOptions)512)]
private (ItemDestination, int) CycleCold(int count)
{
if (count > this.capacity.Cold)
Expand Down