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

Updated KeyBuilder Fast Access #3768

Merged
merged 10 commits into from
Feb 20, 2025

Conversation

cschuchardt88
Copy link
Member

Description

Fixed with Disposing MemoryStream on ToArray(). Made a lot faster as well. Enforced Overflows and other lengths.

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 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

@nan01ab
Copy link
Contributor

nan01ab commented Feb 15, 2025

Any benchmark data?

_stream.WriteByte(prefix);
private void CheckLength(int length)
{
if ((length + _keyLength) > _cacheData.Length)
Copy link
Member

Choose a reason for hiding this comment

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

Increase it?

Copy link
Member Author

@cschuchardt88 cschuchardt88 Feb 15, 2025

Choose a reason for hiding this comment

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

why? Is not >= or what do you mean

Copy link
Member

Choose a reason for hiding this comment

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

I mean, increase the array instead of throw an error

Copy link
Member Author

@cschuchardt88 cschuchardt88 Feb 15, 2025

Choose a reason for hiding this comment

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

Since the Length is passed in, we need to enforce that length. 99% of the time we know the length up front?

Copy link
Member

Choose a reason for hiding this comment

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

Otherwise is not a hint, and is not working as before, it should not happen so it won't have any impact in the performance

Copy link
Member Author

Choose a reason for hiding this comment

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

I had @superboyiii test this on mainnet and testnet, no key is bigger than 64 bytes. So we need to enforce. If you want bigger key you must know the size before hand.

#3705 (review)

Copy link
Member

Choose a reason for hiding this comment

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

It haven't got any sense to me, throw an exception when is possible to increase the buffer, usability vs optimization? Could you clarify why we should not increase the buffer and preserve the previous behavior?

Copy link
Member Author

@cschuchardt88 cschuchardt88 Feb 16, 2025

Choose a reason for hiding this comment

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

I already did state that. You pass in a LENGTH. What's the point of doing that if it not enforced? Im sick of everything being programmed around these bugs. It needs to stop. Have some discipline.

@Jim8y
Copy link
Contributor

Jim8y commented Feb 19, 2025

I will add some benchmark for this pr, wait few a while.

@Jim8y
Copy link
Contributor

Jim8y commented Feb 19, 2025

BenchmarkDotNet v0.14.0, macOS Sonoma 14.6.1 (23G93) [Darwin 23.6.0]
Apple M1 Pro, 1 CPU, 10 logical and 10 physical cores
.NET SDK 9.0.102
[Host] : .NET 9.0.1 (9.0.124.61010), Arm64 RyuJIT AdvSIMD
DefaultJob : .NET 9.0.1 (9.0.124.61010), Arm64 RyuJIT AdvSIMD

Before this pr

Method DataSize Mean Error StdDev Ratio RatioSD Gen0 Allocated Alloc Ratio
KeyBuilder_AddByteArray 8 44.63 ns 0.943 ns 1.158 ns 1.00 0.04 0.0268 168 B 1.00
KeyBuilder_AddReadOnlySpan 8 44.18 ns 0.326 ns 0.272 ns 0.99 0.03 0.0268 168 B 1.00
KeyBuilder_AddSerializableSpan 8 34.30 ns 0.121 ns 0.107 ns 0.77 0.02 0.0268 168 B 1.00
KeyBuilder_AddByteArray 32 44.79 ns 0.308 ns 0.288 ns 1.00 0.01 0.0293 184 B 1.00
KeyBuilder_AddReadOnlySpan 32 43.70 ns 0.199 ns 0.177 ns 0.98 0.01 0.0293 184 B 1.00
KeyBuilder_AddSerializableSpan 32 43.42 ns 0.252 ns 0.224 ns 0.97 0.01 0.0293 184 B 1.00
KeyBuilder_AddByteArray 128 45.27 ns 0.107 ns 0.095 ns 1.00 0.00 0.0344 216 B 1.00
KeyBuilder_AddReadOnlySpan 128 46.20 ns 0.067 ns 0.056 ns 1.02 0.00 0.0344 216 B 1.00
KeyBuilder_AddSerializableSpan 128 45.83 ns 0.070 ns 0.066 ns 1.01 0.00 0.0344 216 B 1.00

After this pr

Method DataSize Mean Error StdDev Median Ratio RatioSD Gen0 Allocated Alloc Ratio
KeyBuilder_AddByteArray 8 21.47 ns 0.031 ns 0.024 ns 21.47 ns 1.00 0.00 0.0204 128 B 1.00
KeyBuilder_AddReadOnlySpan 8 26.38 ns 0.566 ns 1.429 ns 26.83 ns 1.23 0.07 0.0204 128 B 1.00
KeyBuilder_AddSerializableSpan 8 27.09 ns 0.225 ns 0.199 ns 27.06 ns 1.26 0.01 0.0204 128 B 1.00
KeyBuilder_AddByteArray 32 26.18 ns 0.077 ns 0.072 ns 26.20 ns 1.00 0.00 0.0229 144 B 1.00
KeyBuilder_AddReadOnlySpan 32 26.66 ns 0.063 ns 0.059 ns 26.68 ns 1.02 0.00 0.0229 144 B 1.00
KeyBuilder_AddSerializableSpan 32 27.84 ns 0.062 ns 0.058 ns 27.83 ns 1.06 0.00 0.0229 144 B 1.00
KeyBuilder_AddByteArray 128 27.91 ns 0.039 ns 0.037 ns 27.91 ns 1.00 0.00 0.0280 176 B 1.00
KeyBuilder_AddReadOnlySpan 128 28.68 ns 0.058 ns 0.049 ns 28.69 ns 1.03 0.00 0.0280 176 B 1.00
KeyBuilder_AddSerializableSpan 128 29.44 ns 0.042 ns 0.037 ns 29.44 ns 1.05 0.00 0.0280 176 B 1.00

@NGDAdmin NGDAdmin merged commit 0d467ee into neo-project:master Feb 20, 2025
6 of 7 checks passed
@shargon shargon deleted the fix/keybuilder-fast branch February 20, 2025 09:36
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