| | | 1 | | using System; |
| | | 2 | | using System.Buffers; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Net.Sockets; |
| | | 6 | | using System.Numerics; |
| | | 7 | | using System.Runtime.CompilerServices; |
| | | 8 | | using System.Runtime.InteropServices; |
| | | 9 | | using System.Text; |
| | | 10 | | using System.Threading.Tasks; |
| | | 11 | | |
| | | 12 | | namespace DeltaSharp.Util.Hashtable; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Stores source landmarks and collision chains using 32-bit, zero-based block indices |
| | | 16 | | /// in a pooled integer array, with -1 marking empty slots. The indices address all complete |
| | | 17 | | /// 8-byte blocks in an int-sized source span; allocation remains subject to available memory |
| | | 18 | | /// and runtime array-size limits. |
| | | 19 | | /// </summary> |
| | | 20 | | internal unsafe readonly ref struct Hashtable64_32 |
| | | 21 | | #if NET9_0_OR_GREATER |
| | | 22 | | : IHashtable |
| | | 23 | | #endif |
| | | 24 | | { |
| | | 25 | | |
| | | 26 | | private readonly ReadOnlySpan<int> _collide; |
| | | 27 | | |
| | | 28 | | private readonly ReadOnlySpan<int> _landmark; |
| | | 29 | | |
| | | 30 | | private readonly ulong _nHashBitMask; |
| | | 31 | | |
| | | 32 | | private readonly int[] _buffer; |
| | | 33 | | |
| | | 34 | | public Hashtable64_32(ReadOnlySpan<byte> data) |
| | | 35 | | { |
| | | 36 | | //In C# arrays are limited to Int32.MaxValue elements |
| | | 37 | | //So we can safely assume here that the data length will not exceed 2,147,483,640 bytes |
| | | 38 | | //We can use the sign bit to indicate empty slots and avoid the extra checks and shifts needed in the other vers |
| | | 39 | | |
| | 60 | 40 | | var nLong = (int)data.Length / 8; |
| | | 41 | | |
| | | 42 | | // Compute the hash table used to locate matching sections in the source. |
| | 60 | 43 | | int nHash = Hash.TableSize(nLong); |
| | | 44 | | |
| | 60 | 45 | | ulong bucketMask = (ulong)(nHash - 1); |
| | 60 | 46 | | _nHashBitMask = bucketMask; |
| | | 47 | | |
| | 60 | 48 | | _buffer = ArrayPool<int>.Shared.Rent(nHash * 2); |
| | 60 | 49 | | Span<int> landmark = _buffer.AsSpan(nHash, nHash); |
| | 60 | 50 | | landmark.Fill(-1); |
| | | 51 | | // Every live collision link is assigned during insertion below. |
| | 60 | 52 | | Span<int> collide = _buffer.AsSpan(0, nLong); |
| | | 53 | | |
| | 60 | 54 | | ReadOnlySpan<ulong> ulongData = MemoryMarshal.Cast<byte, ulong>(data).Slice(0, nLong); |
| | | 55 | | |
| | | 56 | | unsafe |
| | | 57 | | { |
| | 60 | 58 | | fixed (int* collideP = collide) |
| | 60 | 59 | | fixed (int* landmarkP = landmark) |
| | | 60 | | { |
| | 340132180 | 61 | | for (var i = nLong - 1; i >= 0; i -= 1) |
| | | 62 | | { |
| | 170066030 | 63 | | var d = ulongData[i]; |
| | | 64 | | |
| | 170066030 | 65 | | nuint hv = (nuint)(Hash.Hash64(d) & bucketMask); |
| | | 66 | | |
| | 170066030 | 67 | | *(collideP + i) = landmarkP[hv]; |
| | 170066030 | 68 | | *(landmarkP + hv) = i; |
| | | 69 | | } |
| | | 70 | | } |
| | | 71 | | } |
| | | 72 | | |
| | 60 | 73 | | _collide = collide; |
| | 60 | 74 | | _landmark = landmark; |
| | | 75 | | |
| | 60 | 76 | | } |
| | | 77 | | |
| | | 78 | | |
| | | 79 | | |
| | | 80 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 81 | | public int Match(ReadOnlySpan<byte> data) |
| | | 82 | | { |
| | | 83 | | //assume data is at least 8 bytes long to do a branchless read |
| | 468965 | 84 | | var ulongData = Unsafe.ReadUnaligned<ulong>(ref MemoryMarshal.GetReference(data)); |
| | | 85 | | |
| | 468965 | 86 | | var bucket = Bucket(ulongData); |
| | | 87 | | |
| | | 88 | | //branchless read since bucket will always be within range |
| | 468965 | 89 | | int pos = _landmark[bucket]; |
| | | 90 | | |
| | 468965 | 91 | | return pos * 8; |
| | | 92 | | } |
| | | 93 | | |
| | | 94 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 95 | | public int More(int pos) |
| | | 96 | | { |
| | | 97 | | //branchless read |
| | | 98 | | //index is always within range |
| | 557630 | 99 | | return _collide[(int)((uint)pos / 8)] * 8; |
| | | 100 | | } |
| | | 101 | | |
| | | 102 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 103 | | private int Bucket(ulong x) |
| | | 104 | | { |
| | 468965 | 105 | | return (int)(Hash.Hash64(x) & _nHashBitMask); |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | public HashtableStats GetStats() |
| | | 109 | | { |
| | 30 | 110 | | return new HashtableStats |
| | 30 | 111 | | { |
| | 30 | 112 | | Collisions = _collide.Length - _collide.Count(-1), |
| | 30 | 113 | | Slots = _landmark.Length, |
| | 30 | 114 | | SlotsFilled = _landmark.Length - _landmark.Count(-1) |
| | 30 | 115 | | }; |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | public void Dispose() |
| | | 119 | | { |
| | 60 | 120 | | ArrayPool<int>.Shared.Return(_buffer); |
| | 60 | 121 | | } |
| | | 122 | | } |