-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Fixed hashcode for StackItem #3531
base: master
Are you sure you want to change the base?
Changes from all commits
367d9b4
43e9d5d
eb1c86f
4f02ec7
2171b1d
7b7ec3c
7c280f7
17b3864
2162dfa
f30750f
a6f8575
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,6 +16,8 @@ namespace Neo.VM | |
{ | ||
unsafe internal static class Unsafe | ||
{ | ||
const long HashMagicNumber = 40343; | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static bool NotZero(ReadOnlySpan<byte> x) | ||
{ | ||
|
@@ -38,5 +40,40 @@ public static bool NotZero(ReadOnlySpan<byte> x) | |
} | ||
return false; | ||
} | ||
|
||
/// <summary> | ||
/// Get 64-bit hash code for a byte array | ||
/// </summary> | ||
/// <param name="span">Span</param> | ||
/// <returns></returns> | ||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public static long HashBytes(ReadOnlySpan<byte> span) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Return int and avoid casts? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it's an 64-bit hash function. However will be safe because of the VM limits, but anything bigger than 2GB it will overflow. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any other int32 hash method? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here a simple one. Just remember the one I in code atm is faster public override int GetHashCode()
{
if (byteArray == null || byteArray.Length == 0)
return 0;
unchecked // Overflow is fine, just wrap
{
int hash = 17; // Seed with a prime number
foreach (byte b in byteArray)
{
hash = hash * 31 + b; // Multiply by a prime number and add byte value
}
return hash;
}
} |
||
{ | ||
var len = span.Length; | ||
var hashState = (ulong)len; | ||
|
||
fixed (byte* k = span) | ||
{ | ||
var pwString = (char*)k; | ||
var cbBuf = len / 2; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @cschuchardt88 it doesn't iterate all the span? is that expected @cschuchardt88 ? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Its using char* witch is 2-bytes. |
||
|
||
for (var i = 0; i < cbBuf; i++, pwString++) | ||
hashState = HashMagicNumber * hashState + *pwString; | ||
|
||
if ((len & 1) > 0) | ||
{ | ||
var pC = (byte*)pwString; | ||
hashState = HashMagicNumber * hashState + *pC; | ||
} | ||
} | ||
|
||
return (long)Rotr64(HashMagicNumber * hashState, 4); | ||
} | ||
|
||
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
internal static ulong Rotr64(ulong x, int n) | ||
{ | ||
return ((x) >> n) | ((x) << (64 - n)); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this a good practice?
Then we will need a specific check for checking is it is 0.
Why not null for example?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@shargon added, but it
int
cant benull
, plus its cachedThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Invalidate reset the hash method, is only in buffer items