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 17 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
15 changes: 10 additions & 5 deletions src/Neo.Extensions/ByteArrayEqualityComparer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,15 @@

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

namespace Neo.Extensions
{
public class ByteArrayEqualityComparer : IEqualityComparer<byte[]>
{
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 +28,12 @@ public bool Equals(byte[]? x, byte[]? y)
return x.AsSpan().SequenceEqual(y.AsSpan());
}

public int GetHashCode(byte[] obj)
{
return obj.XxHash3_32();
}
/// <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());
}
}
}
72 changes: 37 additions & 35 deletions src/Neo/SmartContract/StorageItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ namespace Neo.SmartContract
/// </summary>
public class StorageItem : ISerializable
{
private ReadOnlyMemory<byte> value;
private object cache;
private ReadOnlyMemory<byte> _value;
private object _cache;

public int Size => Value.GetVarSize();

Expand All @@ -35,7 +35,7 @@ public ReadOnlyMemory<byte> Value
{
get
{
return !value.IsEmpty ? value : value = cache switch
return !_value.IsEmpty ? _value : _value = _cache switch
{
BigInteger bi => bi.ToByteArrayStandard(),
IInteroperable interoperable => BinarySerializer.Serialize(interoperable.ToStackItem(null), ExecutionEngineLimits.Default),
Expand All @@ -45,8 +45,8 @@ public ReadOnlyMemory<byte> Value
}
set
{
this.value = value;
cache = null;
_value = value.ToArray(); // create new memory region
_cache = null;
}
}

Expand All @@ -61,7 +61,7 @@ public StorageItem() { }
/// <param name="value">The byte array value of the <see cref="StorageItem"/>.</param>
public StorageItem(byte[] value)
{
this.value = value;
_value = value.AsMemory().ToArray(); // allocate new buffer
}

/// <summary>
Expand All @@ -70,7 +70,7 @@ public StorageItem(byte[] value)
/// <param name="value">The integer value of the <see cref="StorageItem"/>.</param>
public StorageItem(BigInteger value)
{
cache = value;
_cache = value;
}

/// <summary>
Expand All @@ -79,7 +79,7 @@ public StorageItem(BigInteger value)
/// <param name="interoperable">The <see cref="IInteroperable"/> value of the <see cref="StorageItem"/>.</param>
public StorageItem(IInteroperable interoperable)
{
cache = interoperable;
_cache = interoperable;
}

/// <summary>
Expand All @@ -97,11 +97,13 @@ public void Add(BigInteger integer)
/// <returns>The created <see cref="StorageItem"/>.</returns>
public StorageItem Clone()
{
return new()
var newItem = new StorageItem
{
value = value,
cache = cache is IInteroperable interoperable ? interoperable.Clone() : cache
_value = _value.ToArray(), // allocate new buffer
_cache = _cache is IInteroperable interoperable ? interoperable.Clone() : _cache,
};

return newItem;
}

public void Deserialize(ref MemoryReader reader)
Expand All @@ -115,17 +117,17 @@ public void Deserialize(ref MemoryReader reader)
/// <param name="replica">The instance to be copied.</param>
public void FromReplica(StorageItem replica)
{
value = replica.value;
if (replica.cache is IInteroperable interoperable)
_value = replica._value.ToArray(); // allocate new buffer. DONT USE INSTANCE
if (replica._cache is IInteroperable interoperable)
{
if (cache?.GetType() == interoperable.GetType())
((IInteroperable)cache).FromReplica(interoperable);
if (_cache?.GetType() == interoperable.GetType())
((IInteroperable)_cache).FromReplica(interoperable);
else
cache = interoperable.Clone();
_cache = interoperable.Clone();
}
else
{
cache = replica.cache;
_cache = replica._cache;
}
}

Expand All @@ -136,14 +138,14 @@ public void FromReplica(StorageItem replica)
/// <returns>The <see cref="IInteroperable"/> in the storage.</returns>
public T GetInteroperable<T>() where T : IInteroperable, new()
{
if (cache is null)
if (_cache is null)
{
var interoperable = new T();
interoperable.FromStackItem(BinarySerializer.Deserialize(value, ExecutionEngineLimits.Default));
cache = interoperable;
interoperable.FromStackItem(BinarySerializer.Deserialize(_value, ExecutionEngineLimits.Default));
_cache = interoperable;
}
value = null;
return (T)cache;
_value = ReadOnlyMemory<byte>.Empty; // garbage collect the previous allocated memory space
return (T)_cache;
}

/// <summary>
Expand All @@ -154,14 +156,14 @@ public void FromReplica(StorageItem replica)
/// <returns>The <see cref="IInteroperable"/> in the storage.</returns>
public T GetInteroperable<T>(bool verify = true) where T : IInteroperableVerifiable, new()
{
if (cache is null)
if (_cache is null)
{
var interoperable = new T();
interoperable.FromStackItem(BinarySerializer.Deserialize(value, ExecutionEngineLimits.Default), verify);
cache = interoperable;
interoperable.FromStackItem(BinarySerializer.Deserialize(_value, ExecutionEngineLimits.Default), verify);
_cache = interoperable;
}
value = null;
return (T)cache;
_value = ReadOnlyMemory<byte>.Empty; // garbage collect the previous allocated memory space
return (T)_cache;
}

public void Serialize(BinaryWriter writer)
Expand All @@ -175,8 +177,8 @@ public void Serialize(BinaryWriter writer)
/// <param name="integer">The integer value to set.</param>
public void Set(BigInteger integer)
{
cache = integer;
value = null;
_cache = integer;
_value = ReadOnlyMemory<byte>.Empty; // garbage collect the previous allocated memory space
}

/// <summary>
Expand All @@ -185,24 +187,24 @@ public void Set(BigInteger integer)
/// <param name="interoperable">The <see cref="IInteroperable"/> value of the <see cref="StorageItem"/>.</param>
public void Set(IInteroperable interoperable)
{
cache = interoperable;
value = null;
_cache = interoperable;
_value = ReadOnlyMemory<byte>.Empty; // garbage collect the previous allocated memory space
}

public static implicit operator BigInteger(StorageItem item)
{
item.cache ??= new BigInteger(item.value.Span);
return (BigInteger)item.cache;
item._cache ??= new BigInteger(item._value.Span);
return (BigInteger)item._cache;
}

public static implicit operator StorageItem(BigInteger value)
{
return new StorageItem(value);
return new(value);
}

public static implicit operator StorageItem(byte[] value)
{
return new StorageItem(value);
return new(value);
}
}
}
Loading