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

[Fix] unalignment memory load in Neo.VM Unsafe.NotZero(ReadOnlySpan<byte>) and remove the use of unsafe #3492

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

nan01ab
Copy link

@nan01ab nan01ab commented Sep 18, 2024

Description

The Unsafe.NotZero(ReadOnlySpan<byte>) method in Neo.VM use long* to load data from ReadOnlySpan<byte> for better performance, but this approach may result in misaligned memory access. This approach is no problem on x86 CPUs, but it may cause bug on some RISC CPUs(for example, some ARM CPUs).

Fixes # (issue)

Type of change

  • Optimization (the change is only an optimization)
  • Style (the change is only a code style for better maintenance or standard purpose)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Test Configuration:

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@nan01ab nan01ab changed the title fix: unalignment memory load in Neo.VM Unsafe.NotZero(ReadOnlySpan<byte>) [fix] unalignment memory load in Neo.VM Unsafe.NotZero(ReadOnlySpan<byte>) Sep 18, 2024
@nan01ab nan01ab changed the title [fix] unalignment memory load in Neo.VM Unsafe.NotZero(ReadOnlySpan<byte>) [Fix] unalignment memory load in Neo.VM Unsafe.NotZero(ReadOnlySpan<byte>) Sep 18, 2024
.gitignore Outdated Show resolved Hide resolved
for (; i < x.Length; i++)
{
if (x[i] != 0) return true;
}
Copy link
Member

@AnnaShaleva AnnaShaleva Sep 18, 2024

Choose a reason for hiding this comment

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

There's a difference in the existing approach comparing to the old code: new code starts comparison from the head of x, and the old code started comparison from the tail of x. GetBoolean is kind of a hot-path code in the VM, thus it would be interesting to see how this bugfix affects the performance (time) of DB restore from dump for mainnet/testnet.

Copy link
Author

Choose a reason for hiding this comment

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

The old code started comparison from the head of x too.

Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't this work?

foreach (var b in Span)
    if (b != 0) return true;

Copy link
Author

@nan01ab nan01ab Sep 19, 2024

Choose a reason for hiding this comment

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

There's a difference in the existing approach comparing to the old code: new code starts comparison from the head of x, and the old code started comparison from the tail of x. GetBoolean is kind of a hot-path code in the VM, thus it would be interesting to see how this bugfix affects the performance (time) of DB restore from dump for mainnet/testnet.

There's a difference in the existing approach comparing to the old code: new code starts comparison from the head of x, and the old code started comparison from the tail of x. GetBoolean is kind of a hot-path code in the VM, thus it would be interesting to see how this bugfix affects the performance (time) of DB restore from dump for mainnet/testnet.

Now the performance has been optimized(by ContainsAnyExcept because it can use SIMD).

Intel Core i7-8750H CPU 2.20GHz (Coffee Lake), 1 CPU, 12 logical and 6 physical cores
.NET SDK 8.0.203
  [Host]     : .NET 8.0.3 (8.0.324.11423), X64 RyuJIT AVX2
  DefaultJob : .NET 8.0.3 (8.0.324.11423), X64 RyuJIT AVX2


| Method            | length | Mean     | Error     | StdDev    |
|------------------ |------- |---------:|----------:|----------:|
| LongPointer       | 10     | 4.221 ns | 0.0285 ns | 0.0222 ns |
| ContainsAnyExcept | 10     | 6.324 ns | 0.0619 ns | 0.0548 ns |
| LongPointer       | 20     | 5.567 ns | 0.0756 ns | 0.0670 ns |
| ContainsAnyExcept | 20     | 6.820 ns | 0.0935 ns | 0.0829 ns |
| LongPointer       | 30     | 7.218 ns | 0.0847 ns | 0.0751 ns |
| ContainsAnyExcept | 30     | 6.545 ns | 0.0514 ns | 0.0429 ns |

The new implementation performs better in some cases, especially if the bytes is relatively long.

@@ -85,7 +85,7 @@ internal bool Equals(StackItem? other, ref uint limits)
public override bool GetBoolean()
{
if (Size > Integer.MaxSize) throw new InvalidCastException();
return Unsafe.NotZero(GetSpan());
return GetSpan().NotZero();
Copy link
Member

Choose a reason for hiding this comment

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

The implementation is quite straightforward, but GetBoolean is frequently-used in VM, thus we have to check mainnet/testnet networks for compatibility. There should be no issues, but let's do it just in case.

Copy link
Member

Choose a reason for hiding this comment

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

@superboyiii could you check it?

@@ -85,7 +85,7 @@ internal bool Equals(StackItem? other, ref uint limits)
public override bool GetBoolean()
{
if (Size > Integer.MaxSize) throw new InvalidCastException();
return Unsafe.NotZero(GetSpan());
return GetSpan().NotZero();
Copy link
Member

Choose a reason for hiding this comment

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

@superboyiii could you check it?

.gitignore Outdated Show resolved Hide resolved
@@ -27,6 +27,23 @@ static Utility()
StrictUTF8.EncoderFallback = EncoderFallback.ExceptionFallback;
}

public static bool NotZero(this ReadOnlySpan<byte> x)
Copy link
Member

Choose a reason for hiding this comment

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

This needs to be moved to the extensions library. For ReadOnlySpans.cs

Copy link
Author

Choose a reason for hiding this comment

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

This needs to be moved to the extensions library. For ReadOnlySpans.cs

Move it with the rest of the content together?

Copy link
Member

Choose a reason for hiding this comment

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

Just that extension method

for (; i < x.Length; i++)
{
if (x[i] != 0) return true;
}
Copy link
Member

Choose a reason for hiding this comment

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

Wouldn't this work?

foreach (var b in Span)
    if (b != 0) return true;

int i;
for (i = 0; i + 7 < x.Length; i += 8)
{
if ((x[i] | x[i + 1] | x[i + 2] | x[i + 3] | x[i + 4] | x[i + 5] | x[i + 6] | x[i + 7]) != 0)
Copy link
Member

Choose a reason for hiding this comment

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

This won't be faster as previous version

Copy link
Author

Choose a reason for hiding this comment

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

This won't be faster as previous version

This won't be faster as previous version

The performance has been optimized,

{
int len = x.Length;
if (len == 0) return false;
fixed (byte* xp = x)
Copy link
Member

Choose a reason for hiding this comment

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

could you explain more how this could be wrong in different platform? could you write a unit test for it?

Copy link
Author

Choose a reason for hiding this comment

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

Align of long is 8, and align of byte is 1, so byte* may not be aligned to 8.

@nan01ab nan01ab changed the title [Fix] unalignment memory load in Neo.VM Unsafe.NotZero(ReadOnlySpan<byte>) [Fix] unalignment memory load in Neo.VM Unsafe.NotZero(ReadOnlySpan<byte>) and remove the use of unsafe Sep 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants