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

[Neo Core Bug] fix compound type reference issue #3334

Merged
merged 17 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from 9 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
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// Benchmarks.cs file belongs to the neo project and is free
// Benchmarks.POC.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
Expand All @@ -9,18 +9,20 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using BenchmarkDotNet.Attributes;
using Neo.Network.P2P.Payloads;
using Neo.SmartContract;
using Neo.VM;
using System.Diagnostics;

namespace Neo;
namespace Neo.Benchmark;

static class Benchmarks
public class Benchmarks_PoCs
{
private static readonly ProtocolSettings protocol = ProtocolSettings.Load("config.json");
private static readonly NeoSystem system = new(protocol, (string)null);

[Benchmark]
public static void NeoIssue2725()
{
// https://github.com/neo-project/neo/issues/2725
Expand Down
1 change: 1 addition & 0 deletions benchmarks/Neo.Benchmarks/Neo.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Neo\Neo.csproj" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
</ItemGroup>

<ItemGroup>
Expand Down
9 changes: 3 additions & 6 deletions benchmarks/Neo.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo;
using System.Reflection;
using BenchmarkDotNet.Running;
using Neo.Benchmark;

foreach (var method in typeof(Benchmarks).GetMethods(BindingFlags.Public | BindingFlags.Static))
{
method.CreateDelegate<Action>().Invoke();
}
BenchmarkRunner.Run<Benchmarks_PoCs>();
shargon marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// Benchmarks.cs file belongs to the neo project and is free
// Benchmarks.POC.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
Expand All @@ -9,12 +9,14 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using BenchmarkDotNet.Attributes;
using System.Diagnostics;

namespace Neo.VM
namespace Neo.VM.Benchmark
{
public static class Benchmarks
public class Benchmarks_PoCs
{
[Benchmark]
public static void NeoIssue2528()
{
// https://github.com/neo-project/neo/issues/2528
Expand Down Expand Up @@ -47,6 +49,7 @@ public static void NeoIssue2528()
Run(nameof(NeoIssue2528), "VwEAwkpKAfsHdwARwG8AnXcAbwAl9////xHAzwJwlAAAdwAQzm8AnXcAbwAl9////0U=");
}

[Benchmark]
public static void NeoVMIssue418()
{
// https://github.com/neo-project/neo-vm/issues/418
Expand Down Expand Up @@ -81,6 +84,7 @@ public static void NeoVMIssue418()
Run(nameof(NeoVMIssue418), "whBNEcARTRHAVgEB/gGdYBFNEU0SwFMSwFhKJPNFUUU=");
}

[Benchmark]
public static void NeoIssue2723()
{
// L00: INITSSLOT 1
Expand Down
122 changes: 122 additions & 0 deletions benchmarks/Neo.VM.Benchmarks/Benchmarks.Types.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// Benchmarks.Types.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 Array = Neo.VM.Types.Array;

namespace Neo.VM.Benchmark;

public class Benchmarks_Types
{
public IEnumerable<(int Depth, int ElementsPerLevel)> ParamSource()
{
int[] depths = [4];
int[] elementsPerLevel = [6];

foreach (var depth in depths)
{
foreach (var elements in elementsPerLevel)
{
if (depth <= 8 || elements <= 2)
{
yield return (depth, elements);
}
}
}
}

[ParamsSource(nameof(ParamSource))]
public (int Depth, int ElementsPerLevel) Params;

[Benchmark]
public void BenchNestedArrayDeepCopy()
{
var root = new Array(new ReferenceCounter());
CreateNestedArray(root, Params.Depth, Params.ElementsPerLevel);
_ = root.DeepCopy();
}

[Benchmark]
public void BenchNestedArrayDeepCopyWithReferenceCounter()
{
var referenceCounter = new ReferenceCounter();
var root = new Array(referenceCounter);
CreateNestedArray(root, Params.Depth, Params.ElementsPerLevel, referenceCounter);
_ = root.DeepCopy();
}

[Benchmark]
public void BenchNestedTestArrayDeepCopy()
{
var root = new TestArray(new ReferenceCounter());
CreateNestedTestArray(root, Params.Depth, Params.ElementsPerLevel);
_ = root.DeepCopy();
}

[Benchmark]
public void BenchNestedTestArrayDeepCopyWithReferenceCounter()
{
var referenceCounter = new ReferenceCounter();
var root = new TestArray(referenceCounter);
CreateNestedTestArray(root, Params.Depth, Params.ElementsPerLevel, referenceCounter);
_ = root.DeepCopy();
}

private static void CreateNestedArray(Array rootArray, int depth, int elementsPerLevel = 1, ReferenceCounter referenceCounter = null)

Check warning on line 73 in benchmarks/Neo.VM.Benchmarks/Benchmarks.Types.cs

View workflow job for this annotation

GitHub Actions / Test-Everything

Cannot convert null literal to non-nullable reference type.

Check warning on line 73 in benchmarks/Neo.VM.Benchmarks/Benchmarks.Types.cs

View workflow job for this annotation

GitHub Actions / Test-Everything

Cannot convert null literal to non-nullable reference type.

Check warning on line 73 in benchmarks/Neo.VM.Benchmarks/Benchmarks.Types.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 73 in benchmarks/Neo.VM.Benchmarks/Benchmarks.Types.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 73 in benchmarks/Neo.VM.Benchmarks/Benchmarks.Types.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 73 in benchmarks/Neo.VM.Benchmarks/Benchmarks.Types.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Cannot convert null literal to non-nullable reference type.
shargon marked this conversation as resolved.
Show resolved Hide resolved
{
if (depth < 0)
{
throw new ArgumentException("Depth must be non-negative", nameof(depth));
}

if (rootArray == null)
{
throw new ArgumentNullException(nameof(rootArray));
}

if (depth == 0)
{
return;
}

for (var i = 0; i < elementsPerLevel; i++)
{
var childArray = new Array(referenceCounter);
rootArray.Add(childArray);
CreateNestedArray(childArray, depth - 1, elementsPerLevel, referenceCounter);
}
}

private static void CreateNestedTestArray(TestArray rootArray, int depth, int elementsPerLevel = 1, ReferenceCounter referenceCounter = null)

Check warning on line 98 in benchmarks/Neo.VM.Benchmarks/Benchmarks.Types.cs

View workflow job for this annotation

GitHub Actions / Test-Everything

Cannot convert null literal to non-nullable reference type.

Check warning on line 98 in benchmarks/Neo.VM.Benchmarks/Benchmarks.Types.cs

View workflow job for this annotation

GitHub Actions / Test-Everything

Cannot convert null literal to non-nullable reference type.

Check warning on line 98 in benchmarks/Neo.VM.Benchmarks/Benchmarks.Types.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 98 in benchmarks/Neo.VM.Benchmarks/Benchmarks.Types.cs

View workflow job for this annotation

GitHub Actions / Test (windows-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 98 in benchmarks/Neo.VM.Benchmarks/Benchmarks.Types.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Cannot convert null literal to non-nullable reference type.

Check warning on line 98 in benchmarks/Neo.VM.Benchmarks/Benchmarks.Types.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Cannot convert null literal to non-nullable reference type.
{
if (depth < 0)
{
throw new ArgumentException("Depth must be non-negative", nameof(depth));
}

if (rootArray == null)
{
throw new ArgumentNullException(nameof(rootArray));
}

if (depth == 0)
{
return;
}

for (var i = 0; i < elementsPerLevel; i++)
{
var childArray = new TestArray(referenceCounter);
rootArray.Add(childArray);
CreateNestedTestArray(childArray, depth - 1, elementsPerLevel, referenceCounter);
}
}
}
1 change: 1 addition & 0 deletions benchmarks/Neo.VM.Benchmarks/Neo.VM.Benchmarks.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

<ItemGroup>
<ProjectReference Include="..\..\src\Neo.VM\Neo.VM.csproj" />
<PackageReference Include="BenchmarkDotNet" Version="0.13.12" />
</ItemGroup>

</Project>
11 changes: 5 additions & 6 deletions benchmarks/Neo.VM.Benchmarks/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@
// Redistribution and use in source and binary forms with or without
// modifications are permitted.

using Neo.VM;
using System.Reflection;
using BenchmarkDotNet.Running;
using Neo.VM.Benchmark;

foreach (var method in typeof(Benchmarks).GetMethods(BindingFlags.Public | BindingFlags.Static))
{
method.CreateDelegate<Action>().Invoke();
}

// BenchmarkRunner.Run<Benchmarks_PoCs>();
BenchmarkRunner.Run<Benchmarks_Types>();
141 changes: 141 additions & 0 deletions benchmarks/Neo.VM.Benchmarks/TestArray.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
// Copyright (C) 2015-2024 The Neo Project.
//
// TestArray.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 Neo.VM.Types;
using System.Collections;

namespace Neo.VM.Benchmark;

public class TestArray : CompoundType, IReadOnlyList<StackItem>
{
protected readonly List<StackItem> _array;

/// <summary>
/// Get or set item in the array.
/// </summary>
/// <param name="index">The index of the item in the array.</param>
/// <returns>The item at the specified index.</returns>
public StackItem this[int index]
{
get => _array[index];
set
{
if (IsReadOnly) throw new InvalidOperationException("The object is readonly.");
ReferenceCounter?.RemoveReference(_array[index], this);
_array[index] = value;
ReferenceCounter?.AddReference(value, this);
}
}

/// <summary>
/// The number of items in the array.
/// </summary>
public override int Count => _array.Count;
public override IEnumerable<StackItem> SubItems => _array;
public override int SubItemsCount => _array.Count;
public override StackItemType Type => StackItemType.Array;

/// <summary>
/// Create an array containing the specified items.
/// </summary>
/// <param name="items">The items to be included in the array.</param>
public TestArray(IEnumerable<StackItem>? items = null)
: this(null, items)
{
}

/// <summary>
/// Create an array containing the specified items. And make the array use the specified <see cref="ReferenceCounter"/>.
/// </summary>
/// <param name="referenceCounter">The <see cref="ReferenceCounter"/> to be used by this array.</param>
/// <param name="items">The items to be included in the array.</param>
public TestArray(ReferenceCounter? referenceCounter, IEnumerable<StackItem>? items = null)
: base(referenceCounter)
{
_array = items switch
{
null => new List<StackItem>(),
List<StackItem> list => list,
_ => new List<StackItem>(items)
};
if (referenceCounter != null)
foreach (StackItem item in _array)
referenceCounter.AddReference(item, this);
}

/// <summary>
/// Add a new item at the end of the array.
/// </summary>
/// <param name="item">The item to be added.</param>
public void Add(StackItem item)
{
if (IsReadOnly) throw new InvalidOperationException("The object is readonly.");
_array.Add(item);
ReferenceCounter?.AddReference(item, this);
}

public override void Clear()
{
if (IsReadOnly) throw new InvalidOperationException("The object is readonly.");
if (ReferenceCounter != null)
foreach (StackItem item in _array)
ReferenceCounter.RemoveReference(item, this);
_array.Clear();
}

public override StackItem ConvertTo(StackItemType type)
{
if (Type == StackItemType.Array && type == StackItemType.Struct)
return new Struct(ReferenceCounter, new List<StackItem>(_array));
return base.ConvertTo(type);
}

internal sealed override StackItem DeepCopy(Dictionary<StackItem, StackItem> refMap, bool asImmutable)
{
if (refMap.TryGetValue(this, out StackItem? mappedItem)) return mappedItem;
var result = this is TestStruct ? new TestStruct(ReferenceCounter) : new TestArray(ReferenceCounter);
refMap.Add(this, result);
foreach (StackItem item in _array)
result.Add(item.DeepCopy(refMap, asImmutable));
result.IsReadOnly = true;
return result;
}

IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}

public IEnumerator<StackItem> GetEnumerator()
{
return _array.GetEnumerator();
}

/// <summary>
/// Remove the item at the specified index.
/// </summary>
/// <param name="index">The index of the item to be removed.</param>
public void RemoveAt(int index)
{
if (IsReadOnly) throw new InvalidOperationException("The object is readonly.");
ReferenceCounter?.RemoveReference(_array[index], this);
_array.RemoveAt(index);
}

/// <summary>
/// Reverse all items in the array.
/// </summary>
public void Reverse()
{
if (IsReadOnly) throw new InvalidOperationException("The object is readonly.");
_array.Reverse();
}
}
Loading