| | | 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 | | /// <summary> |
| | | 14 | | /// Stores up to 255 complete 8-byte blocks (source lengths through 2047 bytes). |
| | | 15 | | /// Uses inline buffers on modern targets and byte arrays on .NET Standard 2.0. |
| | | 16 | | /// </summary> |
| | | 17 | | internal readonly ref struct Hashtable64_8 |
| | | 18 | | #if NET9_0_OR_GREATER |
| | | 19 | | : IHashtable |
| | | 20 | | #endif |
| | | 21 | | { |
| | | 22 | | #if NETSTANDARD2_0 |
| | 784 | 23 | | private readonly byte[] _collide = new byte[256]; |
| | 784 | 24 | | private readonly byte[] _landmark = new byte[256]; |
| | | 25 | | #else |
| | | 26 | | [InlineArray(256)] |
| | | 27 | | struct ByteArray256 |
| | | 28 | | { |
| | | 29 | | byte Element; |
| | | 30 | | } |
| | | 31 | | |
| | | 32 | | private readonly ByteArray256 _collide; |
| | | 33 | | private readonly ByteArray256 _landmark; |
| | | 34 | | #endif |
| | | 35 | | |
| | | 36 | | private const ulong _nHashBitMask = 255; |
| | | 37 | | |
| | | 38 | | public Hashtable64_8(ReadOnlySpan<byte> data) |
| | | 39 | | { |
| | 3920 | 40 | | var nInt = (int)data.Length / 8; |
| | | 41 | | |
| | 3920 | 42 | | if (nInt > byte.MaxValue) |
| | 10 | 43 | | throw new ArgumentException($"Max allowed size is {(byte.MaxValue + 1) * 8 - 1}", nameof(data)); |
| | | 44 | | |
| | | 45 | | #if !NETSTANDARD2_0 |
| | | 46 | | // Clear whole inline buffers instead of writing each element in a loop. |
| | 3128 | 47 | | _collide = default; |
| | 3128 | 48 | | _landmark = default; |
| | | 49 | | #endif |
| | | 50 | | |
| | 3910 | 51 | | ReadOnlySpan<ulong> uintData = MemoryMarshal.Cast<byte, ulong>(data).Slice(0, nInt); |
| | | 52 | | |
| | | 53 | | |
| | 847640 | 54 | | for (var i = nInt - 1; i >= 0; i -= 1) |
| | | 55 | | { |
| | 419910 | 56 | | var d = uintData[i]; |
| | | 57 | | |
| | 419910 | 58 | | var hv = Bucket(d); |
| | | 59 | | |
| | 419910 | 60 | | _collide[i] = _landmark[hv]; |
| | | 61 | | |
| | 419910 | 62 | | _landmark[hv] = (byte)(i + 1); |
| | | 63 | | } |
| | | 64 | | |
| | 3910 | 65 | | } |
| | | 66 | | |
| | | 67 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 68 | | public int Match(ReadOnlySpan<byte> data) |
| | | 69 | | { |
| | | 70 | | //we assume data is at least 8 bytes long to do a branchless read |
| | 397640 | 71 | | var ulongData = Unsafe.ReadUnaligned<ulong>(ref MemoryMarshal.GetReference(data)); |
| | | 72 | | |
| | 397640 | 73 | | var bucket = Bucket(ulongData); |
| | | 74 | | |
| | 397640 | 75 | | var pos = _landmark[bucket]; |
| | | 76 | | |
| | 397640 | 77 | | return (pos - 1) * 8; |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 81 | | public int More(int pos) |
| | | 82 | | { |
| | 831570 | 83 | | var i = (int)((uint)pos / 8); |
| | | 84 | | |
| | 831570 | 85 | | return (_collide[i] - 1) * 8; |
| | | 86 | | |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | [MethodImpl(MethodImplOptions.AggressiveInlining)] |
| | | 90 | | private int Bucket(ulong x) |
| | | 91 | | { |
| | 817550 | 92 | | return (int)(Hash.Hash64(x) & _nHashBitMask); |
| | | 93 | | } |
| | | 94 | | |
| | | 95 | | public HashtableStats GetStats() |
| | | 96 | | { |
| | 10 | 97 | | var collisions = 256; |
| | 10 | 98 | | var filled = 256; |
| | 5140 | 99 | | for (var i = 0; i < 256; i++) |
| | | 100 | | { |
| | 4170 | 101 | | if (_collide[i] == 0) collisions--; |
| | 3520 | 102 | | if (_landmark[i] == 0) filled--; |
| | | 103 | | } |
| | | 104 | | |
| | 10 | 105 | | return new HashtableStats |
| | 10 | 106 | | { |
| | 10 | 107 | | Collisions = collisions, |
| | 10 | 108 | | Slots = 256, |
| | 10 | 109 | | SlotsFilled = filled |
| | 10 | 110 | | }; |
| | | 111 | | } |
| | | 112 | | |
| | | 113 | | public void Dispose() |
| | | 114 | | { |
| | | 115 | | |
| | 3910 | 116 | | } |
| | | 117 | | } |