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 6 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
35 changes: 25 additions & 10 deletions neo/UInt160.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,26 +31,41 @@ 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;
//160 bit / 32 bit step -1
for (int i = (160 / 32 - 1); i >= 0; i--)
Copy link
Contributor

@jsolman jsolman Feb 27, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we were going to unroll to use 1 uint comparison and 2 ulong comparisons instead of 5 uint comparisons?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes,can do more test to find a fast way.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we resolve this, @jsolman? Maybe we could add this change in another PR after another tests.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be switched later

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I dismissed the review; this comment can be left open though while the PR can still be approved.

{
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;
}

/// <summary>
/// Method Equals returns true if objects are equal, false otherwise
/// </summary>
bool IEquatable<UInt160>.Equals(UInt160 other)
public unsafe bool Equals(UInt160 other)
{
return Equals(other);
fixed (byte* px = ToArray(), py = other.ToArray())
{
uint* lpx = (uint*)px;
uint* lpy = (uint*)py;
//160 bit / 32 bit(uint step) -1
for (int i = (160 / 32 - 1); i >= 0; i--)
{
if (lpx[i] != lpy[i])
return false;
}
}
return true;
}

/// <summary>
Expand Down
35 changes: 25 additions & 10 deletions neo/UInt256.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,26 +32,41 @@ 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;
//256bit / 64bit(ulong step) -1
for (int i = (256 / 64 - 1); 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;
}

/// <summary>
/// Method Equals returns true if objects are equal, false otherwise
/// </summary>
bool IEquatable<UInt256>.Equals(UInt256 other)
public unsafe bool Equals(UInt256 other)
{
return Equals(other);
fixed (byte* px = ToArray(), py = other.ToArray())
{
ulong* lpx = (ulong*)px;
ulong* lpy = (ulong*)py;
//256bit / 64bit(ulong step) -1
for (int i = (256 / 64 - 1); i >= 0; i--)
jsolman marked this conversation as resolved.
Show resolved Hide resolved
{
if (lpx[i] != lpy[i])
return false;
}
}
return true;
}

/// <summary>
Expand Down