Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/DryDB/IKeyEncoding.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Buffers.Binary;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
Expand Down Expand Up @@ -262,11 +263,21 @@
/// byte-lexicographic order. Keys sharing an 8-byte prefix collide and fall back
/// to the full comparison.
/// </summary>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ulong GetKeyDigest(ReadOnlySpan<byte> key)
{
var length = Math.Min(key.Length, sizeof(ulong));
if (key.Length >= sizeof(ulong))
{
// Read the first 8 bytes as a big-endian number: one unaligned load plus
// one byte-swap instruction (ldr + rev / mov + bswap).
return BinaryPrimitives.ReverseEndianness(
Unsafe.ReadUnaligned<ulong>(ref MemoryMarshal.GetReference(key)));
}

// Shorter keys only: a whole-ulong load would read past the key, so pack the
// bytes individually (zero padding the low bits).
var digest = 0UL;
for (var i = 0; i < length; i++)
for (var i = 0; i < key.Length; i++)
{
digest |= (ulong)key[i] << (56 - i * 8);
}
Expand All @@ -292,7 +303,7 @@
bytesWritten = Encoding.ASCII.GetBytes(keyString, destination);
return true;
}
catch (ArgumentException ex)

Check warning on line 306 in src/DryDB/IKeyEncoding.cs

View workflow job for this annotation

GitHub Actions / test-dotnet

The variable 'ex' is declared but never used
{
bytesWritten = 0;
return false;
Expand All @@ -314,7 +325,7 @@
bytesWritten = Encoding.ASCII.GetBytes(formattedString, destination);
return true;
}
catch (ArgumentException ex)

Check warning on line 328 in src/DryDB/IKeyEncoding.cs

View workflow job for this annotation

GitHub Actions / test-dotnet

The variable 'ex' is declared but never used
{
bytesWritten = 0;
return false;
Expand Down
Loading