-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Optimizations for
KeyBuilder
(#3598)
* Optimize KeyBuilder and Add some UTs for not-covered methods * optimization for adding UInt160 and UInt256 to KeyBuilder * fix stream capacity calculation * Update src/Neo/SmartContract/KeyBuilder.cs Co-authored-by: Shargon <shargon@gmail.com> * Update src/Neo/SmartContract/KeyBuilder.cs Co-authored-by: Shargon <shargon@gmail.com> * add more benchmark * check IsLittleEndian or not * add UTs for UInt160 and UInt256 * fix code format --------- Co-authored-by: Shargon <shargon@gmail.com> Co-authored-by: Jimmy <jinghui@wayne.edu>
- Loading branch information
1 parent
6e00052
commit b3a7846
Showing
8 changed files
with
236 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
benchmarks/Neo.Benchmarks/SmartContract/Benchmarks.StorageKey.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
// Copyright (C) 2015-2024 The Neo Project. | ||
// | ||
// Benchmarks.StorageKey.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.Text; | ||
|
||
namespace Neo.SmartContract.Benchmark | ||
{ | ||
public class Benchmarks_StorageKey | ||
{ | ||
// for avoiding overhead of encoding | ||
private static readonly byte[] testBytes = Encoding.ASCII.GetBytes("StorageKey"); | ||
|
||
private const int prefixSize = sizeof(int) + sizeof(byte); | ||
|
||
[Benchmark] | ||
public void KeyBuilder_AddInt() | ||
{ | ||
var key = new KeyBuilder(1, 0) | ||
.AddBigEndian(1) | ||
.AddBigEndian(2) | ||
.AddBigEndian(3); | ||
|
||
var bytes = key.ToArray(); | ||
if (bytes.Length != prefixSize + 3 * sizeof(int)) | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
[Benchmark] | ||
public void KeyBuilder_AddIntWithoutPrealloc() | ||
{ | ||
var key = new KeyBuilder(1, 0, 0) | ||
.AddBigEndian(1) | ||
.AddBigEndian(2) | ||
.AddBigEndian(3); | ||
|
||
var bytes = key.ToArray(); | ||
if (bytes.Length != prefixSize + 3 * sizeof(int)) | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
[Benchmark] | ||
public void KeyBuilder_AddBytes() | ||
{ | ||
var key = new KeyBuilder(1, 0) | ||
.Add(testBytes) | ||
.Add(testBytes) | ||
.Add(testBytes); | ||
|
||
var bytes = key.ToArray(); | ||
if (bytes.Length != prefixSize + 3 * testBytes.Length) | ||
throw new InvalidOperationException(); | ||
} | ||
|
||
[Benchmark] | ||
public void KeyBuilder_AddUInt160() | ||
{ | ||
Span<byte> value = stackalloc byte[UInt160.Length]; | ||
var key = new KeyBuilder(1, 0) | ||
.Add(new UInt160(value)); | ||
|
||
var bytes = key.ToArray(); | ||
if (bytes.Length != prefixSize + UInt160.Length) | ||
throw new InvalidOperationException(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters