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

perf: change ripmd160 from class to struct to avoid allocations. #121

Merged
merged 1 commit into from
May 31, 2024

Conversation

RokasBalevicius
Copy link
Contributor

@RokasBalevicius RokasBalevicius commented May 16, 2024

Main change: Ripemd160 hash calculator can be a struct as it is used only in one place and its instance is essentially ephemeral. No need to allocate it on heap.

Benchmark results:

BenchmarkDotNet v0.13.12, macOS Sonoma 14.4.1 (23E224) [Darwin 23.4.0]
Intel Core i7-1068NG7 CPU 2.30GHz, 1 CPU, 8 logical and 4 physical cores
.NET SDK 8.0.100
  [Host]     : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2
  DefaultJob : .NET 8.0.0 (8.0.23.53103), X64 RyuJIT AVX2


| Method                     | Mean     | Error     | StdDev    | Gen0     | Gen1     | Allocated |
|--------------------------- |---------:|----------:|----------:|---------:|---------:|----------:|
| DefaultKeyCreation         | 7.182 ms | 0.1436 ms | 0.4025 ms | 984.3750 | 468.7500 |   3.97 MB |
| KeyCreationWithValueRipemd | 6.294 ms | 0.1123 ms | 0.1499 ms | 757.8125 | 375.0000 |   3.28 MB |

Benchmark code:

using Aerospike.Client;
using BenchmarkDotNet.Attributes;

[MemoryDiagnoser]
public class AeorospikeKeysBench
{
    private string[] _ids;

    public AeorospikeKeysBench()
    {
        var count = 10000;

        _ids = new string[count];

        for (int i = 0; i < count; i++)
        {
            _ids[i] = Guid.NewGuid().ToString();
        }
    }

    [Benchmark]
    public List<Key> DefaultKeyCreation()
    {
        var result = new List<Key>(_ids.Length);
        for (int i = 0; i < _ids.Length; i++)
        {
            var key = new Key("ns", "setName", _ids[i]);
            result.Add(key);
        }

        return result;
    }

    [Benchmark]
    public List<Key2> KeyCreationWithValueRipemd()
    {
        var result = new List<Key2>(_ids.Length);
        for (int i = 0; i < _ids.Length; i++)
        {
            var key = new Key2("ns", "setName", _ids[i]);
            result.Add(key);
        }

        return result;
    }
}

I used 10k keys, because we have use cases where we need to prepare thousands of keys in single request, so I just lazily took benchmarks I already had. Single key benchmark should show similar levels of improvement in allocations (a byte saved is a byte earned).

Key2 -> is copy of existing Key class, but uses the struct ValueRipemd160 instead of the class one to calculate the digest.

Secondary changes: I'm working on Mac using Rider, I noticed that such a setup is not readily supported by this project. I made some changes to make life easier for non windows developers. Namely added Rider related stuff to .gitignore and added build configs which do not build Windows specific projects.

I added all changes into single PR, because it felt like it will be quicker.

chore: add configurations to allow easy build/debug from IDE for Unix users.
chore: add Rider IDE related files to .gitignore.
@RokasBalevicius
Copy link
Contributor Author

@shannonklaus is there any timeline for comments and/or merge?

@shannonklaus
Copy link
Collaborator

@RokasBalevicius Yes, I am looking over this PR this week

@shannonklaus
Copy link
Collaborator

@RokasBalevicius Thank you for your pull request. It will be included in my next release, which will be mid June.

@shannonklaus shannonklaus changed the base branch from master to stage May 31, 2024 17:24
@shannonklaus shannonklaus merged commit 1bd5c35 into aerospike:stage May 31, 2024
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants