< Summary

Information
Class: DeltaSharp.Util.Hashtable.Hashtable64_16
Assembly: DeltaSharp
File(s): /home/runner/work/DeltaSharp/DeltaSharp/src/DeltaSharp/Util/Hashtable/Hashtable64_16.cs
Line coverage
97%
Covered lines: 39
Uncovered lines: 1
Coverable lines: 40
Total lines: 136
Line coverage: 97.5%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)83.33%6695.23%
Match(...)100%11100%
More(...)100%11100%
Bucket(...)100%11100%
Dispose()100%22100%
GetStats()100%11100%

File(s)

/home/runner/work/DeltaSharp/DeltaSharp/src/DeltaSharp/Util/Hashtable/Hashtable64_16.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
 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>
 19internal 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    {
 292035        var nLong = (int)data.Length / 8;
 36
 292037        if (nLong > ushort.MaxValue)
 038            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.
 292041        int nHash = Hash.TableSize(nLong);
 42
 292043        _nHashBitMask = (ulong)(nHash - 1);
 44
 45        ushort[] buffer;
 46
 292047        if (nHash > 10_000) //avoid LOH
 48        {
 2049            _buffer = ArrayPool<ushort>.Shared.Rent(nHash * 2);
 2050            _buffer.AsSpan().Slice(0, nHash * 2).Clear();
 2051            buffer = _buffer;
 52        }
 53        else
 54        {
 290055            buffer = new ushort[nHash * 2];
 56        }
 57
 292058        Span<ushort> collide = buffer.AsSpan().Slice(0, nHash);
 292059        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
 292068        ReadOnlySpan<ulong> ulongData = MemoryMarshal.Cast<byte, ulong>(data).Slice(0, nLong);
 69
 675968070        for (var i = nLong - 1; i >= 0; i -= 1)
 71        {
 337692072            var d = ulongData[i];
 73
 337692074            var hv = Bucket(d);
 75
 337692076            collide[i] = landmark[hv];
 77
 337692078            landmark[hv] = (ushort)(i + 1);
 79        }
 80
 292081        _collide = collide;
 292082        _landmark = landmark;
 83
 292084    }
 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
 370658091        var ulongData = Unsafe.ReadUnaligned<ulong>(ref MemoryMarshal.GetReference(data));
 92
 370658093        var bucket = Bucket(ulongData);
 94
 370658095        fixed (ushort* p = _landmark)
 96        {
 370658097            var pos = *(p + (uint)bucket);
 98
 370658099            return (pos - 1) * 8;
 100        }
 101    }
 102
 103    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 104    public unsafe int More(int pos)
 105    {
 258370160106        fixed (ushort* p = _collide)
 107        {
 258370160108            var a = (uint)pos / 8;
 258370160109            var b = *(p + a);
 110
 258370160111            return (b - 1) * 8;
 112        }
 113    }
 114
 115    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 116    private int Bucket(ulong x)
 117    {
 7083500118        return (int)(Hash.Hash64(x) & _nHashBitMask);
 119    }
 120
 121    public void Dispose()
 122    {
 2920123        if (_buffer is not null)
 20124            ArrayPool<ushort>.Shared.Return(_buffer);
 2920125    }
 126
 127    public HashtableStats GetStats()
 128    {
 10129        return new HashtableStats
 10130        {
 10131            Collisions = _collide.Length - _collide.Count((byte)0),
 10132            Slots = _landmark.Length,
 10133            SlotsFilled = _landmark.Length - _landmark.Count((byte)0)
 10134        };
 135    }
 136}