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

ReferenceCounterV2 #3526

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open

ReferenceCounterV2 #3526

wants to merge 23 commits into from

Conversation

shargon
Copy link
Member

@shargon shargon commented Oct 11, 2024

Description

Close #3517

Type of change

  • Optimization (the change is only an optimization)
  • Style (the change is only a code style for better maintenance or standard purpose)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

  • UT_ReferenceCounterV2

Test Configuration:

Checklist:

  • My code follows the style guidelines of this project
  • I have performed a self-review of my code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published in downstream modules

@shargon shargon requested a review from Jim8y October 11, 2024 07:43
@shargon shargon changed the title ReferenceCounterV2 ReferenceCounterV2 Oct 11, 2024
@shargon shargon marked this pull request as ready for review October 11, 2024 07:53
@Jim8y
Copy link
Contributor

Jim8y commented Oct 11, 2024

its not yet being used right, no wander the bench has no change.

@shargon
Copy link
Member Author

shargon commented Oct 11, 2024

its not yet being used right, no wander the bench has no change.

No, if you like it, i can start with the selection according to the height

@Jim8y
Copy link
Contributor

Jim8y commented Oct 11, 2024

its not yet being used right, no wander the bench has no change.

No, if you like it, i can start with the selection according to the height

I could not really understand the reason of this pr, but after i replace RC with this version, optimization for some pocs are obvious, but for the rest is not that clear:

V1

// | Method | Mean | Error | StdDev | Median |
// |----------------------- |----------------:|---------------:|---------------:|----------------:|
// | NeoIssue2528 | 672,995.40 us | 13,240.184 us | 21,754.002 us | 666,763.00 us |
// | NeoVMIssue418 | 574.74 us | 11.179 us | 11.480 us | 572.26 us |
// | NeoIssue2723 | 68.45 us | 0.968 us | 0.906 us | 68.21 us |
// | PoC_NewBuffer | 68.04 us | 1.078 us | 1.008 us | 67.75 us |
// | PoC_Cat | 68.29 us | 1.143 us | 1.069 us | 67.82 us |
// | PoC_Left | 65.79 us | 0.678 us | 0.634 us | 65.78 us |
// | PoC_Right | 68.19 us | 0.777 us | 0.727 us | 67.88 us |
// | PoC_ReverseN | 8,317,148.36 us | 26,176.966 us | 20,437.254 us | 8,311,623.35 us |
// | PoC_Substr | 67.13 us | 0.641 us | 0.600 us | 67.05 us |
// | PoC_NewArray | 4,016,079.05 us | 48,575.705 us | 45,437.745 us | 4,007,341.40 us |
// | PoC_NewStruct | 4,713,414.06 us | 41,234.516 us | 34,432.688 us | 4,706,219.60 us |
// | PoC_Roll | 1,248,732.44 us | 13,837.434 us | 12,943.545 us | 1,246,873.10 us |
// | PoC_XDrop | 2,367,151.97 us | 131,814.326 us | 388,657.514 us | 2,503,579.05 us |
// | PoC_MemCpy | 106.51 us | 6.273 us | 18.495 us | 113.14 us |
// | PoC_Unpack | 677,618.41 us | 41,248.471 us | 121,622.047 us | 727,964.25 us |
// | PoC_GetScriptContainer | 64.84 us | 1.055 us | 0.987 us | 65.01 us |

V2

// | Method | Mean | Error | StdDev |
// |----------------------- |----------------:|--------------:|--------------:|
// | NeoIssue2528 | 8,057.26 us | 115.627 us | 108.158 us |
// | NeoVMIssue418 | 288.69 us | 3.035 us | 2.839 us |
// | NeoIssue2723 | 69.08 us | 0.629 us | 0.588 us |
// | PoC_NewBuffer | 67.75 us | 0.873 us | 0.816 us |
// | PoC_Cat | 67.80 us | 0.779 us | 0.729 us |
// | PoC_Left | 68.20 us | 0.688 us | 0.644 us |
// | PoC_Right | 66.33 us | 0.684 us | 0.640 us |
// | PoC_ReverseN | 8,247,475.45 us | 23,815.937 us | 22,277.442 us |
// | PoC_Substr | 67.38 us | 0.792 us | 0.703 us |
// | PoC_NewArray | 79.21 us | 0.990 us | 0.926 us |
// | PoC_NewStruct | 85.20 us | 1.249 us | 1.169 us |
// | PoC_Roll | 1,188,522.22 us | 7,654.767 us | 6,785.751 us |
// | PoC_XDrop | 1,185,929.37 us | 16,518.830 us | 15,451.724 us |
// | PoC_MemCpy | 67.03 us | 1.163 us | 1.088 us |
// | PoC_Unpack | 216,052.64 us | 3,346.821 us | 3,130.618 us |
// | PoC_GetScriptContainer | 66.20 us | 0.675 us | 0.632 us |

@shargon
Copy link
Member Author

shargon commented Oct 11, 2024

Is not possible to be slower than the previous case in any of them xD

/// <param name="count">Number of similar entries</param>
public void AddStackReference(StackItem item, int count = 1)
{
Count += count;
Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

throw new InvalidOperationException("Reference was not added before");
}

Count--;
Copy link
Contributor

Choose a reason for hiding this comment

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

Hi, what if you are removing a compound type? for instance, you have an array with 2047 sub items, then you remove the array, the rc will only remove one reference, and the vm will still be full.

Copy link
Contributor

Choose a reason for hiding this comment

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

        [TestMethod]
        public void TestNewArrayThenDrop()
        {
            using ScriptBuilder sb = new();
            sb.EmitPush(2040);
            sb.Emit(OpCode.NEWARRAY);
            sb.Emit(OpCode.DROP);
            using ExecutionEngine engine = new();
            engine.LoadScript(sb.ToArray());
            Assert.AreEqual(0, engine.ReferenceCounter.Count);
            Assert.AreEqual(VMState.HALT, engine.Execute());
            Assert.AreEqual(0, engine.ReferenceCounter.Count);
        }

Assert.AreEqual failed. Expected:<0>. Actual:<2040>.
at Neo.Test.UT_ReferenceCounter.TestNewArrayThenDrop() in /Users/jinghuiliao/git/neo/tests/Neo.VM.Tests/UT_ReferenceCounter.cs:line 257
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

op:[0000]PUSHINT16 []
op:[0003]NEWARRAY [Integer(2040)]
op:[0004]DROP [Array(2040)]
op:[0005] []

Copy link
Contributor

Choose a reason for hiding this comment

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

        [TestMethod]
        public void TestNewArrayThenDrop()
        {
            using ScriptBuilder sb = new();
            sb.EmitPush(2040);
            sb.Emit(OpCode.NEWARRAY);
            sb.Emit(OpCode.DROP);
            sb.EmitPush(10);
            sb.Emit(OpCode.NEWARRAY);
            using ExecutionEngine engine = new();
            engine.LoadScript(sb.ToArray());
            Assert.AreEqual(0, engine.ReferenceCounter.Count);
            var res = engine.Execute();
            Assert.AreEqual(0, engine.ReferenceCounter.Count);
            Assert.AreEqual(VMState.HALT, res);
        }

Assert.AreEqual failed. Expected:<0>. Actual:<2051>.
at Neo.Test.UT_ReferenceCounter.TestNewArrayThenDrop() in /Users/jinghuiliao/git/neo/tests/Neo.VM.Tests/UT_ReferenceCounter.cs:line 259
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

op:[0000]PUSHINT16 []
op:[0003]NEWARRAY [Integer(2040)]
op:[0004]DROP [Array(2040)]
op:[0005]PUSH10 []
op:[0006]NEWARRAY [Integer(10)]

Copy link
Member Author

Choose a reason for hiding this comment

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

Hi, what if you are removing a compound type? for instance, you have an array with 2047 sub items, then you remove the array, the rc will only remove one reference, and the vm will still be full.

yes, we need to propagate this change

Copy link
Member Author

Choose a reason for hiding this comment

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

Could you add these UT?

Copy link
Member Author

@shargon shargon Nov 3, 2024

Choose a reason for hiding this comment

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

Assert.AreEqual(0, engine.ReferenceCounter.Count);
Assert.AreEqual(VMState.HALT, res);

it's 11 in the previous version

Copy link
Member Author

Choose a reason for hiding this comment

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

        [TestMethod]
        public void TestNewArrayThenDrop()
        {
            using ScriptBuilder sb = new();
            sb.EmitPush(2040);
            sb.Emit(OpCode.NEWARRAY);
            sb.Emit(OpCode.DROP);
            using ExecutionEngine engine = new();
            engine.LoadScript(sb.ToArray());
            Assert.AreEqual(0, engine.ReferenceCounter.Count);
            Assert.AreEqual(VMState.HALT, engine.Execute());
            Assert.AreEqual(0, engine.ReferenceCounter.Count);
        }

Assert.AreEqual failed. Expected:<0>. Actual:<2040>. at Neo.Test.UT_ReferenceCounter.TestNewArrayThenDrop() in /Users/jinghuiliao/git/neo/tests/Neo.VM.Tests/UT_ReferenceCounter.cs:line 257 at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

op:[0000]PUSHINT16 [] op:[0003]NEWARRAY [Integer(2040)] op:[0004]DROP [Array(2040)] op:[0005] []

this use the previous version also

@vncoelho
Copy link
Member

vncoelho commented Nov 3, 2024

@shargon, as far as I understood in our last call, there will be nothing like Dictionary<StackItem, int> to track items and avoid they being added twice, right? Neither for checking if removed item as really added.

@shargon
Copy link
Member Author

shargon commented Nov 3, 2024

as far as I understood in our last call, there will be nothing like Dictionary<StackItem, int> to track items and avoid they being added twice, right? Neither for checking if removed item as really added.

no, twice is two now, faster, but same reference is two items

@Jim8y
Copy link
Contributor

Jim8y commented Nov 4, 2024

        [TestMethod]
        public void TestMultiNewArrayThenDrop()
        {
            using ScriptBuilder sb = new();
            sb.EmitPush(1000);
            sb.Emit(OpCode.NEWARRAY);
            sb.Emit(OpCode.DROP);
            sb.EmitPush(1000);
            sb.Emit(OpCode.NEWARRAY);
            sb.Emit(OpCode.DROP);
            sb.EmitPush(1000);
            sb.Emit(OpCode.NEWARRAY);
            sb.Emit(OpCode.DROP);
            using ExecutionEngine engine = new();
            engine.LoadScript(sb.ToArray());
            Assert.AreEqual(0, engine.ReferenceCounter.Count);
            var res = engine.Execute();
            Assert.AreEqual(0, engine.ReferenceCounter.Count);
            Assert.AreEqual(VMState.HALT, res);
        }

Assert.AreEqual failed. Expected:<0>. Actual:<3001>.
at Neo.Test.UT_ReferenceCounter.TestMultiNewArrayThenDrop() in C:\Users\liaoj\git\neo\tests\Neo.VM.Tests\UT_ReferenceCounter.cs:line 317
at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor)
at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

Your code will keep growing the RC for compound type, it never clears it. This will make contract impossible to use compoundtype, since there is < 2048 subitems in total can be ever generated during the whole execution.

@shargon
Copy link
Member Author

shargon commented Nov 4, 2024

        [TestMethod]
        public void TestMultiNewArrayThenDrop()
        {
            using ScriptBuilder sb = new();
            sb.EmitPush(1000);
            sb.Emit(OpCode.NEWARRAY);
            sb.Emit(OpCode.DROP);
            sb.EmitPush(1000);
            sb.Emit(OpCode.NEWARRAY);
            sb.Emit(OpCode.DROP);
            sb.EmitPush(1000);
            sb.Emit(OpCode.NEWARRAY);
            sb.Emit(OpCode.DROP);
            using ExecutionEngine engine = new();
            engine.LoadScript(sb.ToArray());
            Assert.AreEqual(0, engine.ReferenceCounter.Count);
            var res = engine.Execute();
            Assert.AreEqual(0, engine.ReferenceCounter.Count);
            Assert.AreEqual(VMState.HALT, res);
        }

Assert.AreEqual failed. Expected:<0>. Actual:<3001>. at Neo.Test.UT_ReferenceCounter.TestMultiNewArrayThenDrop() in C:\Users\liaoj\git\neo\tests\Neo.VM.Tests\UT_ReferenceCounter.cs:line 317 at System.RuntimeMethodHandle.InvokeMethod(Object target, Void** arguments, Signature sig, Boolean isConstructor) at System.Reflection.MethodBaseInvoker.InvokeWithNoArgs(Object obj, BindingFlags invokeAttr)

Your code will keep growing the RC for compound type, it never clears it. This will make contract impossible to use compoundtype, since there is < 2048 subitems in total can be ever generated during the whole execution.

Waiting for #3549

@shargon shargon added the Blocked This issue can't be worked at the moment label Nov 4, 2024
@Jim8y
Copy link
Contributor

Jim8y commented Nov 7, 2024

@shargon please follow up with this work.

@Jim8y Jim8y added Need Update and removed Blocked This issue can't be worked at the moment labels Nov 7, 2024
{
if (!_stack.Remove(item))
{
throw new InvalidOperationException("Reference was not added before");
Copy link
Member Author

Choose a reason for hiding this comment

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

This version is not compatible with circular references, but at the same it works like a protection

@Jim8y
Copy link
Contributor

Jim8y commented Nov 8, 2024

@shargon test fails

cschuchardt88
cschuchardt88 previously approved these changes Nov 8, 2024
@shargon
Copy link
Member Author

shargon commented Nov 8, 2024

@shargon test fails

Yes, i know, is because we don't allow circulsr reference with this reference counter, i will fix it if the logic seems good to you

@shargon
Copy link
Member Author

shargon commented Nov 8, 2024

Wait I will fix somethings

@shargon
Copy link
Member Author

shargon commented Nov 8, 2024

Still having some issues with some UTs

| Method | Mean     | Error   | StdDev  |
|------- |---------:|--------:|--------:|
| V2     | 209.0 ms | 3.68 ms | 3.26 ms |
| V1     | 232.1 ms | 4.54 ms | 8.75 ms |

shargon and others added 4 commits November 8, 2024 13:11
|------- |---------:|--------:|--------:|
| V2     | 216.6 ms | 1.28 ms | 1.14 ms |
| V1     | 235.8 ms | 3.18 ms | 2.48 ms |
/// <param name="count">Number of similar entries</param>
public void AddStackReference(StackItem item, int count = 1)
{
for (var x = 0; x < count; x++)
Copy link
Contributor

Choose a reason for hiding this comment

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

this actually is not necessary, currently we process his when we create the compound type. For instance, new array will pass the rc and add all its subitems.

Copy link
Member Author

Choose a reason for hiding this comment

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

I know it, but it's hard to explain, there are cases where the items are removed without exists.

  • create a map with items without push the map to stack (unit test)
  • Push the map to stack
  • Add the map to static variables
  • RET

maybe is not this case that i remember, but when the stack was clean, I received "item not found" error

}
}

private int ReferenceEqualsIndexOf(StackItem item, int index = 0)
Copy link
Contributor

Choose a reason for hiding this comment

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

It's complexity is O(n), may not fast in some cases.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, this version is slower than I expected, I tried with different ways, and still slower than expected

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[VM] Remove or Update Tarjan in the virtual machine for better performance.
7 participants