Skip to content

Commit f73fe17

Browse files
authored
misc cleanups (#698)
* cleanup * fixes ---------
1 parent 1bafa48 commit f73fe17

File tree

5 files changed

+65
-4
lines changed

5 files changed

+65
-4
lines changed

BitFaster.Caching.Benchmarks/Lfu/CmSketchFlat.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ private int IndexOf(int item, int i)
199199
return ((int)hash) & tableMask;
200200
}
201201

202-
private int Spread(int x)
202+
private static int Spread(int x)
203203
{
204204
uint y = (uint)x;
205205
y = ((y >> 16) ^ y) * 0x45d9f3b;
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
using System.Threading;
2+
using Benchly;
3+
using BenchmarkDotNet.Attributes;
4+
using BenchmarkDotNet.Jobs;
5+
6+
namespace BitFaster.Caching.Benchmarks
7+
{
8+
[SimpleJob(RuntimeMoniker.Net90)]
9+
[MemoryDiagnoser(displayGenColumns: false)]
10+
[HideColumns("Job", "Median", "RatioSD", "Alloc Ratio")]
11+
[ColumnChart(Title = "Try enter ({JOB})")]
12+
public class LockBench
13+
{
14+
private int _value;
15+
private readonly object monitorLock = new object();
16+
#if NET9_0_OR_GREATER
17+
private readonly Lock threadingLock = new Lock();
18+
#endif
19+
20+
[Benchmark(Baseline = true)]
21+
public void UseMonitor()
22+
{
23+
bool lockTaken = false;
24+
Monitor.TryEnter(monitorLock, ref lockTaken);
25+
26+
if (lockTaken)
27+
{
28+
try
29+
{
30+
_value++;
31+
}
32+
finally
33+
{
34+
if (lockTaken)
35+
{
36+
Monitor.Exit(monitorLock);
37+
}
38+
}
39+
}
40+
}
41+
42+
[Benchmark()]
43+
public void UseLock()
44+
{
45+
#if NET9_0_OR_GREATER
46+
if (threadingLock.TryEnter())
47+
{
48+
try
49+
{
50+
_value++;
51+
}
52+
finally
53+
{
54+
threadingLock.Exit();
55+
}
56+
}
57+
#endif
58+
}
59+
}
60+
}

BitFaster.Caching/Atomic/AtomicEx.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
using System.Collections.Generic;
44
using System.Collections.ObjectModel;
55
using System.Linq;
6-
using System.Text;
76

87
namespace BitFaster.Caching.Atomic
98
{
@@ -21,6 +20,7 @@ internal static int EnumerateCount(IEnumerator enumerator)
2120

2221
internal static ICollection<K> FilterKeys<K, V>(IEnumerable<KeyValuePair<K, V>> kvps, Func<V, bool> filter)
2322
{
23+
#pragma warning disable CA1851
2424
// Here we will double enumerate the kvps list. Alternative is to lazy init the size which will keep resizing
2525
// the List, and spam allocs if the list is long.
2626
List<K> keys = new List<K>(kvps.Count());
@@ -34,6 +34,7 @@ internal static ICollection<K> FilterKeys<K, V>(IEnumerable<KeyValuePair<K, V>>
3434
}
3535

3636
return new ReadOnlyCollection<K>(keys);
37+
#pragma warning restore CA1851
3738
}
3839
}
3940
}

BitFaster.Caching/Lru/ConcurrentLru.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ internal static ICache<K, V> Create<K, V>(LruInfo<K> info)
4040
};
4141
}
4242

43-
private static ICache<K, V> CreateExpireAfterAccess<K, V, TP>(LruInfo<K> info) where K : notnull where TP : struct, ITelemetryPolicy<K, V>
43+
private static ConcurrentLruCore<K, V, LongTickCountLruItem<K, V>, AfterAccessPolicy<K, V>, TP> CreateExpireAfterAccess<K, V, TP>(LruInfo<K> info) where K : notnull where TP : struct, ITelemetryPolicy<K, V>
4444
{
4545
return new ConcurrentLruCore<K, V, LongTickCountLruItem<K, V>, AfterAccessPolicy<K, V>, TP>(
4646
info.ConcurrencyLevel, info.Capacity, info.KeyComparer, new AfterAccessPolicy<K, V>(info.TimeToExpireAfterAccess!.Value), default);
4747
}
4848

49-
private static ICache<K, V> CreateExpireAfter<K, V, TP>(LruInfo<K> info, IExpiryCalculator<K, V> expiry) where K : notnull where TP : struct, ITelemetryPolicy<K, V>
49+
private static ConcurrentLruCore<K, V, LongTickCountLruItem<K, V>, DiscretePolicy<K, V>, TP> CreateExpireAfter<K, V, TP>(LruInfo<K> info, IExpiryCalculator<K, V> expiry) where K : notnull where TP : struct, ITelemetryPolicy<K, V>
5050
{
5151
return new ConcurrentLruCore<K, V, LongTickCountLruItem<K, V>, DiscretePolicy<K, V>, TP>(
5252
info.ConcurrencyLevel, info.Capacity, info.KeyComparer, new DiscretePolicy<K, V>(expiry), default);

0 commit comments

Comments
 (0)