Skip to content

[Event Hub] PartitionResolver Jenkin3 optimizations #50068

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
May 19, 2025
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions sdk/eventhub/Azure.Messaging.EventHubs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@

## 5.13.0-beta.1 (Unreleased)

### Acknowledgments

Thank you to our developer community members who helped to make the Event Hubs client libraries better with their contributions to this release:

- Daniel Marbach _([GitHub](https://github.com/danielmarbach))_

### Features Added

### Breaking Changes
Expand All @@ -10,6 +16,8 @@

### Other Changes

- Significantly improved the performance of the Jenkins3 hash computation used for partition key resolution. Across various input sizes, the updated implementation achieves up to 39% faster hash calculation, with the most notable gains seen for smaller keys (8–32 bytes), and consistent improvements across all sizes. The new approach maintains the exact bit-for-bit hash output while reducing overhead. _(A community contribution, courtesy of [danielmarbach](https://github.com/danielmarbach))_

## 5.12.1 (2025-04-09)

### Bugs Fixed
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Buffers;
using System.Buffers.Binary;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;

Expand Down Expand Up @@ -149,17 +150,31 @@ private static void ComputeHash(ReadOnlySpan<byte> data,
out uint hash1,
out uint hash2)
{
uint a, b, c;
uint len = (uint)data.Length;
uint a = 0xDEADBEEF + len + seed1;
uint b = a;
uint c = a + seed2;

a = b = c = (uint)(0xdeadbeef + data.Length + seed1);
c += seed2;
int chunks = data.Length > 12 ? (data.Length - 1) / 12 : 0;

int index = 0, size = data.Length;
while (size > 12)
ref byte ptr = ref MemoryMarshal.GetReference(data);
for (int i = 0; i < chunks; i++)
{
a += BinaryPrimitives.ReadUInt32LittleEndian(data.Slice(index));
b += BinaryPrimitives.ReadUInt32LittleEndian(data.Slice(index + 4));
c += BinaryPrimitives.ReadUInt32LittleEndian(data.Slice(index + 8));
uint w0 = Unsafe.ReadUnaligned<uint>(ref ptr);
uint w1 = Unsafe.ReadUnaligned<uint>(ref Unsafe.Add(ref ptr, 4));
uint w2 = Unsafe.ReadUnaligned<uint>(ref Unsafe.Add(ref ptr, 8));
ptr = ref Unsafe.Add(ref ptr, 12);

if (!BitConverter.IsLittleEndian)
{
w0 = BinaryPrimitives.ReverseEndianness(w0);
w1 = BinaryPrimitives.ReverseEndianness(w1);
w2 = BinaryPrimitives.ReverseEndianness(w2);
}

a += w0;
b += w1;
c += w2;

a -= c;
a ^= (c << 4) | (c >> 28);
Expand All @@ -184,51 +199,51 @@ private static void ComputeHash(ReadOnlySpan<byte> data,
c -= b;
c ^= (b << 4) | (b >> 28);
b += a;

index += 12;
size -= 12;
}

switch (size)
int consumed = chunks * 12;
ref byte tail = ref Unsafe.Add(ref MemoryMarshal.GetReference(data), consumed);
int left = data.Length - consumed;
switch (left)
{
case 12:
a += BinaryPrimitives.ReadUInt32LittleEndian(data.Slice(index));
b += BinaryPrimitives.ReadUInt32LittleEndian(data.Slice(index + 4));
c += BinaryPrimitives.ReadUInt32LittleEndian(data.Slice(index + 8));
a += BitConverter.IsLittleEndian ? Unsafe.ReadUnaligned<uint>(ref tail) : BinaryPrimitives.ReverseEndianness(Unsafe.ReadUnaligned<uint>(ref tail));
b += BitConverter.IsLittleEndian ? Unsafe.ReadUnaligned<uint>(ref Unsafe.Add(ref tail, 4)) : BinaryPrimitives.ReverseEndianness(Unsafe.ReadUnaligned<uint>(ref Unsafe.Add(ref tail, 4)));
c += BitConverter.IsLittleEndian ? Unsafe.ReadUnaligned<uint>(ref Unsafe.Add(ref tail, 8)) : BinaryPrimitives.ReverseEndianness(Unsafe.ReadUnaligned<uint>(ref Unsafe.Add(ref tail, 8)));
break;
case 11:
c += ((uint)data[index + 10]) << 16;
c += (uint)Unsafe.Add(ref tail, 10) << 16;
goto case 10;
case 10:
c += ((uint)data[index + 9]) << 8;
c += (uint)Unsafe.Add(ref tail, 9) << 8;
goto case 9;
case 9:
c += (uint)data[index + 8];
c += Unsafe.Add(ref tail, 8);
goto case 8;
case 8:
b += BinaryPrimitives.ReadUInt32LittleEndian(data.Slice(index + 4));
a += BinaryPrimitives.ReadUInt32LittleEndian(data.Slice(index));
b += BitConverter.IsLittleEndian ? Unsafe.ReadUnaligned<uint>(ref Unsafe.Add(ref tail, 4)) : BinaryPrimitives.ReverseEndianness(Unsafe.ReadUnaligned<uint>(ref Unsafe.Add(ref tail, 4)));
a += BitConverter.IsLittleEndian ? Unsafe.ReadUnaligned<uint>(ref tail) : BinaryPrimitives.ReverseEndianness(Unsafe.ReadUnaligned<uint>(ref tail));
break;
case 7:
b += ((uint)data[index + 6]) << 16;
b += (uint)Unsafe.Add(ref tail, 6) << 16;
goto case 6;
case 6:
b += ((uint)data[index + 5]) << 8;
b += (uint)Unsafe.Add(ref tail, 5) << 8;
goto case 5;
case 5:
b += (uint)data[index + 4];
b += Unsafe.Add(ref tail, 4);
goto case 4;
case 4:
a += BinaryPrimitives.ReadUInt32LittleEndian(data.Slice(index));
a += BitConverter.IsLittleEndian ? Unsafe.ReadUnaligned<uint>(ref tail) : BinaryPrimitives.ReverseEndianness(Unsafe.ReadUnaligned<uint>(ref tail));
break;
case 3:
a += ((uint)data[index + 2]) << 16;
a += (uint)Unsafe.Add(ref tail, 2) << 16;
goto case 2;
case 2:
a += ((uint)data[index + 1]) << 8;
a += (uint)Unsafe.Add(ref tail, 1) << 8;
goto case 1;
case 1:
a += (uint)data[index];
a += Unsafe.Add(ref tail, 0);
break;
case 0:
hash1 = c;
Expand Down