Skip to content

Commit

Permalink
[Neo VM] optimize newstruct (#3525)
Browse files Browse the repository at this point in the history
* optimize newstruct

* use Array.Fill

* Update src/Neo.VM/JumpTable/JumpTable.Compound.cs

---------

Co-authored-by: Shargon <shargon@gmail.com>
  • Loading branch information
Jim8y and shargon authored Oct 11, 2024
1 parent 018e17b commit a326939
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/Neo.VM/JumpTable/JumpTable.Compound.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
using System.Linq;
using System.Numerics;
using System.Runtime.CompilerServices;
using Array = System.Array;
using VMArray = Neo.VM.Types.Array;

namespace Neo.VM
Expand Down Expand Up @@ -151,8 +152,9 @@ public virtual void NewArray(ExecutionEngine engine, Instruction instruction)
var n = (int)engine.Pop().GetInteger();
if (n < 0 || n > engine.Limits.MaxStackSize)
throw new InvalidOperationException($"MaxStackSize exceed: {n}");

engine.Push(new VMArray(engine.ReferenceCounter, Enumerable.Repeat(StackItem.Null, n)));
var nullArray = new StackItem[n];
Array.Fill(nullArray, StackItem.Null);
engine.Push(new VMArray(engine.ReferenceCounter, nullArray));
}

/// <summary>
Expand Down Expand Up @@ -180,8 +182,9 @@ public virtual void NewArray_T(ExecutionEngine engine, Instruction instruction)
(byte)StackItemType.ByteString => ByteString.Empty,
_ => StackItem.Null
};

engine.Push(new VMArray(engine.ReferenceCounter, Enumerable.Repeat(item, n)));
var itemArray = new StackItem[n];
Array.Fill(itemArray, item);
engine.Push(new VMArray(engine.ReferenceCounter, itemArray));
}

/// <summary>
Expand Down Expand Up @@ -210,10 +213,10 @@ public virtual void NewStruct(ExecutionEngine engine, Instruction instruction)
var n = (int)engine.Pop().GetInteger();
if (n < 0 || n > engine.Limits.MaxStackSize)
throw new InvalidOperationException($"MaxStackSize exceed: {n}");
Struct result = new(engine.ReferenceCounter);
for (var i = 0; i < n; i++)
result.Add(StackItem.Null);
engine.Push(result);

var nullArray = new StackItem[n];
Array.Fill(nullArray, StackItem.Null);
engine.Push(new Struct(engine.ReferenceCounter, nullArray));
}

/// <summary>
Expand Down

0 comments on commit a326939

Please sign in to comment.