Skip to content
Merged

isa #268

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
4 changes: 2 additions & 2 deletions BitFaster.Caching.Benchmarks/Lfu/SketchFrequency.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace BitFaster.Caching.Benchmarks.Lfu
public class SketchFrequency
{
const int iterations = 512;
private static CmSketch<int, DisableAvx2> std = new CmSketch<int, DisableAvx2>(10, EqualityComparer<int>.Default);
private static CmSketch<int, DetectAvx2> avx = new CmSketch<int, DetectAvx2>(10, EqualityComparer<int>.Default);
private static CmSketch<int, DisableHardwareIntrinsics> std = new CmSketch<int, DisableHardwareIntrinsics>(10, EqualityComparer<int>.Default);
private static CmSketch<int, DetectIsa> avx = new CmSketch<int, DetectIsa>(10, EqualityComparer<int>.Default);

[GlobalSetup]
public void Setup()
Expand Down
4 changes: 2 additions & 2 deletions BitFaster.Caching.Benchmarks/Lfu/SketchIncrement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ namespace BitFaster.Caching.Benchmarks.Lfu
public class SketchIncrement
{
const int iterations = 1024;
private static CmSketch<int, DisableAvx2> std = new CmSketch<int, DisableAvx2>(10, EqualityComparer<int>.Default);
private static CmSketch<int, DetectAvx2> avx = new CmSketch<int, DetectAvx2>(10, EqualityComparer<int>.Default);
private static CmSketch<int, DisableHardwareIntrinsics> std = new CmSketch<int, DisableHardwareIntrinsics>(10, EqualityComparer<int>.Default);
private static CmSketch<int, DetectIsa> avx = new CmSketch<int, DetectIsa>(10, EqualityComparer<int>.Default);

[Benchmark(Baseline = true)]
public void Inc()
Expand Down
14 changes: 7 additions & 7 deletions BitFaster.Caching.UnitTests/Lfu/CmSketchTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,18 @@
namespace BitFaster.Caching.UnitTests.Lfu
{
// Test with AVX2 if it is supported
public class CMSketchAvx2Tests : CmSketchTestBase<DetectAvx2>
public class CMSketchAvx2Tests : CmSketchTestBase<DetectIsa>
{
}

// Test with AVX2 disabled
public class CmSketchTests : CmSketchTestBase<DisableAvx2>
public class CmSketchTests : CmSketchTestBase<DisableHardwareIntrinsics>
{
}

public abstract class CmSketchTestBase<AVX2> where AVX2 : struct, IAvx2
public abstract class CmSketchTestBase<I> where I : struct, IsaProbe
{
private CmSketch<int, AVX2> sketch = new CmSketch<int, AVX2>(512, EqualityComparer<int>.Default);
private CmSketch<int, I> sketch = new CmSketch<int, I>(512, EqualityComparer<int>.Default);

public CmSketchTestBase()
{
Expand All @@ -29,7 +29,7 @@ public CmSketchTestBase()
[SkippableFact]
public void WhenCapacityIsZeroDefaultsSelected()
{
sketch = new CmSketch<int, AVX2>(0, EqualityComparer<int>.Default);
sketch = new CmSketch<int, I>(0, EqualityComparer<int>.Default);

sketch.ResetSampleSize.Should().Be(10);
}
Expand Down Expand Up @@ -68,7 +68,7 @@ public void WhenSampleSizeExceededCountIsReset()
{
bool reset = false;

sketch = new CmSketch<int, AVX2>(64, EqualityComparer<int>.Default);
sketch = new CmSketch<int, I>(64, EqualityComparer<int>.Default);

for (int i = 1; i < 20 * 64; i++)
{
Expand Down Expand Up @@ -101,7 +101,7 @@ public void WhenClearedCountIsReset()
private static void SkipAvxIfNotSupported()
{
// when we are trying to test Avx2, skip the test if it's not supported
Skip.If(typeof(AVX2) == typeof(DetectAvx2) && !Avx2.IsSupported);
Skip.If(typeof(I) == typeof(DetectIsa) && !Avx2.IsSupported);
}
}
}
22 changes: 11 additions & 11 deletions BitFaster.Caching/Intrinsics.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,36 +5,36 @@
namespace BitFaster.Caching
{
/// <summary>
/// Represents a marker interface to enable AVX2 specific optimization.
/// Represents a marker interface to enable instruction set hardware intrinsics.
/// </summary>
public interface IAvx2
public interface IsaProbe
{
/// <summary>
/// Gets a value indicating whether Avx2 is supported
/// Gets a value indicating whether AVX2 is supported.
/// </summary>
bool IsSupported { get; }
bool IsAvx2Supported { get; }
}

/// <summary>
/// Detect AVX2 support and enable if available.
/// Detect support for hardware instructions via intrinsics.
/// </summary>
public struct DetectAvx2 : IAvx2
public readonly struct DetectIsa : IsaProbe
{
#if NETSTANDARD2_0
/// <inheritdoc/>
public bool IsSupported => false;
public bool IsAvx2Supported => false;
#else
/// <inheritdoc/>
public bool IsSupported => Avx2.IsSupported;
public bool IsAvx2Supported => Avx2.IsSupported;
#endif
}

/// <summary>
/// Force disable AVX2.
/// Force disable hardware instructions via intrinsics.
/// </summary>
public struct DisableAvx2 : IAvx2
public readonly struct DisableHardwareIntrinsics : IsaProbe
{
/// <inheritdoc/>
public bool IsSupported => false;
public bool IsAvx2Supported => false;
}
}
10 changes: 5 additions & 5 deletions BitFaster.Caching/Lfu/CmSketch.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace BitFaster.Caching.Lfu
/// </summary>
/// This is a direct C# translation of FrequencySketch in the Caffeine library by ben.manes@gmail.com (Ben Manes).
/// https://github.com/ben-manes/caffeine
public sealed class CmSketch<T, AVX2> where AVX2 : struct, IAvx2
public sealed class CmSketch<T, I> where I : struct, IsaProbe
{
// A mixture of seeds from FNV-1a, CityHash, and Murmur3
private static readonly ulong[] Seed = { 0xc3a5c85c97cb3127L, 0xb492b66fbe98f273L, 0x9ae16a3b2f90404fL, 0xcbf29ce484222325L};
Expand Down Expand Up @@ -61,9 +61,9 @@ public int EstimateFrequency(T value)
return EstimateFrequencyStd(value);
#else

AVX2 avx2 = default;
I isa = default;

if (avx2.IsSupported)
if (isa.IsAvx2Supported)
{
return EstimateFrequencyAvx(value);
}
Expand All @@ -84,9 +84,9 @@ public void Increment(T value)
IncrementStd(value);
#else

AVX2 avx2 = default;
I isa = default;

if (avx2.IsSupported)
if (isa.IsAvx2Supported)
{
IncrementAvx(value);
}
Expand Down
8 changes: 4 additions & 4 deletions BitFaster.Caching/Lfu/ConcurrentLfu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public sealed class ConcurrentLfu<K, V> : ICache<K, V>, IAsyncCache<K, V>, IBoun

private readonly CacheMetrics metrics = new CacheMetrics();

private readonly CmSketch<K, DetectAvx2> cmSketch;
private readonly CmSketch<K, DetectIsa> cmSketch;

private readonly LfuNodeList<K, V> windowLru;
private readonly LfuNodeList<K, V> probationLru;
Expand Down Expand Up @@ -100,7 +100,7 @@ public ConcurrentLfu(int concurrencyLevel, int capacity, IScheduler scheduler, I
int writeBufferSize = Math.Min(BitOps.CeilingPowerOfTwo(capacity), 128);
this.writeBuffer = new MpscBoundedBuffer<LfuNode<K, V>>(writeBufferSize);

this.cmSketch = new CmSketch<K, DetectAvx2>(capacity, comparer);
this.cmSketch = new CmSketch<K, DetectIsa>(capacity, comparer);
this.windowLru = new LfuNodeList<K, V>();
this.probationLru = new LfuNodeList<K, V>();
this.protectedLru = new LfuNodeList<K, V>();
Expand Down Expand Up @@ -617,11 +617,11 @@ private LfuNode<K, V> EvictFromWindow()

private ref struct EvictIterator
{
private readonly CmSketch<K, DetectAvx2> sketch;
private readonly CmSketch<K, DetectIsa> sketch;
public LfuNode<K, V> node;
public int freq;

public EvictIterator(CmSketch<K, DetectAvx2> sketch, LfuNode<K, V> node)
public EvictIterator(CmSketch<K, DetectIsa> sketch, LfuNode<K, V> node)
{
this.sketch = sketch;
this.node = node;
Expand Down