Skip to content

[Fix] References in StorageKey#3782

Merged
shargon merged 17 commits intoneo-project:masterfrom
cschuchardt88:fix/storagekey-fast-access
Mar 21, 2025
Merged

[Fix] References in StorageKey#3782
shargon merged 17 commits intoneo-project:masterfrom
cschuchardt88:fix/storagekey-fast-access

Conversation

@cschuchardt88
Copy link
Member

Description

Fixed references in the StorageKey, now makes copies for memory regions. We can't have data be overwritten. I believe this wasn't intended. Other optimizations like using Span instead of byte[] on the stack. Also wrote comments stating and showing the problem with not copying data in the pass. Also wrote tests to make sure data is copied and not referenced.

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

How Has This Been Tested?

  • Unit Tests
  • Local Testing

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

Jim8y
Jim8y previously approved these changes Feb 25, 2025
nan01ab
nan01ab previously approved these changes Feb 25, 2025
Copy link
Member

@shargon shargon left a comment

Choose a reason for hiding this comment

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

Can we have benchmark?

@shargon shargon dismissed stale reviews from nan01ab and Jim8y via f3af3ec February 26, 2025 04:34
/// <summary>
/// Initializes a new instance of the <see cref="StorageKey"/> class.
/// </summary>
/// <param name="cache">The cached byte array. NOTE: It must be read-only and can be modified by the caller.</param>
Copy link
Member

Choose a reason for hiding this comment

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

We have the note for this, i thing we create and duplicate without need

Copy link
Member Author

@cschuchardt88 cschuchardt88 Feb 26, 2025

Choose a reason for hiding this comment

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

I already shown how it can be changed in the comments and in a test.

Copy link
Member

Choose a reason for hiding this comment

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

This comment should be changed

@cschuchardt88
Copy link
Member Author

Can we have benchmark?

Its obvious.

@cschuchardt88
Copy link
Member Author

cschuchardt88 commented Mar 3, 2025

@shargon STOP CHANGING MY CODE.

@cschuchardt88 cschuchardt88 deleted the fix/storagekey-fast-access branch March 3, 2025 09:24
@shargon
Copy link
Member

shargon commented Mar 3, 2025

@shargon STOP CHANGING MY CODE.

what change?

@cschuchardt88
Copy link
Member Author

f3af3ec

The reason I did that is because yes ReadOnlyMemory and Memory can have a null pointer even though it isn't nullable.

@cschuchardt88 cschuchardt88 restored the fix/storagekey-fast-access branch March 3, 2025 10:26
@cschuchardt88 cschuchardt88 reopened this Mar 3, 2025
@cschuchardt88 cschuchardt88 requested review from Jim8y and nan01ab March 6, 2025 15:30
@shargon shargon added the Discussion Initial issue state - proposed but not yet accepted label Mar 10, 2025
@superboyiii
Copy link
Member

@cschuchardt88 Need resolve conflicts

@@ -255,30 +273,26 @@ public StorageKey()
/// </summary>
/// <param name="id">Contract Id</param>
/// <param name="cache">The cached byte array. NOTE: It must be read-only and can be modified by the caller.</param>
Copy link
Member

Choose a reason for hiding this comment

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

This comment should be changed

Copy link
Member

@shargon shargon left a comment

Choose a reason for hiding this comment

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

@cschuchardt88 Serialize should be faster than GetSpan, could you merge this request?

@Wi1l-B0t
Copy link
Contributor

I think this PR is OK.
If it has additional overhead, maybe it can be optimized later.

cschuchardt88 and others added 5 commits March 13, 2025 10:07
@cschuchardt88
Copy link
Member Author

@shargon Note that
image

The above it public for the Serializer and Value property is {get; init;} which means one would be able to change it. If they used var storageKey1 = new StorageKey { Id = 0, Key = keyMemory };. This PR fixes that issue as well.

Also I made all your changes.

prefix.CopyTo(buffer.AsSpan(sizeof(int)));
return buffer;
prefix.CopyTo(buffer[sizeof(int)..]);
return [.. buffer]; // Copy off stack
Copy link
Member

Choose a reason for hiding this comment

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

Why this copy if it's created inside the method?

Copy link
Member

@shargon shargon left a comment

Choose a reason for hiding this comment

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

Although I do not agree with some of these changes, as agreed in the core developers meeting, I will apply the changes in another pull request

@shargon shargon merged commit 6a1019c into neo-project:master Mar 21, 2025
7 checks passed
shargon added a commit that referenced this pull request Mar 21, 2025
@shargon shargon mentioned this pull request Mar 21, 2025
15 tasks
shargon added a commit that referenced this pull request Mar 21, 2025
* Review of #3782

* Clean

* Add note
Jim8y added a commit that referenced this pull request Mar 24, 2025
* master: (52 commits)
  Add SHA512 (#3845)
  Clean crypto (#3844)
  Review of #3782 (#3843)
  [`Fix`] References in `StorageKey` (#3782)
  Ensure `IInteroperable.Clone` (#3829)
  Ensure the interop types are serializable before commit (#3802)
  fix: ModInverse extension in BigIntegerExtensions (#3840)
  Style: avoid allocating emtpy array (#3841)
  Style: format json in tests/ (#3839)
  Reduce mem arguments (#3838)
  Avoid returning the entire mempool when only `maxTransactionsPerBlock` is required (#3823)
  style: use proper Assert methods (#3834)
  Nullable and fix `lock` in `SQLiteWallet` (#3816)
  [`Add`]: `Witness.Empty` for simplifing somethings (#3836)
  Style: format too-long literal strings (#3835)
  Add: NotNullWhen(true) for some out parameters (#3833)
  Update SQLiteWallet.cs (#3830)
  Revert "Optimize DeleteAccount"
  Revert "add lock to Version"
  Revert "Optimize LoadAccounts"
  ...

# Conflicts:
#	neo.sln
cschuchardt88 added a commit to cschuchardt88/neo that referenced this pull request Jun 8, 2025
* Fixed References in StorageKey

* Update src/Neo/SmartContract/StorageKey.cs

* Update src/Neo/SmartContract/StorageKey.cs

Co-authored-by: Christopher Schuchardt <cschuchardt88@gmail.com>

* Update src/Neo/SmartContract/StorageKey.cs

Co-authored-by: Shargon <shargon@gmail.com>

* Update src/Neo/SmartContract/StorageKey.cs

Co-authored-by: Shargon <shargon@gmail.com>

* Apply suggestions from code review

Co-authored-by: Shargon <shargon@gmail.com>

* Changed Text

---------

Co-authored-by: Jimmy <jinghui@wayne.edu>
Co-authored-by: Shargon <shargon@gmail.com>
Co-authored-by: NGD Admin <154295625+NGDAdmin@users.noreply.github.com>
cschuchardt88 pushed a commit to cschuchardt88/neo that referenced this pull request Jun 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Core Discussion Initial issue state - proposed but not yet accepted Ready to Merge

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants