Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
shargon committed Oct 10, 2024
1 parent dbc3e7e commit ca3e0cc
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 69 deletions.
4 changes: 2 additions & 2 deletions benchmarks/Neo.VM.Benchmarks/VMTypes/Benchmarks_DeepCopy.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public void BenchNestedTestArrayDeepCopyWithReferenceCounter()
_ = root.DeepCopy();
}

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

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

Check warning on line 98 in benchmarks/Neo.VM.Benchmarks/VMTypes/Benchmarks_DeepCopy.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/VMTypes/Benchmarks_DeepCopy.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/VMTypes/Benchmarks_DeepCopy.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/VMTypes/Benchmarks_DeepCopy.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/VMTypes/Benchmarks_DeepCopy.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/VMTypes/Benchmarks_DeepCopy.cs

View workflow job for this annotation

GitHub Actions / Test (macos-latest)

Cannot convert null literal to non-nullable reference type.
{
if (depth < 0)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Neo.VM/EvaluationStack.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ internal EvaluationStack(IReferenceCounter referenceCounter)

internal void Clear()
{
foreach (var item in innerList)
foreach (StackItem item in innerList)
referenceCounter.RemoveStackReference(item);
innerList.Clear();
}
Expand Down
67 changes: 65 additions & 2 deletions src/Neo.VM/IReferenceCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,79 @@

namespace Neo.VM
{
/// <summary>
/// Used for reference counting of objects in the VM.
/// </summary>
public interface IReferenceCounter
{
/// <summary>
/// Gets the count of references.
/// </summary>
int Count { get; }

/// <summary>
/// Adds an item to the zero-referred list.
///
/// This method is used when an item has no remaining references.
/// It adds the item to the zero-referred list to be checked for cleanup later.
///
/// Use this method when you detect that an item has zero references and may need to be cleaned up.
/// </summary>
/// <param name="item">The item to add.</param>
void AddZeroReferred(StackItem item);
void AddReference(StackItem item, CompoundType compoundType);
void RemoveReference(StackItem item, CompoundType compoundType);

/// <summary>
/// Adds a reference to a specified item with a parent compound type.
///
/// This method is used when an item gains a new reference through a parent compound type.
/// It increments the reference count and updates the tracking structures if necessary.
///
/// Use this method when you need to add a reference from a compound type to a stack item.
/// </summary>
/// <param name="item">The item to add a reference to.</param>
/// <param name="parent">The parent compound type.</param>
void AddReference(StackItem item, CompoundType parent);

/// <summary>
/// Adds a stack reference to a specified item with a count.
///
/// This method is used when an item gains a new stack reference, usually due to being pushed onto the evaluation stack.
/// It increments the reference count and updates the tracking structures if necessary.
///
/// Use this method when you need to add one or more stack references to a stack item.
/// </summary>
/// <param name="item">The item to add a stack reference to.</param>
/// <param name="count">The number of references to add.</param>
void AddStackReference(StackItem item, int count = 1);

/// <summary>
/// Removes a reference from a specified item with a parent compound type.
///
/// This method is used when an item loses a reference from a parent compound type.
/// It decrements the reference count and updates the tracking structures if necessary.
///
/// Use this method when you need to remove a reference from a compound type to a stack item.
/// </summary>
/// <param name="item">The item to remove a reference from.</param>
/// <param name="parent">The parent compound type.</param>
void RemoveReference(StackItem item, CompoundType parent);

/// <summary>
/// Removes a stack reference from a specified item.
///
/// This method is used when an item loses a stack reference, usually due to being popped off the evaluation stack.
/// It decrements the reference count and updates the tracking structures if necessary.
///
/// Use this method when you need to remove one or more stack references from a stack item.
/// </summary>
/// <param name="item">The item to remove a stack reference from.</param>
void RemoveStackReference(StackItem item);

/// <summary>
/// Checks and processes items that have zero references.
/// This method is used to check items in the zero-referred list and clean up those that are no longer needed.
/// </summary>
/// <returns>The current reference count.</returns>
int CheckZeroReferred();
}
}
58 changes: 6 additions & 52 deletions src/Neo.VM/ReferenceCounter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,7 @@ public sealed class ReferenceCounter : IReferenceCounter
// Keeps the total count of references.
private int _referencesCount = 0;

/// <summary>
/// Gets the count of references.
/// </summary>
/// <inheritdoc/>
public int Count => _referencesCount;

/// <summary>
Expand All @@ -61,16 +59,7 @@ private static bool NeedTrack(StackItem item)
return false;
}

/// <summary>
/// Adds a reference to a specified item with a parent compound type.
///
/// This method is used when an item gains a new reference through a parent compound type.
/// It increments the reference count and updates the tracking structures if necessary.
///
/// Use this method when you need to add a reference from a compound type to a stack item.
/// </summary>
/// <param name="item">The item to add a reference to.</param>
/// <param name="parent">The parent compound type.</param>
/// <inheritdoc/>
public void AddReference(StackItem item, CompoundType parent)
{
// Increment the reference count.
Expand Down Expand Up @@ -98,16 +87,7 @@ public void AddReference(StackItem item, CompoundType parent)
pEntry.References++;
}

/// <summary>
/// Adds a stack reference to a specified item with a count.
///
/// This method is used when an item gains a new stack reference, usually due to being pushed onto the evaluation stack.
/// It increments the reference count and updates the tracking structures if necessary.
///
/// Use this method when you need to add one or more stack references to a stack item.
/// </summary>
/// <param name="item">The item to add a stack reference to.</param>
/// <param name="count">The number of references to add.</param>
/// <inheritdoc/>
public void AddStackReference(StackItem item, int count = 1)
{
// Increment the reference count by the specified count.
Expand All @@ -127,15 +107,7 @@ public void AddStackReference(StackItem item, int count = 1)
_zeroReferred.Remove(item);
}

/// <summary>
/// Adds an item to the zero-referred list.
///
/// This method is used when an item has no remaining references.
/// It adds the item to the zero-referred list to be checked for cleanup later.
///
/// Use this method when you detect that an item has zero references and may need to be cleaned up.
/// </summary>
/// <param name="item">The item to add.</param>
/// <inheritdoc/>
public void AddZeroReferred(StackItem item)
{
// Add the item to the _zeroReferred set.
Expand Down Expand Up @@ -241,17 +213,7 @@ public int CheckZeroReferred()
return _referencesCount;
}


/// <summary>
/// Removes a reference from a specified item with a parent compound type.
///
/// This method is used when an item loses a reference from a parent compound type.
/// It decrements the reference count and updates the tracking structures if necessary.
///
/// Use this method when you need to remove a reference from a compound type to a stack item.
/// </summary>
/// <param name="item">The item to remove a reference from.</param>
/// <param name="parent">The parent compound type.</param>
/// <inheritdoc/>
public void RemoveReference(StackItem item, CompoundType parent)
{
// Decrement the reference count.
Expand All @@ -271,15 +233,7 @@ public void RemoveReference(StackItem item, CompoundType parent)
_zeroReferred.Add(item);
}

/// <summary>
/// Removes a stack reference from a specified item.
///
/// This method is used when an item loses a stack reference, usually due to being popped off the evaluation stack.
/// It decrements the reference count and updates the tracking structures if necessary.
///
/// Use this method when you need to remove one or more stack references from a stack item.
/// </summary>
/// <param name="item">The item to remove a stack reference from.</param>
/// <inheritdoc/>
public void RemoveStackReference(StackItem item)
{
// Decrement the reference count.
Expand Down
9 changes: 2 additions & 7 deletions src/Neo.VM/ReferenceCounterV2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ public void RemoveStackReference(StackItem item)
Count--;
}

public void AddReference(StackItem item, CompoundType compoundType)
public void AddReference(StackItem item, CompoundType parent)
{
AddStackReference(item);
}

public void RemoveReference(StackItem item, CompoundType compoundType)
public void RemoveReference(StackItem item, CompoundType parent)
{
RemoveStackReference(item);
}
Expand All @@ -94,11 +94,6 @@ public void AddZeroReferred(StackItem item)

/// <summary>
/// Checks and processes items that have zero references.
///
/// This method is used to check items in the zero-referred list and clean up those that are no longer needed.
/// It uses Tarjan's algorithm to find strongly connected components and remove those with no references.
///
/// Use this method periodically to clean up items with zero references and free up memory.
/// </summary>
/// <returns>The current reference count.</returns>
public int CheckZeroReferred()
Expand Down
6 changes: 3 additions & 3 deletions src/Neo/SmartContract/BinarySerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public ContainerPlaceholder(StackItemType type, int count)
/// </summary>
/// <param name="data">The byte array to parse.</param>
/// <param name="limits">The limits for the deserialization.</param>
/// <param name="referenceCounter">The <see cref="ReferenceCounter"/> used by the <see cref="StackItem"/>.</param>
/// <param name="referenceCounter">The <see cref="IReferenceCounter"/> used by the <see cref="StackItem"/>.</param>
/// <returns>The deserialized <see cref="StackItem"/>.</returns>
public static StackItem Deserialize(ReadOnlyMemory<byte> data, ExecutionEngineLimits limits, IReferenceCounter referenceCounter = null)
{
Expand All @@ -64,7 +64,7 @@ public static StackItem Deserialize(ReadOnlyMemory<byte> data, ExecutionEngineLi
/// </summary>
/// <param name="reader">The <see cref="MemoryReader"/> for reading data.</param>
/// <param name="limits">The limits for the deserialization.</param>
/// <param name="referenceCounter">The <see cref="ReferenceCounter"/> used by the <see cref="StackItem"/>.</param>
/// <param name="referenceCounter">The <see cref="IReferenceCounter"/> used by the <see cref="StackItem"/>.</param>
/// <returns>The deserialized <see cref="StackItem"/>.</returns>
public static StackItem Deserialize(ref MemoryReader reader, ExecutionEngineLimits limits, IReferenceCounter referenceCounter = null)
{
Expand All @@ -77,7 +77,7 @@ public static StackItem Deserialize(ref MemoryReader reader, ExecutionEngineLimi
/// <param name="reader">The <see cref="MemoryReader"/> for reading data.</param>
/// <param name="maxSize">The maximum size of the result.</param>
/// <param name="maxItems">The max of items to serialize</param>
/// <param name="referenceCounter">The <see cref="ReferenceCounter"/> used by the <see cref="StackItem"/>.</param>
/// <param name="referenceCounter">The <see cref="IReferenceCounter"/> used by the <see cref="StackItem"/>.</param>
/// <returns>The deserialized <see cref="StackItem"/>.</returns>
public static StackItem Deserialize(ref MemoryReader reader, uint maxSize, uint maxItems, IReferenceCounter referenceCounter = null)
{
Expand Down
2 changes: 1 addition & 1 deletion src/Neo/SmartContract/IInteroperable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public interface IInteroperable
/// <summary>
/// Convert the current object to a <see cref="StackItem"/>.
/// </summary>
/// <param name="referenceCounter">The <see cref="ReferenceCounter"/> used by the <see cref="StackItem"/>.</param>
/// <param name="referenceCounter">The <see cref="IReferenceCounter"/> used by the <see cref="StackItem"/>.</param>
/// <returns>The converted <see cref="StackItem"/>.</returns>
StackItem ToStackItem(IReferenceCounter referenceCounter);

Expand Down
2 changes: 1 addition & 1 deletion src/Neo/SmartContract/JsonSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public static byte[] SerializeToByteArray(StackItem item, uint maxSize)
/// <param name="engine">The <see cref="ApplicationEngine"/> used.</param>
/// <param name="json">The <see cref="JToken"/> to deserialize.</param>
/// <param name="limits">The limits for the deserialization.</param>
/// <param name="referenceCounter">The <see cref="ReferenceCounter"/> used by the <see cref="StackItem"/>.</param>
/// <param name="referenceCounter">The <see cref="IReferenceCounter"/> used by the <see cref="StackItem"/>.</param>
/// <returns>The deserialized <see cref="StackItem"/>.</returns>
public static StackItem Deserialize(ApplicationEngine engine, JToken json, ExecutionEngineLimits limits, IReferenceCounter referenceCounter = null)
{
Expand Down

0 comments on commit ca3e0cc

Please sign in to comment.