-
Notifications
You must be signed in to change notification settings - Fork 1k
[Neo Core Bug] fix compound type reference issue #3334
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
Merged
Merged
Changes from 12 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
3aeaedc
fix compound type reference issue
Jim8y 1fc70f3
fix warning
Jim8y fb09d9a
Merge branch 'master' into fix-compound-reference
2a2aaeb
Merge branch 'master' into fix-compound-reference
d59521c
add benchmark
Jim8y a28ec0b
Merge branch 'master' into fix-compound-reference
Jim8y 872baab
Merge branch 'master' into fix-compound-reference
62ec6e0
throw exception instead
d748d83
Merge branch 'master' into fix-compound-reference
c2a0a59
Update benchmarks/Neo.VM.Benchmarks/Benchmarks.Types.cs
shargon 481293c
Merge branch 'master' into fix-compound-reference
7e6a952
Update src/Neo.VM/Types/Map.cs
shargon 4c03b33
Apply suggestions from code review
shargon 774923f
Update src/Neo.VM/Types/Map.cs
38c3c16
Merge branch 'master' into fix-compound-reference
Jim8y 149d53d
update accessibality.
Jim8y 815bfe4
Merge branch 'master' into fix-compound-reference
NGDAdmin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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) | ||
| { | ||
| 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) | ||
| { | ||
| 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); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.