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 VM] adopt light gc from neogo #3581

Open
wants to merge 40 commits into
base: HF_Echidna
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
afa99ca
adopt light gc
Jim8y Nov 15, 2024
a6b3f69
format
Jim8y Nov 15, 2024
4874a6e
fix building issue in benchmark
Jim8y Nov 16, 2024
a8fd587
fix popitem
Jim8y Nov 16, 2024
4de3809
revert change
Jim8y Nov 17, 2024
d7cde61
revert to fix compatibillity
Jim8y Nov 17, 2024
e9061e5
Merge branch 'HF_Echidna' into rc-v2
Jim8y Nov 17, 2024
5d9d7c4
adopt new solution as using null rc, instead of making large scale ch…
Jim8y Nov 17, 2024
0ccd831
avoid change that is currently not necessary
Jim8y Nov 17, 2024
fc8df97
revert null default
Jim8y Nov 17, 2024
6acfc2c
revert null default
Jim8y Nov 17, 2024
2d6deab
revert null
Jim8y Nov 17, 2024
6200bdb
apply compound update and apply hardfork
Jim8y Nov 17, 2024
06fc016
fix stack
Jim8y Nov 17, 2024
4bb8043
fix applicationengine rc version issue
Jim8y Nov 17, 2024
e735b5e
update ut to adopt RCV2
Jim8y Nov 17, 2024
24efc8b
fix UT
Jim8y Nov 17, 2024
aa58fc9
update UT and bench, and apply hecate suggestion
Jim8y Nov 18, 2024
067d3bc
Merge branch 'HF_Echidna' into rc-v2
Jim8y Nov 18, 2024
83c3d2f
update exception message
Jim8y Nov 18, 2024
c2582e2
Update src/Neo.VM/Types/CompoundType.cs
shargon Nov 20, 2024
5d8921c
define and use isRC2
Jim8y Nov 21, 2024
0cb2432
Merge branch 'rc-v2' of github.com:Jim8y/neo into rc-v2
Jim8y Nov 21, 2024
1da5aa7
Avoid replace benchmarks
shargon Nov 21, 2024
ae56086
Clean interface
shargon Nov 21, 2024
ce7aa88
Clean array
shargon Nov 21, 2024
0c16261
Solve HF problem
shargon Nov 21, 2024
c9833da
Fix compile
shargon Nov 21, 2024
07e8a40
Clean code
shargon Nov 21, 2024
c8346d0
Reduce changes
shargon Nov 21, 2024
c625280
Reduce changes in ExecutionEngine and use the same order
shargon Nov 21, 2024
d2960fa
Fix bug and optimize
shargon Nov 21, 2024
a3901cb
Fix comment
shargon Nov 21, 2024
4a65dfd
Update src/Neo.VM/JumpTable/JumpTable.Compound.cs
shargon Nov 21, 2024
8e77783
Avoid exceptions if old method is called
shargon Nov 21, 2024
92b2873
Move logic to CheckPostExecution
shargon Nov 21, 2024
dbf054d
Merge branch 'HF_Echidna' into rc-v2
shargon Dec 19, 2024
6dbc104
Update src/Neo.VM/ReferenceCounter/ReferenceCounterV2.cs
shargon Dec 19, 2024
552991f
Update src/Neo.VM/ReferenceCounter/ReferenceCounterV2.cs
shargon Dec 19, 2024
95f6535
Update src/Neo.VM/ReferenceCounter/ReferenceCounterV2.cs
shargon Jan 7, 2025
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
26 changes: 4 additions & 22 deletions benchmarks/Neo.VM.Benchmarks/TestArray.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,7 @@ public StackItem this[int index]
set
{
if (IsReadOnly) throw new InvalidOperationException("The object is readonly.");
ReferenceCounter?.RemoveReference(_array[index], this);
_array[index] = value;
ReferenceCounter?.AddReference(value, this);
}
}

Expand All @@ -43,32 +41,21 @@ public StackItem this[int index]
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="IReferenceCounter"/>.
/// </summary>
/// <param name="referenceCounter">The <see cref="IReferenceCounter"/> to be used by this array.</param>
/// <param name="items">The items to be included in the array.</param>
public TestArray(IReferenceCounter? referenceCounter, IEnumerable<StackItem>? items = null)
: base(referenceCounter)
public TestArray(IEnumerable<StackItem>? items = null)
: base()
{
_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>
Expand All @@ -79,29 +66,25 @@ 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 new Struct(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);
var result = this is TestStruct ? new TestStruct() : new TestArray();
refMap.Add(this, result);
foreach (StackItem item in _array)
result.Add(item.DeepCopy(refMap, asImmutable));
Expand All @@ -126,7 +109,6 @@ public IEnumerator<StackItem> GetEnumerator()
public void RemoveAt(int index)
{
if (IsReadOnly) throw new InvalidOperationException("The object is readonly.");
ReferenceCounter?.RemoveReference(_array[index], this);
_array.RemoveAt(index);
}

Expand Down
18 changes: 5 additions & 13 deletions benchmarks/Neo.VM.Benchmarks/TestStruct.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,22 +17,14 @@ public class TestStruct : TestArray
{
public override StackItemType Type => StackItemType.Struct;

/// <summary>
/// Create a structure with the specified fields.
/// </summary>
/// <param name="fields">The fields to be included in the structure.</param>
public TestStruct(IEnumerable<StackItem>? fields = null)
: this(null, fields)
{
}

/// <summary>
/// Create a structure with the specified fields. And make the structure use the specified <see cref="IReferenceCounter"/>.
/// </summary>
/// <param name="referenceCounter">The <see cref="IReferenceCounter"/> to be used by this structure.</param>
/// <param name="fields">The fields to be included in the structure.</param>
public TestStruct(IReferenceCounter? referenceCounter, IEnumerable<StackItem>? fields = null)
: base(referenceCounter, fields)
public TestStruct(IEnumerable<StackItem>? fields = null)
: base(fields)
{
}

Expand All @@ -44,7 +36,7 @@ public TestStruct(IReferenceCounter? referenceCounter, IEnumerable<StackItem>? f
public TestStruct Clone(ExecutionEngineLimits limits)
{
int count = (int)(limits.MaxStackSize - 1);
TestStruct result = new(ReferenceCounter);
TestStruct result = new();
Queue<TestStruct> queue = new();
queue.Enqueue(result);
queue.Enqueue(this);
Expand All @@ -58,7 +50,7 @@ public TestStruct Clone(ExecutionEngineLimits limits)
if (count < 0) throw new InvalidOperationException("Beyond clone limits!");
if (item is TestStruct sb)
{
TestStruct sa = new(ReferenceCounter);
TestStruct sa = new();
a.Add(sa);
queue.Enqueue(sa);
queue.Enqueue(sb);
Expand All @@ -75,7 +67,7 @@ public TestStruct Clone(ExecutionEngineLimits limits)
public override StackItem ConvertTo(StackItemType type)
{
if (type == StackItemType.Array)
return new TestArray(ReferenceCounter, new List<StackItem>(_array));
return new TestArray(new List<StackItem>(_array));
return base.ConvertTo(type);
}

Expand Down
14 changes: 7 additions & 7 deletions benchmarks/Neo.VM.Benchmarks/VMTypes/Benchmarks_Convert.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public IEnumerable<object[]> GetTypeConversionPairs()

private Dictionary<StackItemType, List<StackItem>> CreateTestItemsByType()
{
var referenceCounter = new ReferenceCounter();
var referenceCounter = new ReferenceCounterV2();
var result = new Dictionary<StackItemType, List<StackItem>>();

foreach (StackItemType type in Enum.GetValues(typeof(StackItemType)))
Expand All @@ -84,22 +84,22 @@ private Dictionary<StackItemType, List<StackItem>> CreateTestItemsByType()
result[StackItemType.Buffer].Add(new Buffer(new byte[128])); // Another 128-byte buffer, all zeros

// Create an array with 10 items
var longArray = new Array(referenceCounter);
var longArray = new Array();
for (int i = 0; i < 10; i++) longArray.Add(new Integer(i));
result[StackItemType.Array].Add(longArray);
result[StackItemType.Array].Add(new Array(referenceCounter) { StackItem.True, new ByteString(new byte[] { 3, 4, 5 }) });
result[StackItemType.Array].Add(new Array() { StackItem.True, new ByteString(new byte[] { 3, 4, 5 }) });

// Create a struct with 10 items
var longStruct = new Struct(referenceCounter);
var longStruct = new Struct();
for (int i = 0; i < 10; i++) longStruct.Add(new Integer(i * 10));
result[StackItemType.Struct].Add(longStruct);
result[StackItemType.Struct].Add(new Struct(referenceCounter) { StackItem.False, new Buffer(new byte[] { 6, 7, 8 }) });
result[StackItemType.Struct].Add(new Struct() { StackItem.False, new Buffer(new byte[] { 6, 7, 8 }) });

// Create a map with 10 items
var longMap = new Map(referenceCounter);
var longMap = new Map();
for (int i = 0; i < 10; i++) longMap[new Integer(i)] = new ByteString(new byte[] { (byte)(i * 20) });
result[StackItemType.Map].Add(longMap);
result[StackItemType.Map].Add(new Map(referenceCounter) { [new ByteString(new byte[] { 9 })] = StackItem.True });
result[StackItemType.Map].Add(new Map() { [new ByteString(new byte[] { 9 })] = StackItem.True });

result[StackItemType.InteropInterface].Add(new InteropInterface(new object()));
result[StackItemType.InteropInterface].Add(new InteropInterface("test string"));
Expand Down
28 changes: 14 additions & 14 deletions benchmarks/Neo.VM.Benchmarks/VMTypes/Benchmarks_DeepCopy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,38 +39,38 @@ public class Benchmarks_DeepCopy
[Benchmark]
public void BenchNestedArrayDeepCopy()
{
var root = new Array(new ReferenceCounter());
var root = new Array();
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);
var referenceCounter = new ReferenceCounterV2();
var root = new Array();
CreateNestedArray(root, Params.Depth, Params.ElementsPerLevel);
_ = root.DeepCopy();
}

[Benchmark]
public void BenchNestedTestArrayDeepCopy()
{
var root = new TestArray(new ReferenceCounter());
var root = new TestArray();
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);
var referenceCounter = new ReferenceCounterV2();
var root = new TestArray();
CreateNestedTestArray(root, Params.Depth, Params.ElementsPerLevel);
_ = root.DeepCopy();
}

private static void CreateNestedArray(Array? rootArray, int depth, int elementsPerLevel = 1, IReferenceCounter? referenceCounter = null)
private static void CreateNestedArray(Array? rootArray, int depth, int elementsPerLevel = 1)
{
if (depth < 0)
{
Expand All @@ -89,13 +89,13 @@ private static void CreateNestedArray(Array? rootArray, int depth, int elementsP

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

private static void CreateNestedTestArray(TestArray rootArray, int depth, int elementsPerLevel = 1, IReferenceCounter? referenceCounter = null)
private static void CreateNestedTestArray(TestArray rootArray, int depth, int elementsPerLevel = 1)
{
if (depth < 0)
{
Expand All @@ -114,9 +114,9 @@ private static void CreateNestedTestArray(TestArray rootArray, int depth, int el

for (var i = 0; i < elementsPerLevel; i++)
{
var childArray = new TestArray(referenceCounter);
var childArray = new TestArray();
rootArray.Add(childArray);
CreateNestedTestArray(childArray, depth - 1, elementsPerLevel, referenceCounter);
CreateNestedTestArray(childArray, depth - 1, elementsPerLevel);
}
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/Neo.VM/ExecutionEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ protected internal set
/// Initializes a new instance of the <see cref="ExecutionEngine"/> class.
/// </summary>
/// <param name="jumpTable">The jump table to be used.</param>
public ExecutionEngine(JumpTable? jumpTable = null) : this(jumpTable, new ReferenceCounter(), ExecutionEngineLimits.Default)
public ExecutionEngine(JumpTable? jumpTable = null) : this(jumpTable, new ReferenceCounterV2(), ExecutionEngineLimits.Default)
{
}

Expand Down Expand Up @@ -289,8 +289,7 @@ public T Pop<T>() where T : StackItem
/// </summary>
protected virtual void PostExecuteInstruction(Instruction instruction)
{
if (ReferenceCounter.Count < Limits.MaxStackSize) return;
if (ReferenceCounter.CheckZeroReferred() > Limits.MaxStackSize)
if (ReferenceCounter.Count > Limits.MaxStackSize)
throw new InvalidOperationException($"MaxStackSize exceed: {ReferenceCounter.Count}");
}

Expand Down
Loading
Loading