< Summary

Information
Class: DeltaSharp.Util.Hashtable.Hashtable64_24
Assembly: DeltaSharp
File(s): /home/runner/work/DeltaSharp/DeltaSharp/src/DeltaSharp/Util/Hashtable/Hashtable64_24.cs
Line coverage
98%
Covered lines: 51
Uncovered lines: 1
Coverable lines: 52
Total lines: 156
Line coverage: 98%
Branch coverage
90%
Covered branches: 9
Total branches: 10
Branch coverage: 90%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)75%4495.23%
Match(...)100%11100%
More(...)100%11100%
Bucket(...)100%11100%
Write(...)100%11100%
Read(...)100%11100%
Read(...)100%11100%
GetStats()100%66100%
Dispose()100%11100%

File(s)

/home/runner/work/DeltaSharp/DeltaSharp/src/DeltaSharp/Util/Hashtable/Hashtable64_24.cs

#LineLine coverage
 1#if DELTASHARP_HASHTABLE24
 2using System;
 3using System.Buffers;
 4using System.Collections.Generic;
 5using System.Linq;
 6using System.Numerics;
 7using System.Reflection;
 8using System.Runtime.CompilerServices;
 9using System.Runtime.InteropServices;
 10using System.Text;
 11using System.Threading.Tasks;
 12
 13namespace DeltaSharp.Util.Hashtable;
 14
 15/// <summary>
 16/// Stores up to 16,777,215 complete 8-byte blocks (source lengths through 134,217,727 bytes)
 17/// using packed 24-bit, one-based block indices in a pooled byte array.
 18/// Reads mask a 32-bit value to 24 bits; writes use one byte and one 16-bit value
 19/// so neighboring entries remain unchanged.
 20/// </summary>
 21internal unsafe readonly ref struct Hashtable64_24
 22#if NET9_0_OR_GREATER
 23    : IHashtable
 24#endif
 25{
 26
 27    private readonly ReadOnlySpan<byte> _collide;
 28
 29    private readonly ReadOnlySpan<byte> _landmark;
 30
 31    private readonly ulong _nHashBitMask;
 32
 33    private readonly byte[] _buffer;
 34
 35    public Hashtable64_24(ReadOnlySpan<byte> data)
 36    {
 3537        var nLong = (int)data.Length / 8;
 38
 3539        if (nLong > 0x00_ff_ff_ff)
 040            throw new ArgumentException($"Max allowed size is {0x00_ff_ff_ff * 8}", nameof(data));
 41
 42        // Compute the hash table used to locate matching sections in the source.
 3543        int nHash = Hash.TableSize(nLong);
 44
 3545        _nHashBitMask = (ulong)(nHash - 1);
 46
 3547        var arraySize = (nHash * 3 + 1); //we need one extra byte to avoid special casing of the last element
 48
 3549        _buffer = ArrayPool<byte>.Shared.Rent(arraySize * 2);
 3550        _buffer.AsSpan().Slice(0,arraySize*2).Fill(0);
 51
 3552        Span<byte> collide = _buffer.AsSpan(0, arraySize);
 3553        Span<byte> landmark = _buffer.AsSpan(arraySize, arraySize);
 54
 3555        ReadOnlySpan<ulong> ulongData = MemoryMarshal.Cast<byte, ulong>(data).Slice(0, nLong);
 56
 3557        fixed (byte* collideP = collide)
 3558        fixed (byte* landmarkP = landmark)
 59        {
 524292060            for (var i = nLong - 1; i >= 0; i -= 1)
 61            {
 262142562                var d = ulongData[i];
 63
 262142564                var hv = Bucket(d);
 65
 262142566                Write(collideP, i, Read(landmarkP, hv));
 67
 262142568                Write(landmarkP, hv, i + 1);
 69
 70            }
 71        }
 72
 3573        _collide = collide;
 3574        _landmark = landmark;
 75
 3576    }
 77
 78    public int Match(ReadOnlySpan<byte> data)
 79    {
 80        //we assume data is at least 8 bytes long to do a branchless read
 46895081        var ulongData = Unsafe.ReadUnaligned<ulong>(ref MemoryMarshal.GetReference(data));
 82
 46895083        var bucket = Bucket(ulongData);
 84
 46895085        var pos = Read(_landmark, bucket);
 86
 46895087        return (int)((uint)pos - 1) * 8;
 88    }
 89
 90    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 91    public int More(int pos)
 92    {
 55758093        var a = (uint)pos / 8;
 55758094        var b = Read(_collide, (int)a);
 95
 55758096        return (b - 1) * 8;
 97
 98    }
 99
 100    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 101    private int Bucket(ulong x)
 102    {
 3090375103        return (int)(Hash.Hash64(x) & _nHashBitMask);
 104    }
 105
 106    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 107    private unsafe void Write(byte* p, int offset, int value)
 108    {
 5242850109        var low = (byte*)(p + (uint)offset * 3);
 5242850110        *low = (byte)value;
 111
 5242850112        var high = (ushort*)(low + 1);
 5242850113        *high = (ushort)(value >> 8);
 5242850114    }
 115
 116    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 117    private unsafe int Read(ReadOnlySpan<byte> arr, int offset)
 118    {
 1681890119        var v32p = Unsafe.ReadUnaligned<uint>(ref Unsafe.Add(ref MemoryMarshal.GetReference(arr), offset * 3));
 1681890120        return (int)((v32p) & 0x00_ff_ff_ff);
 121    }
 122
 123    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 124    private unsafe int Read(byte* p, int offset)
 125    {
 126        //read a whole uint and mask the last byte
 127
 2621425128        var v32p = (uint*)(p + (uint)offset * 3);
 2621425129        return (int)((*v32p) & 0x00_ff_ff_ff);
 130    }
 131
 132    public HashtableStats GetStats()
 133    {
 5134        var length = _landmark.Length / 3;
 5135        var landmark = length;
 5136        var collide = length;
 137
 655370138        for (var i = 0; i < length; i++)
 139        {
 447590140            if (Read(_landmark, i) == 0) landmark--;
 535455141            if (Read(_collide, i) == 0) collide--;
 142        }
 5143        return new HashtableStats
 5144        {
 5145            Collisions = collide,
 5146            Slots = length,
 5147            SlotsFilled = landmark
 5148        };
 149    }
 150
 151    public void Dispose()
 152    {
 35153        ArrayPool<byte>.Shared.Return(_buffer);
 35154    }
 155}
 156#endif