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

Update RIPEMD160Managed to work w/ Spans #1760

Open
devhawk opened this issue Jul 9, 2020 · 2 comments · May be fixed by #3659
Open

Update RIPEMD160Managed to work w/ Spans #1760

devhawk opened this issue Jul 9, 2020 · 2 comments · May be fixed by #3659
Labels
Discussion Initial issue state - proposed but not yet accepted

Comments

@devhawk
Copy link
Contributor

devhawk commented Jul 9, 2020

Summary or problem description
Current implementation of RIPEMD160 doesn't work w/ Spans, so the Neo.Crypto.Helper.RIPEMD160(Span) overload has to copy the span into an array before hashing

Do you have any solution you want to propose?
DevHawk.RIPEMD160 is a netstandard2.1 implementation of RIPEMD160 that avoids the extra copy.

Neo Version

  • Neo 3

Where in the software does this update applies to?

  • Crypto
@devhawk devhawk added the Discussion Initial issue state - proposed but not yet accepted label Jul 9, 2020
@shargon
Copy link
Member

shargon commented Sep 1, 2020

According to my tests this version it's slower, could you check it @devhawk ?

[TestMethod]
        public void Test()
        {
            var rand = new Random();
            var data = new byte[10];
            rand.NextBytes(data);

            ReadOnlySpan<byte> span = data.AsSpan();

            var st = new Stopwatch(); st.Start();

            for (int x = 0; x < 100_000; x++)
            {
                span.ToArray().RIPEMD160Old();
            }

            st.Stop();
            Console.WriteLine("OLD: " + st.Elapsed);

            st = new Stopwatch(); st.Start();

            for (int x = 0; x < 100_000; x++)
            {
                span.RIPEMD160();
            }

            st.Stop();
            Console.WriteLine("NEW: " + st.Elapsed);
        }

public static byte[] RIPEMD160(this ReadOnlySpan<byte> value)
        {
            using var ripemd160 = new RIPEMD160();

            var output = new Span<byte>(new byte[Cryptography.RIPEMD160.HashSize]);
            if (!ripemd160.TryComputeHash(value, output, out _))
                throw new Exception();

            return output.ToArray();
        }
OLD: 00:00:00.1709104
NEW: 00:00:00.2378644

@devhawk
Copy link
Contributor Author

devhawk commented Sep 1, 2020

The version with spans generates less garbage over time. For long running services, garbage collecting can become a large issue. There's a reason the .NET team has been so focused on enabling span based processing in ASP.NET scenarios via libraries like Pipelines and Sys.Text.Json

shargon added a commit that referenced this issue Jan 8, 2025
@shargon shargon linked a pull request Jan 8, 2025 that will close this issue
15 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Discussion Initial issue state - proposed but not yet accepted
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants