-
Notifications
You must be signed in to change notification settings - Fork 1k
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
[Fixes/Optimizations/Styles
] KeyBuilder, StorageKey, StorageItem, MemorySnapshot, MemoryStore, ByteArrayComparer & ByteArrayEqualityComparer
#3705
base: master
Are you sure you want to change the base?
Changes from 15 commits
1bf58cd
3f6811a
d3f7327
5a2e4e9
96d20d3
8037ddf
53cfb06
2058a28
e5d25b2
db7d314
abf93fb
c206715
f32faf0
6dd4bb9
de4c2df
b4e5418
f6b0c11
a8af5a6
dda49b0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,10 +9,10 @@ | |
// Redistribution and use in source and binary forms with or without | ||
// modifications are permitted. | ||
|
||
using Neo.Extensions; | ||
using Neo.IO; | ||
using System; | ||
using System.Buffers.Binary; | ||
using System.IO; | ||
using System.Runtime.CompilerServices; | ||
|
||
namespace Neo.SmartContract | ||
|
@@ -22,7 +22,8 @@ namespace Neo.SmartContract | |
/// </summary> | ||
public class KeyBuilder | ||
{ | ||
private readonly MemoryStream stream; | ||
private readonly Memory<byte> _cachedData; | ||
private int _keyLen = 0; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think the same, but the true is that it will speed up the code a lot of, maybe we should check all the keyBuilds and force to be less than this length There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. In my opinion this 3 classes should be improved in different PR's because I'm agree with some changes and not with others |
||
|
||
/// <summary> | ||
/// Initializes a new instance of the <see cref="KeyBuilder"/> class. | ||
|
@@ -32,12 +33,11 @@ public class KeyBuilder | |
/// <param name="keySizeHint">The hint of the storage key size(including the id and prefix).</param> | ||
public KeyBuilder(int id, byte prefix, int keySizeHint = ApplicationEngine.MaxStorageKeySize) | ||
cschuchardt88 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
Span<byte> data = stackalloc byte[sizeof(int)]; | ||
BinaryPrimitives.WriteInt32LittleEndian(data, id); | ||
_cachedData = new byte[keySizeHint]; | ||
cschuchardt88 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
BinaryPrimitives.WriteInt32LittleEndian(_cachedData.Span, id); | ||
|
||
stream = new(keySizeHint); | ||
stream.Write(data); | ||
stream.WriteByte(prefix); | ||
_keyLen = sizeof(int); | ||
_cachedData.Span[_keyLen++] = prefix; | ||
} | ||
|
||
/// <summary> | ||
|
@@ -48,7 +48,7 @@ public KeyBuilder(int id, byte prefix, int keySizeHint = ApplicationEngine.MaxSt | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public KeyBuilder Add(byte key) | ||
{ | ||
stream.WriteByte(key); | ||
_cachedData.Span[_keyLen++] = key; | ||
return this; | ||
} | ||
|
||
|
@@ -60,7 +60,8 @@ public KeyBuilder Add(byte key) | |
[MethodImpl(MethodImplOptions.AggressiveInlining)] | ||
public KeyBuilder Add(ReadOnlySpan<byte> key) | ||
{ | ||
stream.Write(key); | ||
key.CopyTo(_cachedData.Span[_keyLen..]); | ||
_keyLen += key.Length; | ||
return this; | ||
} | ||
|
||
|
@@ -95,11 +96,11 @@ public KeyBuilder Add(ReadOnlySpan<byte> key) | |
/// <returns>A reference to this instance after the add operation has completed.</returns> | ||
public KeyBuilder Add(ISerializable key) | ||
{ | ||
using (BinaryWriter writer = new(stream, Utility.StrictUTF8, true)) | ||
{ | ||
key.Serialize(writer); | ||
writer.Flush(); | ||
} | ||
var raw = key.ToArray(); | ||
|
||
raw.CopyTo(_cachedData[_keyLen..]); | ||
_keyLen += raw.Length; | ||
|
||
return this; | ||
} | ||
|
||
|
@@ -165,18 +166,12 @@ public KeyBuilder AddBigEndian(ulong key) | |
/// <returns>The storage key.</returns> | ||
public byte[] ToArray() | ||
{ | ||
using (stream) | ||
{ | ||
return stream.ToArray(); | ||
} | ||
return _cachedData[.._keyLen].ToArray(); | ||
} | ||
|
||
public static implicit operator StorageKey(KeyBuilder builder) | ||
{ | ||
using (builder.stream) | ||
{ | ||
return new StorageKey(builder.stream.ToArray()); | ||
} | ||
return new StorageKey(builder._cachedData[..builder._keyLen].ToArray()); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Default
should be marked as obsolete because it's public