From 2cbb1337b0d80eab25fc2bf38306fb9cd39dc663 Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Sun, 4 Sep 2022 17:27:06 -0700 Subject: [PATCH 1/2] dedupe --- BitFaster.Caching/BitOps.cs | 9 +++++ .../Buffers/StripedMpmcBuffer.cs | 36 +------------------ .../Buffers/StripedMpscBuffer.cs | 36 +------------------ 3 files changed, 11 insertions(+), 70 deletions(-) diff --git a/BitFaster.Caching/BitOps.cs b/BitFaster.Caching/BitOps.cs index 5b4999e6..1ee92726 100644 --- a/BitFaster.Caching/BitOps.cs +++ b/BitFaster.Caching/BitOps.cs @@ -70,5 +70,14 @@ public static int BitCount(ulong x) return BitOperations.PopCount(x); #endif } + + // Computes Stafford variant 13 of 64-bit mix function. + // http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html + public static ulong Mix64(ulong z) + { + z = (z ^ z >> 30) * 0xbf58476d1ce4e5b9L; + z = (z ^ z >> 27) * 0x94d049bb133111ebL; + return z ^ z >> 31; + } } } diff --git a/BitFaster.Caching/Buffers/StripedMpmcBuffer.cs b/BitFaster.Caching/Buffers/StripedMpmcBuffer.cs index b1d1c0e0..b291231e 100644 --- a/BitFaster.Caching/Buffers/StripedMpmcBuffer.cs +++ b/BitFaster.Caching/Buffers/StripedMpmcBuffer.cs @@ -1,9 +1,5 @@ using System; -#if !NETSTANDARD2_0 -using System.Runtime.Intrinsics.X86; -#endif - namespace BitFaster.Caching.Buffers { /// @@ -59,28 +55,7 @@ public int DrainTo(T[] outputBuffer) public BufferStatus TryAdd(T item) { - // Is using Sse42.Crc32 faster? - //#if NETSTANDARD2_0 - // ulong z = Mix64((ulong)Environment.CurrentManagedThreadId); - // int inc = (int)(z >> 32) | 1; - // int h = (int)z; - //#else - // int inc, h; - - // // https://rigtorp.se/notes/hashing/ - // if (Sse42.IsSupported) - // { - // h = inc = (int)Sse42.Crc32(486187739, (uint)Environment.CurrentManagedThreadId); - // } - // else - // { - // ulong z = Mix64((ulong)Environment.CurrentManagedThreadId); - // inc = (int)(z >> 32) | 1; - // h = (int)z; - // } - //#endif - - var z = Mix64((ulong)Environment.CurrentManagedThreadId); + var z = BitOps.Mix64((ulong)Environment.CurrentManagedThreadId); var inc = (int)(z >> 32) | 1; var h = (int)z; @@ -110,14 +85,5 @@ public void Clear() buffers[i].Clear(); } } - - // Computes Stafford variant 13 of 64-bit mix function. - // http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html - private static ulong Mix64(ulong z) - { - z = (z ^ z >> 30) * 0xbf58476d1ce4e5b9L; - z = (z ^ z >> 27) * 0x94d049bb133111ebL; - return z ^ z >> 31; - } } } diff --git a/BitFaster.Caching/Buffers/StripedMpscBuffer.cs b/BitFaster.Caching/Buffers/StripedMpscBuffer.cs index bb6f45e1..437ba233 100644 --- a/BitFaster.Caching/Buffers/StripedMpscBuffer.cs +++ b/BitFaster.Caching/Buffers/StripedMpscBuffer.cs @@ -2,10 +2,6 @@ using System.Diagnostics; using System.Linq; -#if !NETSTANDARD2_0 -using System.Runtime.Intrinsics.X86; -#endif - namespace BitFaster.Caching.Buffers { /// @@ -60,28 +56,7 @@ public int DrainTo(T[] outputBuffer) public BufferStatus TryAdd(T item) { - // Is using Sse42.Crc32 faster? - //#if NETSTANDARD2_0 - // ulong z = Mix64((ulong)Environment.CurrentManagedThreadId); - // int inc = (int)(z >> 32) | 1; - // int h = (int)z; - //#else - // int inc, h; - - // // https://rigtorp.se/notes/hashing/ - // if (Sse42.IsSupported) - // { - // h = inc = (int)Sse42.Crc32(486187739, (uint)Environment.CurrentManagedThreadId); - // } - // else - // { - // ulong z = Mix64((ulong)Environment.CurrentManagedThreadId); - // inc = (int)(z >> 32) | 1; - // h = (int)z; - // } - //#endif - - var z = Mix64((ulong)Environment.CurrentManagedThreadId); + var z = BitOps.Mix64((ulong)Environment.CurrentManagedThreadId); var inc = (int)(z >> 32) | 1; var h = (int)z; @@ -111,14 +86,5 @@ public void Clear() buffers[i].Clear(); } } - - // Computes Stafford variant 13 of 64-bit mix function. - // http://zimbry.blogspot.com/2011/09/better-bit-mixing-improving-on.html - private static ulong Mix64(ulong z) - { - z = (z ^ z >> 30) * 0xbf58476d1ce4e5b9L; - z = (z ^ z >> 27) * 0x94d049bb133111ebL; - return z ^ z >> 31; - } } } From 0328fec4a7bb09da70c95d1d32788975b5ca647a Mon Sep 17 00:00:00 2001 From: Alex Peck Date: Sun, 4 Sep 2022 17:29:05 -0700 Subject: [PATCH 2/2] ns --- BitFaster.Caching/BitOps.cs | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/BitFaster.Caching/BitOps.cs b/BitFaster.Caching/BitOps.cs index 1ee92726..0ea28129 100644 --- a/BitFaster.Caching/BitOps.cs +++ b/BitFaster.Caching/BitOps.cs @@ -1,7 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Numerics; -using System.Text; +using System.Numerics; namespace BitFaster.Caching {