Skip to content

Commit e21bd63

Browse files
authored
singleton cache capacity is a prime number (#425)
* singleton cache prime * fallback ---------
1 parent f4344e1 commit e21bd63

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

BitFaster.Caching.UnitTests/ConcurrentDictionarySizeTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ public ConcurrentDictionarySizeTests(ITestOutputHelper testOutputHelper)
1818
[InlineData(8, 11)]
1919
[InlineData(12, 17)]
2020
[InlineData(196, 197)]
21-
[InlineData(500, 197)]
21+
[InlineData(7199370, 7199370)]
2222
public void NextPrimeGreaterThan(int input, int nextPrime)
2323
{
2424
ConcurrentDictionarySize.NextPrimeGreaterThan(input).Should().Be(nextPrime);

BitFaster.Caching/ConcurrentDictionarySize.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ internal static int NextPrimeGreaterThan(int min)
1515
}
1616
}
1717

18-
return 197;
18+
return min;
1919
}
2020

2121
/// <summary>
@@ -71,7 +71,11 @@ internal static int Estimate(int desiredSize)
7171
#else
7272
internal static ReadOnlySpan<int> Primes => new int[] {
7373
#endif
74-
7, 11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197
74+
3, 7, 11, 17, 23, 29, 37, 47, 59, 71, 89, 107, 131, 163, 197, 239, 293, 353, 431, 521, 631, 761, 919,
75+
1103, 1327, 1597, 1931, 2333, 2801, 3371, 4049, 4861, 5839, 7013, 8419, 10103, 12143, 14591,
76+
17519, 21023, 25229, 30293, 36353, 43627, 52361, 62851, 75431, 90523, 108631, 130363, 156437,
77+
187751, 225307, 270371, 324449, 389357, 467237, 560689, 672827, 807403, 968897, 1162687, 1395263,
78+
1674319, 2009191, 2411033, 2893249, 3471899, 4166287, 4999559, 5999471, 7199369
7579
};
7680

7781
#if NETSTANDARD2_0

BitFaster.Caching/SingletonCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public SingletonCache()
3333
/// <param name="comparer">The IEqualityComparer implementation to use when comparing keys.</param>
3434
public SingletonCache(int concurrencyLevel, int capacity, IEqualityComparer<TKey> comparer)
3535
{
36-
this.cache = new ConcurrentDictionary<TKey, ReferenceCount<TValue>>(concurrencyLevel, capacity, comparer);
36+
this.cache = new ConcurrentDictionary<TKey, ReferenceCount<TValue>>(concurrencyLevel, ConcurrentDictionarySize.NextPrimeGreaterThan(capacity), comparer);
3737
}
3838

3939
/// <summary>

0 commit comments

Comments
 (0)