From e8fed8912f9969825e058cc160c1e2ed68773a50 Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Fri, 17 Nov 2023 21:52:21 -0800 Subject: [PATCH 1/3] opt --- BitFaster.Caching/Lfu/CmSketchCore.cs | 5 ++++- BitFaster.Caching/Lfu/ConcurrentLfu.cs | 10 ++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/BitFaster.Caching/Lfu/CmSketchCore.cs b/BitFaster.Caching/Lfu/CmSketchCore.cs index cdd70336..5882c57c 100644 --- a/BitFaster.Caching/Lfu/CmSketchCore.cs +++ b/BitFaster.Caching/Lfu/CmSketchCore.cs @@ -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; @@ -83,6 +84,7 @@ public int EstimateFrequency(T value) /// Increment the count of the specified value. /// /// The value. + [MethodImpl((MethodImplOptions)512)] public void Increment(T value) { #if NETSTANDARD2_0 @@ -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)); diff --git a/BitFaster.Caching/Lfu/ConcurrentLfu.cs b/BitFaster.Caching/Lfu/ConcurrentLfu.cs index 0d47aeee..704b019f 100644 --- a/BitFaster.Caching/Lfu/ConcurrentLfu.cs +++ b/BitFaster.Caching/Lfu/ConcurrentLfu.cs @@ -570,6 +570,7 @@ private void DrainBuffers() } } + [MethodImpl((MethodImplOptions)512)] private bool Maintenance(LfuNode droppedWrite = null) { this.drainStatus.VolatileWrite(DrainStatus.ProcessingToIdle); @@ -623,6 +624,7 @@ private bool Maintenance(LfuNode droppedWrite = null) return done; } + [MethodImpl((MethodImplOptions)512)] private void OnAccess(LfuNode node) { // there was a cache hit even if the item was removed or is not yet added. @@ -648,6 +650,7 @@ private void OnAccess(LfuNode node) } } + [MethodImpl((MethodImplOptions)512)] private void OnWrite(LfuNode node) { // Nodes can be removed while they are in the write buffer, in which case they should @@ -695,6 +698,7 @@ private void OnWrite(LfuNode node) } } + [MethodImpl((MethodImplOptions)512)] private void PromoteProbation(LfuNode node) { if (node.list == null) @@ -718,12 +722,14 @@ private void PromoteProbation(LfuNode node) } } + [MethodImpl((MethodImplOptions)512)] private void EvictEntries() { var candidate = EvictFromWindow(); EvictFromMain(candidate); } + [MethodImpl((MethodImplOptions)512)] private LfuNode EvictFromWindow() { LfuNode first = null; @@ -768,6 +774,7 @@ public void Next() } } + [MethodImpl((MethodImplOptions)512)] private void EvictFromMain(LfuNode candidateNode) { var victim = new EvictIterator(this.cmSketch, this.probationLru.First); // victims are LRU position in probation @@ -827,6 +834,7 @@ private void EvictFromMain(LfuNode candidateNode) } } + [MethodImpl((MethodImplOptions)512)] private bool AdmitCandidate(K candidateKey, K victimKey) { int victimFreq = this.cmSketch.EstimateFrequency(victimKey); @@ -838,6 +846,7 @@ private bool AdmitCandidate(K candidateKey, K victimKey) return candidateFreq > victimFreq; } + [MethodImpl((MethodImplOptions)512)] private void Evict(LfuNode evictee) { this.dictionary.TryRemove(evictee.Key, out var _); @@ -846,6 +855,7 @@ private void Evict(LfuNode evictee) this.metrics.evictedCount++; } + [MethodImpl((MethodImplOptions)512)] private void ReFitProtected() { // If hill climbing decreased protected, there may be too many items From e72faaafd1cebcb99302dd391810dff4f0194fc1 Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Fri, 17 Nov 2023 22:34:34 -0800 Subject: [PATCH 2/3] more methods --- BitFaster.Caching/Lfu/ConcurrentLfu.cs | 1 + BitFaster.Caching/Lru/ConcurrentLruCore.cs | 1 + 2 files changed, 2 insertions(+) diff --git a/BitFaster.Caching/Lfu/ConcurrentLfu.cs b/BitFaster.Caching/Lfu/ConcurrentLfu.cs index 704b019f..f543e24a 100644 --- a/BitFaster.Caching/Lfu/ConcurrentLfu.cs +++ b/BitFaster.Caching/Lfu/ConcurrentLfu.cs @@ -512,6 +512,7 @@ IEnumerator IEnumerable.GetEnumerator() return ((ConcurrentLfu)this).GetEnumerator(); } + [MethodImpl((MethodImplOptions)512)] private void TryScheduleDrain() { if (this.drainStatus.NonVolatileRead() >= DrainStatus.ProcessingToIdle) diff --git a/BitFaster.Caching/Lru/ConcurrentLruCore.cs b/BitFaster.Caching/Lru/ConcurrentLruCore.cs index 5ecb8dbb..18e65794 100644 --- a/BitFaster.Caching/Lru/ConcurrentLruCore.cs +++ b/BitFaster.Caching/Lru/ConcurrentLruCore.cs @@ -565,6 +565,7 @@ private void TrimWarmOrHot(ItemRemovedReason reason) } } + [MethodImpl((MethodImplOptions)512)] private void Cycle(int hotCount) { if (isWarm) From e6b0e054cecf9cf0af371f2ba664b844c7df6661 Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Fri, 17 Nov 2023 22:51:28 -0800 Subject: [PATCH 3/3] lru --- BitFaster.Caching/Lru/ConcurrentLruCore.cs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/BitFaster.Caching/Lru/ConcurrentLruCore.cs b/BitFaster.Caching/Lru/ConcurrentLruCore.cs index 18e65794..99a55086 100644 --- a/BitFaster.Caching/Lru/ConcurrentLruCore.cs +++ b/BitFaster.Caching/Lru/ConcurrentLruCore.cs @@ -631,6 +631,7 @@ private void CycleDuringWarmup(int hotCount) } } + [MethodImpl((MethodImplOptions)512)] private (ItemDestination, int) CycleHot(int hotCount) { if (hotCount > this.capacity.Hot) @@ -658,6 +659,7 @@ private void CycleDuringWarmup(int hotCount) } } + [MethodImpl((MethodImplOptions)512)] private (ItemDestination, int) CycleWarm(int count) { if (count > this.capacity.Warm) @@ -696,6 +698,7 @@ private void CycleDuringWarmup(int hotCount) } } + [MethodImpl((MethodImplOptions)512)] private (ItemDestination, int) CycleCold(int count) { if (count > this.capacity.Cold)