Skip to content

Commit a326939

Browse files
Jimmyshargon
andauthored
[Neo VM] optimize newstruct (#3525)
* optimize newstruct * use Array.Fill * Update src/Neo.VM/JumpTable/JumpTable.Compound.cs --------- Co-authored-by: Shargon <[email protected]>
1 parent 018e17b commit a326939

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

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

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.Linq;
1515
using System.Numerics;
1616
using System.Runtime.CompilerServices;
17+
using Array = System.Array;
1718
using VMArray = Neo.VM.Types.Array;
1819

1920
namespace Neo.VM
@@ -151,8 +152,9 @@ public virtual void NewArray(ExecutionEngine engine, Instruction instruction)
151152
var n = (int)engine.Pop().GetInteger();
152153
if (n < 0 || n > engine.Limits.MaxStackSize)
153154
throw new InvalidOperationException($"MaxStackSize exceed: {n}");
154-
155-
engine.Push(new VMArray(engine.ReferenceCounter, Enumerable.Repeat(StackItem.Null, n)));
155+
var nullArray = new StackItem[n];
156+
Array.Fill(nullArray, StackItem.Null);
157+
engine.Push(new VMArray(engine.ReferenceCounter, nullArray));
156158
}
157159

158160
/// <summary>
@@ -180,8 +182,9 @@ public virtual void NewArray_T(ExecutionEngine engine, Instruction instruction)
180182
(byte)StackItemType.ByteString => ByteString.Empty,
181183
_ => StackItem.Null
182184
};
183-
184-
engine.Push(new VMArray(engine.ReferenceCounter, Enumerable.Repeat(item, n)));
185+
var itemArray = new StackItem[n];
186+
Array.Fill(itemArray, item);
187+
engine.Push(new VMArray(engine.ReferenceCounter, itemArray));
185188
}
186189

187190
/// <summary>
@@ -210,10 +213,10 @@ public virtual void NewStruct(ExecutionEngine engine, Instruction instruction)
210213
var n = (int)engine.Pop().GetInteger();
211214
if (n < 0 || n > engine.Limits.MaxStackSize)
212215
throw new InvalidOperationException($"MaxStackSize exceed: {n}");
213-
Struct result = new(engine.ReferenceCounter);
214-
for (var i = 0; i < n; i++)
215-
result.Add(StackItem.Null);
216-
engine.Push(result);
216+
217+
var nullArray = new StackItem[n];
218+
Array.Fill(nullArray, StackItem.Null);
219+
engine.Push(new Struct(engine.ReferenceCounter, nullArray));
217220
}
218221

219222
/// <summary>

0 commit comments

Comments
 (0)