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
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,6 @@ TestResults/
coverage/
.config
tools/

# Rider related files
.idea/
22 changes: 22 additions & 0 deletions Aerospike.sln
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ Global
Coverage|Any CPU = Coverage|Any CPU
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
Debug_Unix|Any CPU = Debug_Unix|Any CPU
Release_Unix|Any CPU = Release_Unix|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{86B2791C-FDDD-40B6-B675-B372DC4B8C87}.Coverage|Any CPU.ActiveCfg = Release|Any CPU
Expand All @@ -31,36 +33,56 @@ Global
{86B2791C-FDDD-40B6-B675-B372DC4B8C87}.Debug|Any CPU.Build.0 = Debug|Any CPU
{86B2791C-FDDD-40B6-B675-B372DC4B8C87}.Release|Any CPU.ActiveCfg = Release|Any CPU
{86B2791C-FDDD-40B6-B675-B372DC4B8C87}.Release|Any CPU.Build.0 = Release|Any CPU
{86B2791C-FDDD-40B6-B675-B372DC4B8C87}.Debug_Unix|Any CPU.ActiveCfg = Debug_Unix|Any CPU
{86B2791C-FDDD-40B6-B675-B372DC4B8C87}.Release_Unix|Any CPU.ActiveCfg = Release_Unix|Any CPU
{3382B556-835E-4F0D-84B7-181639BA4414}.Coverage|Any CPU.ActiveCfg = Release|Any CPU
{3382B556-835E-4F0D-84B7-181639BA4414}.Coverage|Any CPU.Build.0 = Release|Any CPU
{3382B556-835E-4F0D-84B7-181639BA4414}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3382B556-835E-4F0D-84B7-181639BA4414}.Debug|Any CPU.Build.0 = Debug|Any CPU
{3382B556-835E-4F0D-84B7-181639BA4414}.Release|Any CPU.ActiveCfg = Release|Any CPU
{3382B556-835E-4F0D-84B7-181639BA4414}.Release|Any CPU.Build.0 = Release|Any CPU
{3382B556-835E-4F0D-84B7-181639BA4414}.Debug_Unix|Any CPU.ActiveCfg = Debug_Unix|Any CPU
{3382B556-835E-4F0D-84B7-181639BA4414}.Release_Unix|Any CPU.ActiveCfg = Release_Unix|Any CPU
{C76109A1-1A6C-4F78-848B-DB049D5509E3}.Coverage|Any CPU.ActiveCfg = Coverage|Any CPU
{C76109A1-1A6C-4F78-848B-DB049D5509E3}.Coverage|Any CPU.Build.0 = Coverage|Any CPU
{C76109A1-1A6C-4F78-848B-DB049D5509E3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{C76109A1-1A6C-4F78-848B-DB049D5509E3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C76109A1-1A6C-4F78-848B-DB049D5509E3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C76109A1-1A6C-4F78-848B-DB049D5509E3}.Release|Any CPU.Build.0 = Release|Any CPU
{C76109A1-1A6C-4F78-848B-DB049D5509E3}.Debug_Unix|Any CPU.ActiveCfg = Debug_Unix|Any CPU
{C76109A1-1A6C-4F78-848B-DB049D5509E3}.Debug_Unix|Any CPU.Build.0 = Debug_Unix|Any CPU
{C76109A1-1A6C-4F78-848B-DB049D5509E3}.Release_Unix|Any CPU.ActiveCfg = Release_Unix|Any CPU
{C76109A1-1A6C-4F78-848B-DB049D5509E3}.Release_Unix|Any CPU.Build.0 = Release_Unix|Any CPU
{A1AA3444-EAED-4394-BCCA-96EA0E0A88D1}.Coverage|Any CPU.ActiveCfg = Release|Any CPU
{A1AA3444-EAED-4394-BCCA-96EA0E0A88D1}.Coverage|Any CPU.Build.0 = Release|Any CPU
{A1AA3444-EAED-4394-BCCA-96EA0E0A88D1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A1AA3444-EAED-4394-BCCA-96EA0E0A88D1}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A1AA3444-EAED-4394-BCCA-96EA0E0A88D1}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A1AA3444-EAED-4394-BCCA-96EA0E0A88D1}.Release|Any CPU.Build.0 = Release|Any CPU
{A1AA3444-EAED-4394-BCCA-96EA0E0A88D1}.Debug_Unix|Any CPU.ActiveCfg = Debug_Unix|Any CPU
{A1AA3444-EAED-4394-BCCA-96EA0E0A88D1}.Debug_Unix|Any CPU.Build.0 = Debug_Unix|Any CPU
{A1AA3444-EAED-4394-BCCA-96EA0E0A88D1}.Release_Unix|Any CPU.ActiveCfg = Release_Unix|Any CPU
{A1AA3444-EAED-4394-BCCA-96EA0E0A88D1}.Release_Unix|Any CPU.Build.0 = Release_Unix|Any CPU
{09C0B19A-41DD-4B47-A015-B7DFCA0072D3}.Coverage|Any CPU.ActiveCfg = Release|Any CPU
{09C0B19A-41DD-4B47-A015-B7DFCA0072D3}.Coverage|Any CPU.Build.0 = Release|Any CPU
{09C0B19A-41DD-4B47-A015-B7DFCA0072D3}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{09C0B19A-41DD-4B47-A015-B7DFCA0072D3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{09C0B19A-41DD-4B47-A015-B7DFCA0072D3}.Release|Any CPU.ActiveCfg = Release|Any CPU
{09C0B19A-41DD-4B47-A015-B7DFCA0072D3}.Release|Any CPU.Build.0 = Release|Any CPU
{09C0B19A-41DD-4B47-A015-B7DFCA0072D3}.Debug_Unix|Any CPU.ActiveCfg = Debug_Unix|Any CPU
{09C0B19A-41DD-4B47-A015-B7DFCA0072D3}.Debug_Unix|Any CPU.Build.0 = Debug_Unix|Any CPU
{09C0B19A-41DD-4B47-A015-B7DFCA0072D3}.Release_Unix|Any CPU.ActiveCfg = Release_Unix|Any CPU
{09C0B19A-41DD-4B47-A015-B7DFCA0072D3}.Release_Unix|Any CPU.Build.0 = Release_Unix|Any CPU
{DF7217EF-2088-466E-AD21-25CD7973868B}.Coverage|Any CPU.ActiveCfg = Release|Any CPU
{DF7217EF-2088-466E-AD21-25CD7973868B}.Coverage|Any CPU.Build.0 = Release|Any CPU
{DF7217EF-2088-466E-AD21-25CD7973868B}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{DF7217EF-2088-466E-AD21-25CD7973868B}.Debug|Any CPU.Build.0 = Debug|Any CPU
{DF7217EF-2088-466E-AD21-25CD7973868B}.Release|Any CPU.ActiveCfg = Release|Any CPU
{DF7217EF-2088-466E-AD21-25CD7973868B}.Release|Any CPU.Build.0 = Release|Any CPU
{DF7217EF-2088-466E-AD21-25CD7973868B}.Debug_Unix|Any CPU.ActiveCfg = Debug_Unix|Any CPU
{DF7217EF-2088-466E-AD21-25CD7973868B}.Debug_Unix|Any CPU.Build.0 = Debug_Unix|Any CPU
{DF7217EF-2088-466E-AD21-25CD7973868B}.Release_Unix|Any CPU.ActiveCfg = Release_Unix|Any CPU
{DF7217EF-2088-466E-AD21-25CD7973868B}.Release_Unix|Any CPU.Build.0 = Release_Unix|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
9 changes: 9 additions & 0 deletions AerospikeAdmin/AerospikeAdmin.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<UseWindowsForms>true</UseWindowsForms>
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
<Version>7.2.0</Version>
<Configurations>Debug;Release;Release_Unix;Debug_Unix</Configurations>
<Platforms>AnyCPU</Platforms>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<OutputPath>bin\x64\Debug\</OutputPath>
Expand Down Expand Up @@ -60,6 +62,13 @@
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\Debug IIS\</OutputPath>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release_Unix' ">
<Optimize Condition=" '$(Optimize)' == '' ">true</Optimize>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug_Unix' ">
<DebugSymbols Condition=" '$(DebugSymbols)' == '' ">true</DebugSymbols>
<Optimize Condition=" '$(Optimize)' == '' ">false</Optimize>
</PropertyGroup>
<ItemGroup>
<Compile Update="RolesControl.cs">
<SubType>UserControl</SubType>
Expand Down
11 changes: 11 additions & 0 deletions AerospikeBenchmarks/AerospikeBenchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,17 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>disable</Nullable>
<Version>7.2.0</Version>
<Configurations>Debug;Release;Debug_Unix;Release_Unix</Configurations>
<Platforms>AnyCPU</Platforms>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug_Unix' ">
<DebugSymbols Condition=" '$(DebugSymbols)' == '' ">true</DebugSymbols>
<Optimize Condition=" '$(Optimize)' == '' ">false</Optimize>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release_Unix' ">
<Optimize Condition=" '$(Optimize)' == '' ">true</Optimize>
</PropertyGroup>

<ItemGroup>
Expand Down
13 changes: 13 additions & 0 deletions AerospikeClient/AerospikeClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
<OutputType>Library</OutputType>
<StartupObject />
<RootNamespace>Aerospike.Client</RootNamespace>
<Configurations>Debug;Release;Debug_Unix;Release_Unix</Configurations>
<Platforms>AnyCPU</Platforms>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand All @@ -35,6 +37,17 @@
<NoWarn>1701;1702;CS1591</NoWarn>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug_Unix' ">
<NoWarn>1701;1702;CS1591</NoWarn>
<DebugSymbols Condition=" '$(DebugSymbols)' == '' ">true</DebugSymbols>
<Optimize Condition=" '$(Optimize)' == '' ">false</Optimize>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release_Unix' ">
<NoWarn>1701;1702;CS1591</NoWarn>
<Optimize Condition=" '$(Optimize)' == '' ">true</Optimize>
</PropertyGroup>

<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
Expand Down
8 changes: 4 additions & 4 deletions AerospikeClient/Util/Hash.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright 2012-2018 Aerospike, Inc.
*
* Portions may be licensed to Aerospike, Inc. under one or more contributor
Expand Down Expand Up @@ -42,12 +42,12 @@ public static byte[] ComputeHash(byte[] buffer, int length)
public static byte[] ComputeHash(byte[] buffer, int length)
{
// Benchmarks show creating instance every time is faster than thread local
// implementation because instance implementation puts Ripemd160 arrays on
// implementation because instance implementation puts ValueRipemd160 on
// stack (fast) and eliminates the overhead of retrieving thread local instance.
Ripemd160 ripe = new Ripemd160();
ValueRipemd160 ripe = new ValueRipemd160();
ripe.Add(buffer, 0, length);
return ripe.HashDigest();
}
#endif
}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/*
/*
* Copyright 2012-2018 Aerospike, Inc.
*
* Portions may be licensed to Aerospike, Inc. under one or more contributor
Expand All @@ -14,7 +14,7 @@
* License for the specific language governing permissions and limitations under
* the License.
*/
/*
/*
* Copyright (c) 2000 - 2015 The Legion of the Bouncy Castle Inc. (http://www.bouncycastle.org)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Expand All @@ -25,7 +25,7 @@
*
* The above copyright notice and this permission notice shall be included in all copies or
* substantial portions of the Software.
*
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Expand All @@ -39,23 +39,27 @@ namespace Aerospike.Client
/// <summary>
/// RIPEMD-160 hash. Algorithm provided by Hans Dobbertin, Antoon Bosselaers, and Bart Preneel:
/// https://homes.esat.kuleuven.be/~bosselae/ripemd160.html
///
///
/// This is an optimized implementation based on Bouncy Castle's RipeMD160Digest.cs:
/// http://www.bouncycastle.org/csharp
/// </summary>
public sealed class Ripemd160
public struct ValueRipemd160
{
private uint[] X = new uint[16];
private byte[] xBuf = new byte[4];
private int xOff;
private int xBufOff;
private long byteCount;
private int xOff = 0;
private int xBufOff = 0;
private long byteCount = 0;
private uint H0 = 0x67452301;
private uint H1 = 0xefcdab89;
private uint H2 = 0x98badcfe;
private uint H3 = 0x10325476;
private uint H4 = 0xc3d2e1f0;

public ValueRipemd160()
{
}

/// <summary>
/// Reset parameters to start new hash.
/// Only call if plan to hash multiple times with same Ripemd160 instance.
Expand Down Expand Up @@ -115,7 +119,7 @@ public void Add(byte[] input, int inOff, int length)
/// <summary>
/// Add byte to be hashed.
/// </summary>
public void Add(byte input)
private void Add(byte input)
{
xBuf[xBufOff++] = input;

Expand Down Expand Up @@ -173,8 +177,8 @@ public byte[] HashDigest()
private void ProcessWord(byte[] input, int inOff)
{
X[xOff++] = ((uint)input[inOff]) |
(((uint)input[inOff + 1]) << 8) |
(((uint)input[inOff + 2]) << 16) |
(((uint)input[inOff + 1]) << 8) |
(((uint)input[inOff + 2]) << 16) |
(((uint)input[inOff + 3]) << 24);

if (xOff == 16)
Expand All @@ -193,7 +197,7 @@ private void ProcessLength(long bitLength)
X[14] = (uint)(bitLength & 0xffffffff);
X[15] = (uint)((ulong)bitLength >> 32);
}

private void ProcessBlock()
{
uint aa = H0, aaa = H0;
Expand Down Expand Up @@ -1028,4 +1032,4 @@ private void ProcessBlock()
Array.Clear(X, 0, X.Length);
}
}
}
}
13 changes: 13 additions & 0 deletions AerospikeClientProxy/AerospikeClientProxy.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
<OutputType>Library</OutputType>
<StartupObject />
<RootNamespace>Aerospike.Client</RootNamespace>
<Configurations>Debug;Release;Debug_Unix;Release_Unix</Configurations>
<Platforms>AnyCPU</Platforms>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand All @@ -35,6 +37,17 @@
<NoWarn>1701;1702;CS1591</NoWarn>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug_Unix' ">
<NoWarn>1701;1702;CS1591</NoWarn>
<DebugSymbols Condition=" '$(DebugSymbols)' == '' ">true</DebugSymbols>
<Optimize Condition=" '$(Optimize)' == '' ">false</Optimize>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release_Unix' ">
<NoWarn>1701;1702;CS1591</NoWarn>
<Optimize Condition=" '$(Optimize)' == '' ">true</Optimize>
</PropertyGroup>

<PropertyGroup>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<DocumentationFile>bin\$(Configuration)\$(TargetFramework)\$(AssemblyName).xml</DocumentationFile>
Expand Down
11 changes: 11 additions & 0 deletions AerospikeDemo/AerospikeDemo.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
<UseWindowsForms>true</UseWindowsForms>
<ImportWindowsDesktopTargets>true</ImportWindowsDesktopTargets>
<Version>7.2.0</Version>
<Configurations>Debug;Release;Debug_Unix;Release_Unix</Configurations>
<Platforms>AnyCPU</Platforms>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<OutputPath>bin\x64\Debug\</OutputPath>
Expand Down Expand Up @@ -66,6 +68,15 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<NoWarn>1701;1702;CA1416</NoWarn>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Debug_Unix' ">
<NoWarn>1701;1702;CA1416</NoWarn>
<DebugSymbols Condition=" '$(DebugSymbols)' == '' ">true</DebugSymbols>
<Optimize Condition=" '$(Optimize)' == '' ">false</Optimize>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)' == 'Release_Unix' ">
<NoWarn>1701;1702;CA1416</NoWarn>
<Optimize Condition=" '$(Optimize)' == '' ">true</Optimize>
</PropertyGroup>
<ItemGroup>
<BootstrapperPackage Include=".NETFramework,Version=v4.0,Profile=Client">
<Visible>False</Visible>
Expand Down
15 changes: 14 additions & 1 deletion AerospikeTest/AerospikeTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@

<Authors>Aerospike</Authors>

<Configurations>Debug;Release;Coverage</Configurations>
<Configurations>Debug;Release;Coverage;Debug_Unix;Release_Unix</Configurations>

<Platforms>AnyCPU</Platforms>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
Expand All @@ -37,6 +39,17 @@
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|AnyCPU'">
<DefineConstants>TRACE;NETCORE</DefineConstants>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Debug_Unix' ">
<DefineConstants>TRACE;DEBUG;NETCORE</DefineConstants>
<DebugSymbols Condition=" '$(DebugSymbols)' == '' ">true</DebugSymbols>
<Optimize Condition=" '$(Optimize)' == '' ">false</Optimize>
</PropertyGroup>

<PropertyGroup Condition=" '$(Configuration)' == 'Release_Unix' ">
<DefineConstants>TRACE;NETCORE</DefineConstants>
<Optimize Condition=" '$(Optimize)' == '' ">true</Optimize>
</PropertyGroup>
<PropertyGroup>
<StartupObject />
</PropertyGroup>
Expand Down
Loading