Skip to content

Commit

Permalink
Merge branch 'HF_Echidna' into fix.overflow-in-substr
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon authored Nov 7, 2024
2 parents 20ec1e1 + 7860aa8 commit f9fc6fc
Show file tree
Hide file tree
Showing 182 changed files with 3,911 additions and 711 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -260,4 +260,8 @@ launchSettings.json

# UT coverage report
/coverages
**/.DS_Store
**/.DS_Store

# Benchmarks
**/BenchmarkDotNet.Artifacts/

56 changes: 56 additions & 0 deletions benchmarks/Neo.Benchmarks/Benchmarks.Hash.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// Benchmarks.Hash.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using BenchmarkDotNet.Attributes;
using Neo.Cryptography;
using Neo.Extensions;
using System.Diagnostics;
using System.IO.Hashing;
using System.Text;

namespace Neo.Benchmark;

public class Benchmarks_Hash
{
// 256 KiB
static readonly byte[] data = Encoding.ASCII.GetBytes(string.Concat(Enumerable.Repeat("Hello, World!^_^", 16 * 1024)));

static readonly byte[] hash = "9182abedfbb9b18d81a05d8bcb45489e7daa2858".HexToBytes();

[Benchmark]
public void RIPEMD160_ComputeHash()
{
using var ripemd160 = new RIPEMD160Managed();
var result = ripemd160.ComputeHash(data);
Debug.Assert(result.SequenceEqual(hash));
}

[Benchmark]
public void XxHash32_HashToUInt32()
{
var result = XxHash32.HashToUInt32(data);
Debug.Assert(result == 682967318u);
}

[Benchmark]
public void XxHash3_HashToUInt64()
{
var result = (uint)XxHash3.HashToUInt64(data);
Debug.Assert(result == 1389469485u);
}

[Benchmark]
public void Murmur32_HashToUInt32()
{
var result = data.Murmur32(0);
Debug.Assert(result == 3731881930u);
}
}
1 change: 1 addition & 0 deletions benchmarks/Neo.Benchmarks/Neo.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.IO.Hashing" Version="8.0.0" />
<ProjectReference Include="..\..\src\Neo\Neo.csproj" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
</ItemGroup>
Expand Down
1 change: 1 addition & 0 deletions benchmarks/Neo.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@

// BenchmarkRunner.Run<Benchmarks_PoCs>();
BenchmarkRunner.Run<Benchmarks_UInt160>();
BenchmarkRunner.Run<Benchmarks_Hash>();
165 changes: 165 additions & 0 deletions benchmarks/Neo.Extensions.Benchmarks/Benchmark.ByteArrayComparer.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// Benchmark.ByteArrayComparer.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using BenchmarkDotNet.Attributes;

namespace Neo.Extensions
{
public class Benchmark_ByteArrayComparer
{
private ByteArrayComparer comparer = ByteArrayComparer.Default;
private ByteArrayComparerV0 _oldComparer = ByteArrayComparerV0.Default;
private byte[]? x, y;

[GlobalSetup]
public void Setup()
{
comparer = ByteArrayComparer.Default;
_oldComparer = ByteArrayComparerV0.Default;
}

[GlobalSetup(Target = nameof(NewCompare_50Bytes))]
public void SetupNew50Bytes()
{
comparer = ByteArrayComparer.Default;

x = new byte[50]; // 50 bytes
y = new byte[50]; // 50 bytes
Array.Fill(x, (byte)0xCC);
Array.Copy(x, y, x.Length);
}

[Benchmark]
public void NewCompare_50Bytes()
{
comparer.Compare(x, y);
}

[GlobalSetup(Target = nameof(OldCompare_50Bytes))]
public void SetupOld50Bytes()
{
_oldComparer = ByteArrayComparerV0.Default;

x = new byte[50]; // 50 bytes
y = new byte[50]; // 50 bytes
Array.Fill(x, (byte)0xCC);
Array.Copy(x, y, x.Length);
}

[Benchmark]
public void OldCompare_50Bytes()
{
_oldComparer.Compare(x, y);
}

[GlobalSetup(Target = nameof(NewCompare_500Bytes))]
public void SetupNew500Bytes()
{
comparer = ByteArrayComparer.Default;

x = new byte[500]; // 500 bytes
y = new byte[500]; // 500 bytes
Array.Fill(x, (byte)0xCC);
Array.Copy(x, y, x.Length);
}

[Benchmark]
public void NewCompare_500Bytes()
{
comparer.Compare(x, y);
}

[GlobalSetup(Target = nameof(OldCompare_500Bytes))]
public void SetupOld500Bytes()
{
_oldComparer = ByteArrayComparerV0.Default;

x = new byte[500]; // 500 bytes
y = new byte[500]; // 500 bytes
Array.Fill(x, (byte)0xCC);
Array.Copy(x, y, x.Length);
}

[Benchmark]
public void OldCompare_500Bytes()
{
_oldComparer.Compare(x, y);
}

[GlobalSetup(Target = nameof(NewCompare_5000Bytes))]
public void SetupNew5000Bytes()
{
comparer = ByteArrayComparer.Default;

x = new byte[5000]; // 5000 bytes
y = new byte[5000]; // 5000 bytes
Array.Fill(x, (byte)0xCC);
Array.Copy(x, y, x.Length);
}

[Benchmark]
public void NewCompare_5000Bytes()
{
comparer.Compare(x, y);
}

[GlobalSetup(Target = nameof(OldCompare_5000Bytes))]
public void SetupOld5000Bytes()
{
_oldComparer = ByteArrayComparerV0.Default;

x = new byte[5000]; // 5000 bytes
y = new byte[5000]; // 5000 bytes
Array.Fill(x, (byte)0xCC);
Array.Copy(x, y, x.Length);
}

[Benchmark]
public void OldCompare_5000Bytes()
{
_oldComparer.Compare(x, y);
}

[GlobalSetup(Target = nameof(NewCompare_50000Bytes))]
public void SetupNew50000Bytes()
{
comparer = ByteArrayComparer.Default;

x = new byte[50000]; // 50000 bytes
y = new byte[50000]; // 50000 bytes
Array.Fill(x, (byte)0xCC);
Array.Copy(x, y, x.Length);
}

[Benchmark]
public void NewCompare_50000Bytes()
{
comparer.Compare(x, y);
}

[GlobalSetup(Target = nameof(OldCompare_50000Bytes))]
public void SetupOld50000Bytes()
{
_oldComparer = ByteArrayComparerV0.Default;

x = new byte[50000]; // 50000 bytes
y = new byte[50000]; // 50000 bytes
Array.Fill(x, (byte)0xCC);
Array.Copy(x, y, x.Length);
}

[Benchmark]
public void OldCompare_50000Bytes()
{
_oldComparer.Compare(x, y);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<RootNamespace>Neo.Extensions</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="MSTest.TestFramework" Version="3.5.0" />
<ProjectReference Include="..\..\src\Neo.Extensions\Neo.Extensions.csproj" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// ByteArrayComparer.cs file belongs to the neo project and is free
// OldByteArrayComparer.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
Expand All @@ -9,20 +9,18 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;

namespace Neo.IO
namespace Neo.Extensions
{
internal class ByteArrayComparer : IComparer<byte[]>
public class ByteArrayComparerV0 : IComparer<byte[]>
{
public static readonly ByteArrayComparer Default = new(1);
public static readonly ByteArrayComparer Reverse = new(-1);
public static readonly ByteArrayComparerV0 Default = new(1);
public static readonly ByteArrayComparerV0 Reverse = new(-1);

private readonly int _direction;

private ByteArrayComparer(int direction)
internal ByteArrayComparerV0(int direction)
{
_direction = direction;
}
Expand All @@ -35,8 +33,8 @@ public int Compare(byte[]? x, byte[]? y)
if (y is null && x is not null)
return _direction > 0 ? x.Length : -x.Length;
return _direction > 0 ?
CompareInternal(x!, y!) :
-CompareInternal(x!, y!);
CompareInternal(x!, y!) :
-CompareInternal(x!, y!);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
15 changes: 15 additions & 0 deletions benchmarks/Neo.Extensions.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// Program.cs file belongs to the neo project and is free
// software distributed under the MIT software license, see the
// accompanying file LICENSE in the main directory of the
// repository or http://www.opensource.org/licenses/mit-license.php
// for more details.
//
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using BenchmarkDotNet.Running;
using Neo.Extensions;

BenchmarkRunner.Run(typeof(Benchmark_ByteArrayComparer));
Loading

0 comments on commit f9fc6fc

Please sign in to comment.