< Summary

Information
Class: DeltaSharp.Util.Hashtable.Hashtable64_32
Assembly: DeltaSharp
File(s): /home/runner/work/DeltaSharp/DeltaSharp/src/DeltaSharp/Util/Hashtable/Hashtable64_32.cs
Line coverage
100%
Covered lines: 33
Uncovered lines: 0
Coverable lines: 33
Total lines: 122
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
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%22100%
Match(...)100%11100%
More(...)100%11100%
Bucket(...)100%11100%
GetStats()100%11100%
Dispose()100%11100%

File(s)

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

#LineLine coverage
 1using System;
 2using System.Buffers;
 3using System.Collections.Generic;
 4using System.Linq;
 5using System.Net.Sockets;
 6using System.Numerics;
 7using System.Runtime.CompilerServices;
 8using System.Runtime.InteropServices;
 9using System.Text;
 10using System.Threading.Tasks;
 11
 12namespace DeltaSharp.Util.Hashtable;
 13
 14/// <summary>
 15/// Stores source landmarks and collision chains using 32-bit, zero-based block indices
 16/// in a pooled integer array, with -1 marking empty slots. The indices address all complete
 17/// 8-byte blocks in an int-sized source span; allocation remains subject to available memory
 18/// and runtime array-size limits.
 19/// </summary>
 20internal unsafe readonly ref struct Hashtable64_32
 21#if NET9_0_OR_GREATER
 22    : IHashtable
 23#endif
 24{
 25
 26    private readonly ReadOnlySpan<int> _collide;
 27
 28    private readonly ReadOnlySpan<int> _landmark;
 29
 30    private readonly ulong _nHashBitMask;
 31
 32    private readonly int[] _buffer;
 33
 34    public Hashtable64_32(ReadOnlySpan<byte> data)
 35    {
 36        //In C# arrays are limited to Int32.MaxValue elements
 37        //So we can safely assume here that the data length will not exceed 2,147,483,640 bytes
 38        //We can use the sign bit to indicate empty slots and avoid the extra checks and shifts needed in the other vers
 39
 6040        var nLong = (int)data.Length / 8;
 41
 42        // Compute the hash table used to locate matching sections in the source.
 6043        int nHash = Hash.TableSize(nLong);
 44
 6045        ulong bucketMask = (ulong)(nHash - 1);
 6046        _nHashBitMask = bucketMask;
 47
 6048        _buffer = ArrayPool<int>.Shared.Rent(nHash * 2);
 6049        Span<int> landmark = _buffer.AsSpan(nHash, nHash);
 6050        landmark.Fill(-1);
 51        // Every live collision link is assigned during insertion below.
 6052        Span<int> collide = _buffer.AsSpan(0, nLong);
 53
 6054        ReadOnlySpan<ulong> ulongData = MemoryMarshal.Cast<byte, ulong>(data).Slice(0, nLong);
 55
 56        unsafe
 57        {
 6058            fixed (int* collideP = collide)
 6059            fixed (int* landmarkP = landmark)
 60            {
 34013218061                for (var i = nLong - 1; i >= 0; i -= 1)
 62                {
 17006603063                    var d = ulongData[i];
 64
 17006603065                    nuint hv = (nuint)(Hash.Hash64(d) & bucketMask);
 66
 17006603067                    *(collideP + i) = landmarkP[hv];
 17006603068                    *(landmarkP + hv) = i;
 69                }
 70            }
 71        }
 72
 6073        _collide = collide;
 6074        _landmark = landmark;
 75
 6076    }
 77
 78
 79
 80    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 81    public int Match(ReadOnlySpan<byte> data)
 82    {
 83        //assume data is at least 8 bytes long to do a branchless read
 46896584        var ulongData = Unsafe.ReadUnaligned<ulong>(ref MemoryMarshal.GetReference(data));
 85
 46896586        var bucket = Bucket(ulongData);
 87
 88        //branchless read since bucket will always be within range
 46896589        int pos = _landmark[bucket];
 90
 46896591        return pos * 8;
 92    }
 93
 94    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 95    public int More(int pos)
 96    {
 97        //branchless read
 98        //index is always within range
 55763099        return _collide[(int)((uint)pos / 8)] * 8;
 100    }
 101
 102    [MethodImpl(MethodImplOptions.AggressiveInlining)]
 103    private int Bucket(ulong x)
 104    {
 468965105        return (int)(Hash.Hash64(x) & _nHashBitMask);
 106    }
 107
 108    public HashtableStats GetStats()
 109    {
 30110        return new HashtableStats
 30111        {
 30112            Collisions = _collide.Length - _collide.Count(-1),
 30113            Slots = _landmark.Length,
 30114            SlotsFilled = _landmark.Length - _landmark.Count(-1)
 30115        };
 116    }
 117
 118    public void Dispose()
 119    {
 60120        ArrayPool<int>.Shared.Return(_buffer);
 60121    }
 122}