Skip to content

Commit d8a437e

Browse files
committed
That's one small step for hash function, one giant leap for performance.
2.5% for this is nothing less but a giant leap 😸
1 parent eaef4ed commit d8a437e

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

1brc/Utf8Span.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,10 @@ public override int GetHashCode()
140140

141141
const uint prime = 16777619u;
142142

143-
if (Length >= 3)
144-
return (int)(((uint)Length * prime) ^ (*(uint*)(Pointer)));
143+
return Length >= 3 // no, moving condition inside does not help
144+
? (int)((*(uint*)Pointer * prime) ^ ((uint)Length))
145+
: (int)((*(ushort*)Pointer * prime) ^ ((uint)Length));
145146

146-
return (int)(uint)(*(ushort*)Pointer * prime);
147147
}
148148

149149
public override string ToString() => new((sbyte*)Pointer, 0, (int)Length, Encoding.UTF8);

0 commit comments

Comments
 (0)