|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +#if !NET6_0_OR_GREATER |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +#endif |
| 6 | + |
| 7 | +namespace LightningDB.Comparers; |
| 8 | + |
| 9 | +/// <summary> |
| 10 | +/// Compares MDBValue instances by hash code for faster comparison of large values. |
| 11 | +/// Falls back to byte comparison when hashes collide. |
| 12 | +/// Sort order is deterministic within a process but may vary across restarts. |
| 13 | +/// </summary> |
| 14 | +public sealed class HashCodeComparer : IComparer<MDBValue> |
| 15 | +{ |
| 16 | + public static readonly HashCodeComparer Instance = new(); |
| 17 | + |
| 18 | + private HashCodeComparer() { } |
| 19 | + |
| 20 | + public int Compare(MDBValue x, MDBValue y) |
| 21 | + { |
| 22 | + var left = x.AsSpan(); |
| 23 | + var right = y.AsSpan(); |
| 24 | + |
| 25 | + var leftHash = ComputeHash(left); |
| 26 | + var rightHash = ComputeHash(right); |
| 27 | + |
| 28 | + var hashCmp = leftHash.CompareTo(rightHash); |
| 29 | + return hashCmp != 0 ? hashCmp : left.SequenceCompareTo(right); |
| 30 | + } |
| 31 | + |
| 32 | +#if NET6_0_OR_GREATER |
| 33 | + private static int ComputeHash(ReadOnlySpan<byte> data) |
| 34 | + { |
| 35 | + var hc = new HashCode(); |
| 36 | + hc.AddBytes(data); |
| 37 | + return hc.ToHashCode(); |
| 38 | + } |
| 39 | +#else |
| 40 | + private static ulong ComputeHash(ReadOnlySpan<byte> data) |
| 41 | + { |
| 42 | + const ulong prime = 0x9E3779B97F4A7C15UL; |
| 43 | + var hash = (ulong)data.Length; |
| 44 | + |
| 45 | + while (data.Length >= 8) |
| 46 | + { |
| 47 | + hash ^= MemoryMarshal.Read<ulong>(data); |
| 48 | + hash *= prime; |
| 49 | + hash ^= hash >> 32; |
| 50 | + data = data.Slice(8); |
| 51 | + } |
| 52 | + |
| 53 | + if (data.Length >= 4) |
| 54 | + { |
| 55 | + hash ^= MemoryMarshal.Read<uint>(data); |
| 56 | + hash *= prime; |
| 57 | + data = data.Slice(4); |
| 58 | + } |
| 59 | + |
| 60 | + foreach (var b in data) |
| 61 | + { |
| 62 | + hash ^= b; |
| 63 | + hash *= prime; |
| 64 | + } |
| 65 | + |
| 66 | + return hash; |
| 67 | + } |
| 68 | +#endif |
| 69 | +} |
0 commit comments