Skip to content
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

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 15 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
16 changes: 16 additions & 0 deletions src/Neo.Extensions/ByteArrayComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
// modifications are permitted.

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

Expand Down Expand Up @@ -47,5 +48,20 @@ public int Compare(byte[]? x, byte[]? y)
// value would be "int.MaxValue + 1"
return unchecked(x.AsSpan().SequenceCompareTo(y.AsSpan()) * _direction);
}

/// <inheritdoc />
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public int Compare(object? x, object? y)
{
if (ReferenceEquals(x, y)) return 0;

if (x is not null and not byte[])
throw new ArgumentException($"Unable to cast '{x.GetType().FullName}' to '{typeof(byte[]).FullName}'.");

if (y is not null and not byte[])
throw new ArgumentException($"Unable to cast '{y.GetType().FullName}' to '{typeof(byte[]).FullName}'.");

return Compare(x as byte[], y as byte[]);
}
}
}
28 changes: 24 additions & 4 deletions src/Neo.Extensions/ByteArrayEqualityComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,17 @@
// modifications are permitted.

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace Neo.Extensions
{
public class ByteArrayEqualityComparer : IEqualityComparer<byte[]>
public class ByteArrayEqualityComparer : IEqualityComparer<byte[]>, IEqualityComparer
{
public static readonly ByteArrayEqualityComparer Default = new();
public static readonly ByteArrayEqualityComparer Instance = new();
Copy link
Member

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


/// <inheritdoc />
public bool Equals(byte[]? x, byte[]? y)
{
if (ReferenceEquals(x, y)) return true;
Expand All @@ -26,9 +29,26 @@ public bool Equals(byte[]? x, byte[]? y)
return x.AsSpan().SequenceEqual(y.AsSpan());
}

public int GetHashCode(byte[] obj)
/// <inheritdoc />
public new bool Equals(object? x, object? y)
{
return obj.XxHash3_32();
if (ReferenceEquals(x, y)) return true; // Check is `null` or same object instance

if (x is not null and not byte[])
throw new ArgumentException($"Unable to cast '{x.GetType().FullName}' to '{typeof(byte[]).FullName}'.");

if (y is not null and not byte[])
throw new ArgumentException($"Unable to cast '{y.GetType().FullName}' to '{typeof(byte[]).FullName}'.");

return Equals(x as byte[], y as byte[]); // if x or y isn't byte array they will be `null`
shargon marked this conversation as resolved.
Show resolved Hide resolved
}

/// <inheritdoc />
public int GetHashCode([DisallowNull] byte[] obj) =>
shargon marked this conversation as resolved.
Show resolved Hide resolved
obj.XxHash3_32();

/// <inheritdoc />
public int GetHashCode([DisallowNull] object obj) =>
obj is byte[] b ? GetHashCode(b) : 0;
}
}
2 changes: 1 addition & 1 deletion src/Neo/IO/Caching/ECPointCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ namespace Neo.IO.Caching
internal class ECPointCache : FIFOCache<byte[], ECPoint>
{
public ECPointCache(int max_capacity)
: base(max_capacity, ByteArrayEqualityComparer.Default)
: base(max_capacity, ByteArrayEqualityComparer.Instance)
{
}

Expand Down
11 changes: 8 additions & 3 deletions src/Neo/Persistence/MemorySnapshot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ internal class MemorySnapshot : ISnapshot
public MemorySnapshot(ConcurrentDictionary<byte[], byte[]> innerData)
{
_innerData = innerData;
_immutableData = innerData.ToImmutableDictionary(ByteArrayEqualityComparer.Default);
_writeBatch = new ConcurrentDictionary<byte[], byte[]?>(ByteArrayEqualityComparer.Default);
_immutableData = innerData.ToImmutableDictionary(ByteArrayEqualityComparer.Instance);
_writeBatch = new ConcurrentDictionary<byte[], byte[]?>(ByteArrayEqualityComparer.Instance);
}

public void Commit()
Expand Down Expand Up @@ -61,16 +61,21 @@ public void Put(byte[] key, byte[] value)
public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte[]? keyOrPrefix, SeekDirection direction = SeekDirection.Forward)
{
keyOrPrefix ??= [];

if (direction == SeekDirection.Backward && keyOrPrefix.Length == 0) yield break;

var comparer = direction == SeekDirection.Forward ? ByteArrayComparer.Default : ByteArrayComparer.Reverse;

IEnumerable<KeyValuePair<byte[], byte[]>> records = _immutableData;

if (keyOrPrefix.Length > 0)
records = records
.Where(p => comparer.Compare(p.Key, keyOrPrefix) >= 0);

records = records.OrderBy(p => p.Key, comparer);

foreach (var pair in records)
yield return (pair.Key[..], pair.Value[..]);
yield return new(pair.Key[..], pair.Value[..]);
}

public byte[]? TryGet(byte[] key)
Expand Down
9 changes: 6 additions & 3 deletions src/Neo/Persistence/MemoryStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Neo.Persistence
/// </summary>
public class MemoryStore : IStore
{
private readonly ConcurrentDictionary<byte[], byte[]> _innerData = new(ByteArrayEqualityComparer.Default);
private readonly ConcurrentDictionary<byte[], byte[]> _innerData = new(ByteArrayEqualityComparer.Instance);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Delete(byte[] key)
Expand All @@ -51,16 +51,19 @@ public void Put(byte[] key, byte[] value)
public IEnumerable<(byte[] Key, byte[] Value)> Seek(byte[]? keyOrPrefix, SeekDirection direction = SeekDirection.Forward)
{
keyOrPrefix ??= [];
if (direction == SeekDirection.Backward && keyOrPrefix.Length == 0) yield break;

if (direction == SeekDirection.Backward && keyOrPrefix.Length == 0) yield break;
var comparer = direction == SeekDirection.Forward ? ByteArrayComparer.Default : ByteArrayComparer.Reverse;

IEnumerable<KeyValuePair<byte[], byte[]>> records = _innerData;

if (keyOrPrefix.Length > 0)
records = records
.Where(p => comparer.Compare(p.Key, keyOrPrefix) >= 0);
records = records.OrderBy(p => p.Key, comparer);

foreach (var pair in records)
yield return (pair.Key[..], pair.Value[..]);
yield return new(pair.Key[..], pair.Value[..]);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
39 changes: 17 additions & 22 deletions src/Neo/SmartContract/KeyBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -22,7 +22,8 @@ namespace Neo.SmartContract
/// </summary>
public class KeyBuilder
{
private readonly MemoryStream stream;
private readonly Memory<byte> _cachedData;
private int _keyLen = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

keySizeHint is a hint.
The actual length may exceed the keySizeHint.
So the KeyBuilder cannot use Memory<byte> instead of MemoryStream.

Copy link
Member

Choose a reason for hiding this comment

The 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

Copy link
Member

Choose a reason for hiding this comment

The 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.
Expand All @@ -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>
Expand All @@ -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;
}

Expand All @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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());
}
}
}
Loading