Skip to content
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

speed up Uint160.CompareTo and Uint256.CompareTo function #552

Merged
merged 21 commits into from
Mar 16, 2019
Merged
Show file tree
Hide file tree
Changes from 4 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
19 changes: 11 additions & 8 deletions neo/UInt160.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,19 @@ public UInt160(byte[] value)
/// Method CompareTo returns 1 if this UInt160 is bigger than other UInt160; -1 if it's smaller; 0 if it's equals
/// Example: assume this is 01ff00ff00ff00ff00ff00ff00ff00ff00ff00a4, this.CompareTo(02ff00ff00ff00ff00ff00ff00ff00ff00ff00a3) returns 1
/// </summary>
public int CompareTo(UInt160 other)
public unsafe int CompareTo(UInt160 other)
{
byte[] x = ToArray();
byte[] y = other.ToArray();
for (int i = x.Length - 1; i >= 0; i--)
fixed (byte* px = ToArray(), py = other.ToArray())
{
if (x[i] > y[i])
return 1;
if (x[i] < y[i])
return -1;
uint* lpx = (uint*)px;
uint* lpy = (uint*)py;
for (int i = 5; i >= 0; i--)
lightszero marked this conversation as resolved.
Show resolved Hide resolved
{
if (lpx[i] > lpy[i])
lightszero marked this conversation as resolved.
Show resolved Hide resolved
return 1;
if (lpx[i] < lpy[i])
return -1;
}
}
return 0;
}
Expand Down
19 changes: 11 additions & 8 deletions neo/UInt256.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,19 @@ public UInt256(byte[] value)
/// Method CompareTo returns 1 if this UInt256 is bigger than other UInt256; -1 if it's smaller; 0 if it's equals
/// Example: assume this is 01ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00a4, this.CompareTo(02ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00ff00a3) returns 1
/// </summary>
public int CompareTo(UInt256 other)
public unsafe int CompareTo(UInt256 other)
{
byte[] x = ToArray();
byte[] y = other.ToArray();
for (int i = x.Length - 1; i >= 0; i--)
fixed (byte* px = ToArray(), py = other.ToArray())
{
if (x[i] > y[i])
return 1;
if (x[i] < y[i])
return -1;
ulong* lpx = (ulong*)px;
ulong* lpy = (ulong*)py;
for (int i = 4; i >= 0; i--)
{
lightszero marked this conversation as resolved.
Show resolved Hide resolved
if (lpx[i] > lpy[i])
return 1;
if (lpx[i] < lpy[i])
return -1;
}
}
return 0;
}
Expand Down