Skip to content

[Optimize]: Murmur128 is a noncryptographic hash aglorithm #3919

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

Merged

Conversation

Wi1l-B0t
Copy link
Contributor

@Wi1l-B0t Wi1l-B0t commented May 2, 2025

Description

Same as the murmur32, murmur128 is a noncryptographic hash aglorithm.
So it shouldn't be implemented as a cryptographic hash (This would be misleading.):

This PR:

  1. Implements murmur128 as a noncryptographic hash aglorithm;
  2. Optimizes murmur128 performance in some cases;

A simple benchmark:

Before:

.NET SDK 9.0.203
  [Host]     : .NET 9.0.4 (9.0.425.16305), Arm64 RyuJIT AdvSIMD
  DefaultJob : .NET 9.0.4 (9.0.425.16305), Arm64 RyuJIT AdvSIMD


| Method    | Mean     | Error    | StdDev   |
|---------- |---------:|---------:|---------:|
| Murmur128 | 81.65 us | 1.461 us | 1.295 us |

After:

.NET SDK 9.0.203
  [Host]     : .NET 9.0.4 (9.0.425.16305), Arm64 RyuJIT AdvSIMD
  DefaultJob : .NET 9.0.4 (9.0.425.16305), Arm64 RyuJIT AdvSIMD


| Method                | Mean     | Error    | StdDev   |
|---------------------- |---------:|---------:|---------:|
| Murmur128_ComputeHash | 40.74 us | 0.097 us | 0.090 us |

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

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

@Wi1l-B0t Wi1l-B0t changed the title [Fix&Optimize]: Murmur128 is a noncryptographic hash aglorithm [Optimize]: Murmur128 is a noncryptographic hash aglorithm May 2, 2025
shargon
shargon previously approved these changes May 3, 2025
@shargon shargon requested a review from Copilot May 3, 2025 11:32
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR implements a noncryptographic version of the Murmur128 hash algorithm and optimizes its performance while updating the associated tests and helper methods.

  • Implements Murmur128 as a noncryptographic hash algorithm by deriving from NonCryptographicHashAlgorithm.
  • Refactors the hash calculation to use a new Append/ComputeHash pattern with improved tail handling.
  • Updates tests and helper extensions to reflect the new API and improved performance.

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 2 comments.

File Description
tests/Neo.UnitTests/Cryptography/UT_Murmur128.cs Test adjustments to use the new ComputeHash method and HashSizeInBits.
src/Neo/Cryptography/Murmur128.cs Refactor of the Murmur128 implementation to optimize performance and tail processing.
src/Neo/Cryptography/Helper.cs Update Murmur128 extension methods to use ReadOnlySpan-based implementation.
benchmarks/Neo.Benchmarks/Benchmarks.Hash.cs Introduction of a benchmark for the Murmur128 performance.

Comment on lines +86 to +88
for (; source.Length >= 16; source = source[16..])
{
Mix(source);
Copy link
Preview

Copilot AI May 3, 2025

Choose a reason for hiding this comment

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

[nitpick] Consider refactoring this loop to use an index-based iteration instead of repeatedly slicing the source span. This may help reduce overhead in performance-critical paths.

Copilot uses AI. Check for mistakes.

Copy link
Member

Choose a reason for hiding this comment

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

@Wi1l-B0t Copilot is right, avoiding coy the array and using index it will be faster

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@Wi1l-B0t Copilot is right, avoiding coy the array and using index it will be faster

The source is a span. Slice is fast.

@shargon shargon merged commit cedb65c into neo-project:master May 4, 2025
7 checks passed
@Wi1l-B0t Wi1l-B0t deleted the feat.murmur128-is-noncryptogrphic-hash branch May 5, 2025 14:37
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.

2 participants