Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
52 changes: 52 additions & 0 deletions Neo.Cryptography.MPTTrie.Benchmarks/Benchmarks.Cache.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// Copyright (C) 2015-2025 The Neo Project.
//
// Benchmarks.Cache.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 System;
using System.Security.Policy;

namespace Neo.Cryptography.MPTTrie.Benchmarks
{
public class Benchmarks_Cache
{
private readonly byte _prefix = 0x01;
private readonly UInt256 _hash;

public Benchmarks_Cache()
{
var randomBytes = new byte[UInt256.Length];
new Random(42).NextBytes(randomBytes);
_hash = new UInt256(randomBytes);
}

[Benchmark]
public byte[] Key_Original()
{
var buffer = new byte[UInt256.Length + 1];
using (var ms = new MemoryStream(buffer, true))
using (var writer = new BinaryWriter(ms))
{
writer.Write(_prefix);
_hash.Serialize(writer);
}
return buffer;
}

[Benchmark]
public byte[] Key_Optimized()
{
var buffer = new byte[UInt256.Length + 1];
buffer[0] = _prefix;
_hash.GetSpan().CopyTo(buffer.AsSpan(1));
return buffer;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net9.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\src\Neo\Neo.csproj" />
</ItemGroup>

</Project>
23 changes: 23 additions & 0 deletions Neo.Cryptography.MPTTrie.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (C) 2015-2025 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;

// List all benchmarks:
// dotnet run -c Release --framework [for example: net9.0] -- --list flat(or tree)
// Run a specific benchmark:
// dotnet run -c Release --framework [for example: net9.0] -- -f [benchmark name]
// Run all benchmarks:
// dotnet run -c Release --framework [for example: net9.0] -- -f *
// Run all benchmarks of a class:
// dotnet run -c Release --framework [for example: net9.0] -- -f '*Class*'
// More options: https://benchmarkdotnet.org/articles/guides/console-args.html
BenchmarkSwitcher.FromAssembly(typeof(Program).Assembly).Run(args);
7 changes: 7 additions & 0 deletions neo.sln
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SignClient", "src\Plugins\S
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.Plugins.SignClient.Tests", "tests\Neo.Plugins.SignClient.Tests\Neo.Plugins.SignClient.Tests.csproj", "{E2CFEAA1-45F2-4075-94ED-866862C6863F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Neo.Cryptography.MPTTrie.Benchmarks", "Neo.Cryptography.MPTTrie.Benchmarks\Neo.Cryptography.MPTTrie.Benchmarks.csproj", "{A53FE0B5-22BA-4C52-8FD4-80877DB62992}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -259,6 +261,10 @@ Global
{E2CFEAA1-45F2-4075-94ED-866862C6863F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{E2CFEAA1-45F2-4075-94ED-866862C6863F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{E2CFEAA1-45F2-4075-94ED-866862C6863F}.Release|Any CPU.Build.0 = Release|Any CPU
{A53FE0B5-22BA-4C52-8FD4-80877DB62992}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{A53FE0B5-22BA-4C52-8FD4-80877DB62992}.Debug|Any CPU.Build.0 = Debug|Any CPU
{A53FE0B5-22BA-4C52-8FD4-80877DB62992}.Release|Any CPU.ActiveCfg = Release|Any CPU
{A53FE0B5-22BA-4C52-8FD4-80877DB62992}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -305,6 +311,7 @@ Global
{19B1CF1A-17F4-4E04-AB9C-55CE74952E11} = {EDE05FA8-8E73-4924-BC63-DD117127EEE1}
{CAD55942-48A3-4526-979D-7519FADF19FE} = {C2DC830A-327A-42A7-807D-295216D30DBB}
{E2CFEAA1-45F2-4075-94ED-866862C6863F} = {7F257712-D033-47FF-B439-9D4320D06599}
{A53FE0B5-22BA-4C52-8FD4-80877DB62992} = {C25EB0B0-0CAC-4CC1-8F36-F9229EFB99EC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {BCBA19D9-F868-4C6D-8061-A2B91E06E3EC}
Expand Down
10 changes: 4 additions & 6 deletions src/Neo.Cryptography.MPTTrie/Cache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,10 @@

using Neo.Extensions;
using Neo.Persistence;
using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Policy;

namespace Neo.Cryptography.MPTTrie
{
Expand Down Expand Up @@ -45,12 +47,8 @@ public Cache(IStoreSnapshot store, byte prefix)
private byte[] Key(UInt256 hash)
{
var buffer = new byte[UInt256.Length + 1];
using (var ms = new MemoryStream(buffer, true))
using (var writer = new BinaryWriter(ms))
{
writer.Write(_prefix);
hash.Serialize(writer);
}
buffer[0] = _prefix;
hash.GetSpan().CopyTo(buffer.AsSpan(1));
return buffer;
}

Expand Down