< Summary

Information
Class: DeltaSharp.Util.Hashtable.Hashtable64_8
Assembly: DeltaSharp
File(s): /home/runner/work/DeltaSharp/DeltaSharp/src/DeltaSharp/Util/Hashtable/Hashtable64_8.cs
Line coverage
100%
Covered lines: 33
Uncovered lines: 0
Coverable lines: 33
Total lines: 117
Line coverage: 100%
Branch coverage
100%
Covered branches: 10
Total branches: 10
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

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

File(s)

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

#LineLine coverage
 1using System;
 2using System.Buffers;
 3using System.Collections.Generic;
 4using System.Linq;
 5using System.Numerics;
 6using System.Runtime.CompilerServices;
 7using System.Runtime.InteropServices;
 8using System.Text;
 9using System.Threading.Tasks;
 10
 11namespace 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>
 17internal readonly ref struct Hashtable64_8
 18#if NET9_0_OR_GREATER
 19    : IHashtable
 20#endif
 21{
 22#if NETSTANDARD2_0
 78423    private readonly byte[] _collide = new byte[256];
 78424    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    {
 392040        var nInt = (int)data.Length / 8;
 41
 392042        if (nInt > byte.MaxValue)
 1043            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.
 312847        _collide = default;
 312848        _landmark = default;
 49#endif
 50
 391051        ReadOnlySpan<ulong> uintData = MemoryMarshal.Cast<byte, ulong>(data).Slice(0, nInt);
 52
 53
 84764054        for (var i = nInt - 1; i >= 0; i -= 1)
 55        {
 41991056            var d = uintData[i];
 57
 41991058            var hv = Bucket(d);
 59
 41991060            _collide[i] = _landmark[hv];
 61
 41991062            _landmark[hv] = (byte)(i + 1);
 63        }
 64
 391065    }
 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
 39764071        var ulongData = Unsafe.ReadUnaligned<ulong>(ref MemoryMarshal.GetReference(data));
 72
 39764073        var bucket = Bucket(ulongData);
 74
 39764075        var pos = _landmark[bucket];
 76
 39764077        return (pos - 1) * 8;
 78    }
 79
 80    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 81    public int More(int pos)
 82    {
 83157083        var i = (int)((uint)pos / 8);
 84
 83157085        return (_collide[i] - 1) * 8;
 86
 87    }
 88
 89    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 90    private int Bucket(ulong x)
 91    {
 81755092        return (int)(Hash.Hash64(x) & _nHashBitMask);
 93    }
 94
 95    public HashtableStats GetStats()
 96    {
 1097        var collisions = 256;
 1098        var filled = 256;
 514099        for (var i = 0; i < 256; i++)
 100        {
 4170101            if (_collide[i] == 0) collisions--;
 3520102            if (_landmark[i] == 0) filled--;
 103        }
 104
 10105        return new HashtableStats
 10106        {
 10107            Collisions = collisions,
 10108            Slots = 256,
 10109            SlotsFilled = filled
 10110        };
 111    }
 112
 113    public void Dispose()
 114    {
 115
 3910116    }
 117}