-
Notifications
You must be signed in to change notification settings - Fork 1k
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
Base58 perfomance patch #3186
base: master
Are you sure you want to change the base?
Base58 perfomance patch #3186
Conversation
@kzorin52 thank you very much for contributing to neo. This pr looks good to me. |
src/Neo/Cryptography/Base58.cs
Outdated
@@ -74,19 +84,28 @@ public static byte[] Decode(string input) | |||
var bi = BigInteger.Zero; | |||
for (int i = 0; i < input.Length; i++) | |||
{ | |||
int digit = Alphabet.IndexOf(input[i]); | |||
if (digit < 0) | |||
var digit = s_mapBase58[(byte)input[i]]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Change to char array and avoid cast?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Char
wouldn't work I don't think. Char
uses encoding
.
src/Neo/Cryptography/Base58.cs
Outdated
private static int LeadingBase58Zeros(string collection) | ||
{ | ||
int i = 0; | ||
for (; i < collection.Length && collection[i] == s_zeroChar; i++) { } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cache length?
@shargon all fixed 🥳 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tested here and it is working basically.
However, no UTs nor benchmark.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to optimized with benchmarks data to show that it indeed faster or we can't merge to repo.
This change is not important.
src/Neo/Cryptography/Base58.cs
Outdated
@@ -27,6 +28,9 @@ public static class Base58 | |||
/// </summary> | |||
public const string Alphabet = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; | |||
|
|||
private static readonly char s_zeroChar = Alphabet[0]; | |||
private static readonly Lazy<IReadOnlyDictionary<char, int>> s_alphabetDic = new(() => Enumerable.Range(0, Alphabet.Length).ToDictionary(t => Alphabet[t], t => t)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This cost more in the current code in resources of memory management for the GC
in dotnet
.
@cschuchardt88 Okay, I'll do benchmarks and profiling of the code |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The optimisation itself is legit, the compatibility is preserved, but you're right in that we need some benchmarks. If possible, then for multiple architectures.
I'm sorry, I've been really busy lately. As soon as I have free time, I will be able to make the planned benchmarks. |
We have something wrong in the format |
@shargon there is no fix. Its a bug in Workaround is to wrap#pragma warning disable format // add this line
private static readonly int[] s_ints =
[
0, 1, 2, 3, 4
];
#pragma warning restore format // add this line |
@cschuchardt88 hi! like this? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be good if we hit in tests all the array values
Description
Various Base58 performance improvements:
Decode()
:IndexOf()
to Base58 character map.TakeWhile(...).Count()
to efficient variant, without memory and cpu-time lossbyte[] Concat()
to singleSpan<byte>
Type of change
How Has This Been Tested?
Checklist:
P.S. It is my first time opening a pull request, please correct me if I've done something wrong.