Skip to content
Merged
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
37 changes: 5 additions & 32 deletions BitFaster.Caching/Lfu/ConcurrentLfu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@
using BitFaster.Caching.Lru;
using BitFaster.Caching.Scheduler;

#if !NETSTANDARD2_0
using System.Buffers;
#endif

#if DEBUG
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -56,9 +52,7 @@ public sealed class ConcurrentLfu<K, V> : ICache<K, V>, IAsyncCache<K, V>, IBoun

private readonly IScheduler scheduler;

#if NETSTANDARD2_0
private readonly LfuNode<K, V>[] drainBuffer;
#endif

/// <summary>
/// Initializes a new instance of the ConcurrentLfu class with the specified capacity.
Expand Down Expand Up @@ -97,9 +91,7 @@ public ConcurrentLfu(int concurrencyLevel, int capacity, IScheduler scheduler, I

this.scheduler = scheduler;

#if NETSTANDARD2_0
this.drainBuffer = new LfuNode<K, V>[this.readBuffer.Capacity];
#endif
}

///<inheritdoc/>
Expand Down Expand Up @@ -435,26 +427,25 @@ private void DrainBuffers()
private bool Maintenance(LfuNode<K, V> droppedWrite = null)
{
this.drainStatus.Set(DrainStatus.ProcessingToIdle);
var localDrainBuffer = RentDrainBuffer();

// extract to a buffer before doing book keeping work, ~2x faster
int readCount = readBuffer.DrainTo(localDrainBuffer);
int readCount = readBuffer.DrainTo(this.drainBuffer);

for (int i = 0; i < readCount; i++)
{
this.cmSketch.Increment(localDrainBuffer[i].Key);
this.cmSketch.Increment(this.drainBuffer[i].Key);
}

for (int i = 0; i < readCount; i++)
{
OnAccess(localDrainBuffer[i]);
OnAccess(this.drainBuffer[i]);
}

int writeCount = this.writeBuffer.DrainTo(new ArraySegment<LfuNode<K, V>>(localDrainBuffer));
int writeCount = this.writeBuffer.DrainTo(new ArraySegment<LfuNode<K, V>>(this.drainBuffer));

for (int i = 0; i < writeCount; i++)
{
OnWrite(localDrainBuffer[i]);
OnWrite(this.drainBuffer[i]);
}

// we are done only when both buffers are empty
Expand All @@ -466,8 +457,6 @@ private bool Maintenance(LfuNode<K, V> droppedWrite = null)
done = true;
}

ReturnDrainBuffer(localDrainBuffer);

EvictEntries();
this.capacity.OptimizePartitioning(this.metrics, this.cmSketch.ResetSampleSize);
ReFitProtected();
Expand Down Expand Up @@ -687,22 +676,6 @@ private void ReFitProtected()
}
}

private LfuNode<K, V>[] RentDrainBuffer()
{
#if !NETSTANDARD2_0
return ArrayPool<LfuNode<K, V>>.Shared.Rent(this.readBuffer.Capacity);
#else
return drainBuffer;
#endif
}

private void ReturnDrainBuffer(LfuNode<K, V>[] localDrainBuffer)
{
#if !NETSTANDARD2_0
ArrayPool<LfuNode<K, V>>.Shared.Return(localDrainBuffer);
#endif
}

[DebuggerDisplay("{Format(),nq}")]
private class DrainStatus
{
Expand Down