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

Merged
merged 16 commits into from
Nov 7, 2024

Conversation

nan01ab
Copy link
Contributor

@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
src/Neo.VM/Utility.cs Outdated Show resolved Hide resolved
src/Neo.VM/Types/ByteString.cs Show resolved Hide resolved
src/Neo.VM/Types/ByteString.cs Show resolved Hide resolved
.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
Contributor 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

Copy link
Member

@cschuchardt88 cschuchardt88 Sep 23, 2024

Choose a reason for hiding this comment

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

Would like this method to be moved to Neo.Extensions library

src/Neo.VM/Utility.cs Outdated Show resolved Hide resolved
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
Contributor 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,

src/Neo.VM/Unsafe.cs Show resolved Hide resolved
@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
shargon
shargon previously approved these changes Sep 23, 2024
Jim8y
Jim8y previously approved these changes Sep 23, 2024
@shargon
Copy link
Member

shargon commented Sep 24, 2024

netstandard2.1 problems

Copy link
Member

@cschuchardt88 cschuchardt88 left a comment

Choose a reason for hiding this comment

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

Fix to make compatible with netstandard2.1. And Move NotZero method to Neo.Extensions library.

Error: /Users/runner/work/neo/neo/src/Neo.VM/Utility.cs(38,22): error CS1061: 'ReadOnlySpan<byte>' does not contain a definition for 'IndexOfAnyExcept' and no accessible extension method 'IndexOfAnyExcept' accepting a first argument of type 'ReadOnlySpan<byte>' could be found (are you missing a using directive or an assembly reference?) [/Users/runner/work/neo/neo/src/Neo.VM/Neo.VM.csproj::TargetFramework=netstandard2.1]

@nan01ab
Copy link
Contributor Author

nan01ab commented Sep 26, 2024

Fix to make compatible with netstandard2.1. And Move NotZero method to Neo.Extensions library.

Error: /Users/runner/work/neo/neo/src/Neo.VM/Utility.cs(38,22): error CS1061: 'ReadOnlySpan<byte>' does not contain a definition for 'IndexOfAnyExcept' and no accessible extension method 'IndexOfAnyExcept' accepting a first argument of type 'ReadOnlySpan<byte>' could be found (are you missing a using directive or an assembly reference?) [/Users/runner/work/neo/neo/src/Neo.VM/Neo.VM.csproj::TargetFramework=netstandard2.1]

🥲

@Jim8y Jim8y dismissed stale reviews from shargon and themself via aab0d3b November 6, 2024 04:21
@Jim8y Jim8y dismissed cschuchardt88’s stale review November 6, 2024 04:22

not part of this pr.

@Jim8y Jim8y requested a review from shargon November 6, 2024 04:31
Jim8y
Jim8y previously approved these changes Nov 6, 2024
Jim8y
Jim8y previously approved these changes Nov 6, 2024
@shargon shargon merged commit 64e13bb into neo-project:master Nov 7, 2024
6 of 7 checks passed
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.

6 participants