| | | 1 | | using System; |
| | | 2 | | using System.Buffers; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Linq; |
| | | 5 | | using System.Numerics; |
| | | 6 | | using System.Runtime.CompilerServices; |
| | | 7 | | using System.Runtime.InteropServices; |
| | | 8 | | using System.Text; |
| | | 9 | | using System.Threading.Tasks; |
| | | 10 | | |
| | | 11 | | namespace DeltaSharp.Util.Hashtable; |
| | | 12 | | |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Stores up to 65,535 complete 8-byte blocks (source lengths through 524,287 bytes) |
| | | 16 | | /// using 16-bit, one-based block indices. Uses a managed array for smaller tables |
| | | 17 | | /// and rents an array from the shared pool for larger tables. |
| | | 18 | | /// </summary> |
| | | 19 | | internal readonly ref struct Hashtable64_16 |
| | | 20 | | #if NET9_0_OR_GREATER |
| | | 21 | | : IHashtable |
| | | 22 | | #endif |
| | | 23 | | { |
| | | 24 | | |
| | | 25 | | private readonly ReadOnlySpan<ushort> _collide; |
| | | 26 | | |
| | | 27 | | private readonly ReadOnlySpan<ushort> _landmark; |
| | | 28 | | |
| | | 29 | | private readonly ulong _nHashBitMask; |
| | | 30 | | |
| | | 31 | | private readonly ushort[]? _buffer; |
| | | 32 | | |
| | | 33 | | public Hashtable64_16(ReadOnlySpan<byte> data) |
| | | 34 | | { |
| | 2920 | 35 | | var nLong = (int)data.Length / 8; |
| | | 36 | | |
| | 2920 | 37 | | if (nLong > ushort.MaxValue) |
| | 0 | 38 | | throw new ArgumentException($"Max allowed size is {ushort.MaxValue * 8}", nameof(data)); |
| | | 39 | | |
| | | 40 | | // Compute the hash table used to locate matching sections in the source. |
| | 2920 | 41 | | int nHash = Hash.TableSize(nLong); |
| | | 42 | | |
| | 2920 | 43 | | _nHashBitMask = (ulong)(nHash - 1); |
| | | 44 | | |
| | | 45 | | ushort[] buffer; |
| | | 46 | | |
| | 2920 | 47 | | if (nHash > 10_000) //avoid LOH |
| | | 48 | | { |
| | 20 | 49 | | _buffer = ArrayPool<ushort>.Shared.Rent(nHash * 2); |
| | 20 | 50 | | _buffer.AsSpan().Slice(0, nHash * 2).Clear(); |
| | 20 | 51 | | buffer = _buffer; |
| | | 52 | | } |
| | | 53 | | else |
| | | 54 | | { |
| | 2900 | 55 | | buffer = new ushort[nHash * 2]; |
| | | 56 | | } |
| | | 57 | | |
| | 2920 | 58 | | Span<ushort> collide = buffer.AsSpan().Slice(0, nHash); |
| | 2920 | 59 | | Span<ushort> landmark = buffer.AsSpan().Slice(nHash, nHash); |
| | | 60 | | |
| | | 61 | | //Span<ushort> collide = _memoryOwner.Memory.Span.Slice(0, nHash); |
| | | 62 | | //Span<ushort> landmark = _memoryOwner.Memory.Span.Slice(nHash, nHash); |
| | | 63 | | |
| | | 64 | | //Span<ushort> collide = new ushort[nHash * 2]; |
| | | 65 | | //Span<ushort> landmark = collide.Slice(nHash); |
| | | 66 | | //collide = collide.Slice(0, nHash); |
| | | 67 | | |
| | 2920 | 68 | | ReadOnlySpan<ulong> ulongData = MemoryMarshal.Cast<byte, ulong>(data).Slice(0, nLong); |
| | | 69 | | |
| | 6759680 | 70 | | for (var i = nLong - 1; i >= 0; i -= 1) |
| | | 71 | | { |
| | 3376920 | 72 | | var d = ulongData[i]; |
| | | 73 | | |
| | 3376920 | 74 | | var hv = Bucket(d); |
| | | 75 | | |
| | 3376920 | 76 | | collide[i] = landmark[hv]; |
| | | 77 | | |
| | 3376920 | 78 | | landmark[hv] = (ushort)(i + 1); |
| | | 79 | | } |
| | | 80 | | |
| | 2920 | 81 | | _collide = collide; |
| | 2920 | 82 | | _landmark = landmark; |
| | | 83 | | |
| | 2920 | 84 | | } |
| | | 85 | | |
| | | 86 | | |
| | | 87 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 88 | | public unsafe int Match(ReadOnlySpan<byte> data) |
| | | 89 | | { |
| | | 90 | | //we assume data is at least 8 bytes long to do a branchless read |
| | 3706580 | 91 | | var ulongData = Unsafe.ReadUnaligned<ulong>(ref MemoryMarshal.GetReference(data)); |
| | | 92 | | |
| | 3706580 | 93 | | var bucket = Bucket(ulongData); |
| | | 94 | | |
| | 3706580 | 95 | | fixed (ushort* p = _landmark) |
| | | 96 | | { |
| | 3706580 | 97 | | var pos = *(p + (uint)bucket); |
| | | 98 | | |
| | 3706580 | 99 | | return (pos - 1) * 8; |
| | | 100 | | } |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 104 | | public unsafe int More(int pos) |
| | | 105 | | { |
| | 258370160 | 106 | | fixed (ushort* p = _collide) |
| | | 107 | | { |
| | 258370160 | 108 | | var a = (uint)pos / 8; |
| | 258370160 | 109 | | var b = *(p + a); |
| | | 110 | | |
| | 258370160 | 111 | | return (b - 1) * 8; |
| | | 112 | | } |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 116 | | private int Bucket(ulong x) |
| | | 117 | | { |
| | 7083500 | 118 | | return (int)(Hash.Hash64(x) & _nHashBitMask); |
| | | 119 | | } |
| | | 120 | | |
| | | 121 | | public void Dispose() |
| | | 122 | | { |
| | 2920 | 123 | | if (_buffer is not null) |
| | 20 | 124 | | ArrayPool<ushort>.Shared.Return(_buffer); |
| | 2920 | 125 | | } |
| | | 126 | | |
| | | 127 | | public HashtableStats GetStats() |
| | | 128 | | { |
| | 10 | 129 | | return new HashtableStats |
| | 10 | 130 | | { |
| | 10 | 131 | | Collisions = _collide.Length - _collide.Count((byte)0), |
| | 10 | 132 | | Slots = _landmark.Length, |
| | 10 | 133 | | SlotsFilled = _landmark.Length - _landmark.Count((byte)0) |
| | 10 | 134 | | }; |
| | | 135 | | } |
| | | 136 | | } |