Skip to content

Commit 5f2a013

Browse files
authored
Merge branch 'master' into test-application-logs
2 parents 25c2a0c + 5fe5462 commit 5f2a013

File tree

6 files changed

+430
-51
lines changed

6 files changed

+430
-51
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
// Copyright (C) 2015-2024 The Neo Project.
2+
//
3+
// Benchmarks.UInt160.cs file belongs to the neo project and is free
4+
// software distributed under the MIT software license, see the
5+
// accompanying file LICENSE in the main directory of the
6+
// repository or http://www.opensource.org/licenses/mit-license.php
7+
// for more details.
8+
//
9+
// Redistribution and use in source and binary forms with or without
10+
// modifications are permitted.
11+
12+
using BenchmarkDotNet.Attributes;
13+
14+
namespace Neo.Benchmark;
15+
16+
public class Benchmarks_UInt160
17+
{
18+
static readonly OldUInt160 s_oldUInt160 = new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);
19+
static readonly UInt160 s_newUInt160 = new([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1]);
20+
21+
[Benchmark]
22+
public void TestOldUInt160Gernerator1()
23+
{
24+
_ = new OldUInt160();
25+
}
26+
27+
[Benchmark]
28+
public void TestOldUInt160Gernerator2()
29+
{
30+
_ = new OldUInt160(new byte[20]);
31+
}
32+
33+
[Benchmark]
34+
public void TestOldUInt160CompareTo()
35+
{
36+
OldUInt160.Zero.CompareTo(OldUInt160.Zero);
37+
OldUInt160.Zero.CompareTo(s_oldUInt160);
38+
s_oldUInt160.CompareTo(OldUInt160.Zero);
39+
}
40+
41+
[Benchmark]
42+
public void TestOldUInt160Equals()
43+
{
44+
OldUInt160.Zero.Equals(OldUInt160.Zero);
45+
OldUInt160.Zero.Equals(s_oldUInt160);
46+
s_oldUInt160.Equals(null);
47+
}
48+
49+
[Benchmark]
50+
public void TestOldUInt160Parse()
51+
{
52+
_ = OldUInt160.Parse("0x0000000000000000000000000000000000000000");
53+
_ = OldUInt160.Parse("0000000000000000000000000000000000000000");
54+
}
55+
56+
[Benchmark]
57+
public void TestOldUInt160TryParse()
58+
{
59+
OldUInt160.TryParse(null, out _);
60+
OldUInt160.TryParse("0x0000000000000000000000000000000000000000", out var temp);
61+
OldUInt160.TryParse("0x1230000000000000000000000000000000000000", out temp);
62+
OldUInt160.TryParse("000000000000000000000000000000000000000", out _);
63+
}
64+
65+
[Benchmark]
66+
public void TestOldUInt160OperatorLarger()
67+
{
68+
_ = s_oldUInt160 > OldUInt160.Zero;
69+
}
70+
71+
[Benchmark]
72+
public void TestOldUInt160OperatorLargerAndEqual()
73+
{
74+
_ = s_oldUInt160 >= OldUInt160.Zero;
75+
}
76+
77+
[Benchmark]
78+
public void TestOldUInt160OperatorSmaller()
79+
{
80+
_ = s_oldUInt160 < OldUInt160.Zero;
81+
}
82+
83+
[Benchmark]
84+
public void TestOldUInt160OperatorSmallerAndEqual()
85+
{
86+
_ = s_oldUInt160 <= OldUInt160.Zero;
87+
}
88+
89+
[Benchmark]
90+
public void TestGernerator1()
91+
{
92+
_ = new UInt160();
93+
}
94+
95+
[Benchmark]
96+
public void TestGernerator2()
97+
{
98+
_ = new UInt160(new byte[20]);
99+
}
100+
101+
[Benchmark]
102+
public void TestCompareTo()
103+
{
104+
UInt160.Zero.CompareTo(UInt160.Zero);
105+
UInt160.Zero.CompareTo(s_newUInt160);
106+
s_newUInt160.CompareTo(UInt160.Zero);
107+
}
108+
109+
[Benchmark]
110+
public void TestEquals()
111+
{
112+
UInt160.Zero.Equals(UInt160.Zero);
113+
UInt160.Zero.Equals(s_newUInt160);
114+
s_newUInt160.Equals(null);
115+
}
116+
117+
[Benchmark]
118+
public void TestParse()
119+
{
120+
_ = UInt160.Parse("0x0000000000000000000000000000000000000000");
121+
_ = UInt160.Parse("0000000000000000000000000000000000000000");
122+
}
123+
124+
[Benchmark]
125+
public void TestTryParse()
126+
{
127+
UInt160.TryParse(null, out _);
128+
UInt160.TryParse("0x0000000000000000000000000000000000000000", out var temp);
129+
UInt160.TryParse("0x1230000000000000000000000000000000000000", out temp);
130+
UInt160.TryParse("000000000000000000000000000000000000000", out _);
131+
}
132+
133+
[Benchmark]
134+
public void TestOperatorLarger()
135+
{
136+
_ = s_newUInt160 > UInt160.Zero;
137+
}
138+
139+
[Benchmark]
140+
public void TestOperatorLargerAndEqual()
141+
{
142+
_ = s_newUInt160 >= UInt160.Zero;
143+
}
144+
145+
[Benchmark]
146+
public void TestOperatorSmaller()
147+
{
148+
_ = s_newUInt160 < UInt160.Zero;
149+
}
150+
151+
[Benchmark]
152+
public void TestOperatorSmallerAndEqual()
153+
{
154+
_ = s_newUInt160 <= UInt160.Zero;
155+
}
156+
}

benchmarks/Neo.Benchmarks/Neo.Benchmarks.csproj

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
<TargetFrameworks>net8.0</TargetFrameworks>
66
<RootNamespace>Neo</RootNamespace>
77
<ImplicitUsings>enable</ImplicitUsings>
8+
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
89
</PropertyGroup>
910

1011
<ItemGroup>
+184
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
// Copyright (C) 2015-2024 The Neo Project.
2+
//
3+
// OldUInt160.cs file belongs to the neo project and is free
4+
// software distributed under the MIT software license, see the
5+
// accompanying file LICENSE in the main directory of the
6+
// repository or http://www.opensource.org/licenses/mit-license.php
7+
// for more details.
8+
//
9+
// Redistribution and use in source and binary forms with or without
10+
// modifications are permitted.
11+
12+
using Neo.Extensions;
13+
using Neo.IO;
14+
using System.Globalization;
15+
using System.Runtime.InteropServices;
16+
17+
namespace Neo
18+
{
19+
/// <summary>
20+
/// Represents a 160-bit unsigned integer.
21+
/// </summary>
22+
[StructLayout(LayoutKind.Explicit, Size = 20)]
23+
public class OldUInt160 : IComparable<OldUInt160>, IEquatable<OldUInt160>, ISerializable
24+
{
25+
/// <summary>
26+
/// The length of <see cref="OldUInt160"/> values.
27+
/// </summary>
28+
public const int Length = 20;
29+
30+
/// <summary>
31+
/// Represents 0.
32+
/// </summary>
33+
public static readonly OldUInt160 Zero = new();
34+
35+
[FieldOffset(0)] private ulong value1;
36+
[FieldOffset(8)] private ulong value2;
37+
[FieldOffset(16)] private uint value3;
38+
39+
public int Size => Length;
40+
41+
/// <summary>
42+
/// Initializes a new instance of the <see cref="OldUInt160"/> class.
43+
/// </summary>
44+
public OldUInt160()
45+
{
46+
}
47+
48+
/// <summary>
49+
/// Initializes a new instance of the <see cref="OldUInt160"/> class.
50+
/// </summary>
51+
/// <param name="value">The value of the <see cref="OldUInt160"/>.</param>
52+
public unsafe OldUInt160(ReadOnlySpan<byte> value)
53+
{
54+
if (value.Length != Length) throw new FormatException();
55+
fixed (ulong* p = &value1)
56+
{
57+
Span<byte> dst = new(p, Length);
58+
value[..Length].CopyTo(dst);
59+
}
60+
}
61+
62+
public int CompareTo(OldUInt160 other)
63+
{
64+
int result = value3.CompareTo(other.value3);
65+
if (result != 0) return result;
66+
result = value2.CompareTo(other.value2);
67+
if (result != 0) return result;
68+
return value1.CompareTo(other.value1);
69+
}
70+
71+
public void Deserialize(ref MemoryReader reader)
72+
{
73+
value1 = reader.ReadUInt64();
74+
value2 = reader.ReadUInt64();
75+
value3 = reader.ReadUInt32();
76+
}
77+
78+
public override bool Equals(object obj)
79+
{
80+
if (ReferenceEquals(obj, this)) return true;
81+
return Equals(obj as OldUInt160);
82+
}
83+
84+
public bool Equals(OldUInt160 other)
85+
{
86+
if (other is null) return false;
87+
return value1 == other.value1
88+
&& value2 == other.value2
89+
&& value3 == other.value3;
90+
}
91+
92+
public override int GetHashCode()
93+
{
94+
return (int)value1;
95+
}
96+
97+
/// <summary>
98+
/// Parses an <see cref="OldUInt160"/> from the specified <see cref="string"/>.
99+
/// </summary>
100+
/// <param name="value">An <see cref="OldUInt160"/> represented by a <see cref="string"/>.</param>
101+
/// <returns>The parsed <see cref="OldUInt160"/>.</returns>
102+
/// <exception cref="FormatException"><paramref name="value"/> is not in the correct format.</exception>
103+
public static OldUInt160 Parse(string value)
104+
{
105+
if (!TryParse(value, out var result)) throw new FormatException();
106+
return result;
107+
}
108+
109+
public void Serialize(BinaryWriter writer)
110+
{
111+
writer.Write(value1);
112+
writer.Write(value2);
113+
writer.Write(value3);
114+
}
115+
116+
public override string ToString()
117+
{
118+
return "0x" + this.ToArray().ToHexString(reverse: true);
119+
}
120+
121+
/// <summary>
122+
/// Parses an <see cref="OldUInt160"/> from the specified <see cref="string"/>.
123+
/// </summary>
124+
/// <param name="s">An <see cref="OldUInt160"/> represented by a <see cref="string"/>.</param>
125+
/// <param name="result">The parsed <see cref="OldUInt160"/>.</param>
126+
/// <returns><see langword="true"/> if an <see cref="OldUInt160"/> is successfully parsed; otherwise, <see langword="false"/>.</returns>
127+
public static bool TryParse(string s, out OldUInt160 result)
128+
{
129+
if (s == null)
130+
{
131+
result = null;
132+
return false;
133+
}
134+
if (s.StartsWith("0x", StringComparison.InvariantCultureIgnoreCase))
135+
s = s[2..];
136+
if (s.Length != Length * 2)
137+
{
138+
result = null;
139+
return false;
140+
}
141+
byte[] data = new byte[Length];
142+
for (int i = 0; i < Length; i++)
143+
if (!byte.TryParse(s.Substring(i * 2, 2), NumberStyles.AllowHexSpecifier, null, out data[Length - i - 1]))
144+
{
145+
result = null;
146+
return false;
147+
}
148+
result = new OldUInt160(data);
149+
return true;
150+
}
151+
152+
public static bool operator ==(OldUInt160 left, OldUInt160 right)
153+
{
154+
if (ReferenceEquals(left, right)) return true;
155+
if (left is null || right is null) return false;
156+
return left.Equals(right);
157+
}
158+
159+
public static bool operator !=(OldUInt160 left, OldUInt160 right)
160+
{
161+
return !(left == right);
162+
}
163+
164+
public static bool operator >(OldUInt160 left, OldUInt160 right)
165+
{
166+
return left.CompareTo(right) > 0;
167+
}
168+
169+
public static bool operator >=(OldUInt160 left, OldUInt160 right)
170+
{
171+
return left.CompareTo(right) >= 0;
172+
}
173+
174+
public static bool operator <(OldUInt160 left, OldUInt160 right)
175+
{
176+
return left.CompareTo(right) < 0;
177+
}
178+
179+
public static bool operator <=(OldUInt160 left, OldUInt160 right)
180+
{
181+
return left.CompareTo(right) <= 0;
182+
}
183+
}
184+
}

benchmarks/Neo.Benchmarks/Program.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@
1212
using BenchmarkDotNet.Running;
1313
using Neo.Benchmark;
1414

15-
BenchmarkRunner.Run<Benchmarks_PoCs>();
15+
// BenchmarkRunner.Run<Benchmarks_PoCs>();
16+
BenchmarkRunner.Run<Benchmarks_UInt160>();

0 commit comments

Comments
 (0)