204 lines
No EOL
7.1 KiB
C#
204 lines
No EOL
7.1 KiB
C#
using System.Numerics;
|
|
using System.Runtime.CompilerServices;
|
|
using System.Runtime.Intrinsics;
|
|
using System.Runtime.Intrinsics.X86;
|
|
|
|
namespace PersistentMap;
|
|
|
|
using System;
|
|
using System.Buffers.Binary;
|
|
using System.Runtime.CompilerServices;
|
|
|
|
public interface IKeyStrategy<K>
|
|
{
|
|
int Compare(K x, K y);
|
|
long GetPrefix(K key);
|
|
|
|
bool UsesPrefixes => true;
|
|
}
|
|
|
|
|
|
public struct UnicodeStrategy : IKeyStrategy<string>
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public int Compare(string? x, string? y) => string.CompareOrdinal(x, y);
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public long GetPrefix(string key)
|
|
{
|
|
if (string.IsNullOrEmpty(key)) return long.MinValue;
|
|
|
|
// 1. Prepare Buffer (8 bytes)
|
|
// stackalloc is virtually free (pointer bump)
|
|
Span<byte> utf8Bytes = stackalloc byte[8];
|
|
|
|
// 2. Transcode (The "Safe" Magic)
|
|
// This intrinsic handles ASCII efficiently and converts Surrogates/Chinese
|
|
// into bytes that maintain the correct "Magnitude" (Sort Order).
|
|
// Invalid surrogates become 0xEF (Replacement Char), which sorts > ASCII.
|
|
System.Text.Unicode.Utf8.FromUtf16(
|
|
key.AsSpan(0, Math.Min(key.Length, 8)),
|
|
utf8Bytes,
|
|
out _,
|
|
out _,
|
|
replaceInvalidSequences: true); // True ensures we get 0xEF for broken chars
|
|
|
|
// 3. Load as Big Endian Long
|
|
long packed = BinaryPrimitives.ReadInt64BigEndian(utf8Bytes);
|
|
|
|
// 4. Sign Toggle
|
|
// Maps the byte range 0x00..0xFF to the signed long range Min..Max
|
|
// Essential for the < and > operators to work correctly.
|
|
return packed ^ unchecked((long)0x8080808080808080);
|
|
}
|
|
}
|
|
|
|
public struct IntStrategy : IKeyStrategy<int>
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public int Compare(int x, int y) => x.CompareTo(y);
|
|
|
|
public bool UsesPrefixes => false;
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public long GetPrefix(int key)
|
|
{
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
public struct DoubleStrategy : IKeyStrategy<double>
|
|
{
|
|
// Use the standard comparison for the fallback/refine step
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public int Compare(double x, double y) => x.CompareTo(y);
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public long GetPrefix(double key)
|
|
{
|
|
// 1. Bit Cast to Long (0 cost)
|
|
long bits = Unsafe.As<double, long>(ref key);
|
|
|
|
// 2. The Magic Twist
|
|
// If the sign bit (MSB) is set (negative), we flip ALL bits.
|
|
// If the sign bit is clear (positive), we flip ONLY the sign bit.
|
|
// This maps:
|
|
// -Negative Max -> 0
|
|
// -0 -> Midpoint
|
|
// +Negative Max -> Max
|
|
|
|
long mask = (bits >> 63); // 0 for positive, -1 (All 1s) for negative
|
|
|
|
// If negative: bits ^ -1 = ~bits (Flip All)
|
|
// If positive: bits ^ 0 = bits (Flip None)
|
|
// Then we toggle the sign bit (0x8000...) to shift the range to signed long.
|
|
|
|
return (bits ^ (mask & 0x7FFFFFFFFFFFFFFF)) ^ unchecked((long)0x8000000000000000);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
/// Helper for SIMD accelerated prefix scanning.
|
|
/// </summary>
|
|
public static class PrefixScanner
|
|
{
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
public static int FindFirstGreaterOrEqual(ReadOnlySpan<long> prefixes, long targetPrefix)
|
|
{
|
|
// Handle MinValue specifically to avoid underflow in (target - 1) logic
|
|
// If target is MinValue, any value in prefixes is >= target.
|
|
// So the first element (index 0) is the match.
|
|
// TODO: evaluate if this is needed.
|
|
if (targetPrefix == long.MinValue)
|
|
{
|
|
return 0;
|
|
}
|
|
|
|
// Fallback for short arrays or unsupported hardware
|
|
if (!Avx2.IsSupported || prefixes.Length < 4)
|
|
return LinearScan(prefixes, targetPrefix);
|
|
|
|
return Avx512F.IsSupported
|
|
? ScanAvx512(prefixes, targetPrefix)
|
|
: ScanAvx2(prefixes, targetPrefix);
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private static int LinearScan(ReadOnlySpan<long> prefixes, long target)
|
|
{
|
|
for (var i = 0; i < prefixes.Length; i++)
|
|
if (prefixes[i] >= target)
|
|
return i;
|
|
return prefixes.Length;
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private static unsafe int ScanAvx2(ReadOnlySpan<long> prefixes, long target)
|
|
{
|
|
// Create a vector where every element is the target prefix
|
|
var vTarget = Vector256.Create(target);
|
|
var i = 0;
|
|
var len = prefixes.Length;
|
|
|
|
// Process 4 longs at a time (256 bits)
|
|
for (; i <= len - 4; i += 4)
|
|
fixed (long* ptr = prefixes)
|
|
{
|
|
var vData = Avx2.LoadVector256(ptr + i);
|
|
|
|
// Compare: result is -1 (all 1s) if true, 0 if false
|
|
// We want Data >= Target.
|
|
// AVX2 CompareGreaterThan is for signed. Longs should be treated carefully,
|
|
// but for text prefixes (positive), signed compare is usually sufficient.
|
|
// Effectively: !(Data < Target) could be safer if signs vary,
|
|
// but here we assume prefixes are derived from unsigned chars.
|
|
// Standard AVX2 hack for CompareGreaterOrEqual (Signed):
|
|
// No native _mm256_cmpge_epi64 in AVX2.
|
|
// Use CompareGreaterThan(Data, Target - 1)
|
|
var vResult = Avx2.CompareGreaterThan(vData, Vector256.Create(target - 1));
|
|
|
|
var mask = Avx2.MoveMask(vResult.AsByte());
|
|
|
|
if (mask != 0)
|
|
{
|
|
// Identify the first set bit corresponding to a 64-bit element
|
|
// MoveMask returns 32 bits (1 per byte). Each long is 8 bytes.
|
|
// We check bits 0, 8, 16, 24.
|
|
if ((mask & 0xFF) != 0) return i + 0;
|
|
if ((mask & 0xFF00) != 0) return i + 1;
|
|
if ((mask & 0xFF0000) != 0) return i + 2;
|
|
return i + 3;
|
|
}
|
|
}
|
|
|
|
return LinearScan(prefixes.Slice(i), target) + i;
|
|
}
|
|
|
|
[MethodImpl(MethodImplOptions.AggressiveInlining)]
|
|
private static unsafe int ScanAvx512(ReadOnlySpan<long> prefixes, long target)
|
|
{
|
|
var vTarget = Vector512.Create(target);
|
|
var i = 0;
|
|
var len = prefixes.Length;
|
|
|
|
for (; i <= len - 8; i += 8)
|
|
fixed (long* ptr = prefixes)
|
|
{
|
|
var vData = Avx512F.LoadVector512(ptr + i);
|
|
// AVX512 has dedicated Compare Greater Than or Equal Long
|
|
var mask = Avx512F.CompareGreaterThanOrEqual(vData, vTarget);
|
|
|
|
if (mask != Vector512<long>.Zero)
|
|
{
|
|
// Extract most significant bit mask
|
|
var m = mask.ExtractMostSignificantBits();
|
|
// Count trailing zeros to find the index
|
|
return i + BitOperations.TrailingZeroCount(m);
|
|
}
|
|
}
|
|
|
|
return LinearScan(prefixes.Slice(i), target) + i;
|
|
}
|
|
} |