Skip to content
This repository has been archived by the owner on Nov 22, 2023. It is now read-only.

Improve the performance of NeoVM #95

Merged
merged 8 commits into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
352 changes: 302 additions & 50 deletions src/neo-vm/ExecutionEngine.cs

Large diffs are not rendered by default.

8 changes: 3 additions & 5 deletions src/neo-vm/HashComparer.cs
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Collections.Generic;

namespace Neo.VM
{
internal class HashComparer : IEqualityComparer<byte[]>
{
public bool Equals(byte[] x, byte[] y)
{
return x.SequenceEqual(y);
return Unsafe.Equals(x, y);
}

public int GetHashCode(byte[] obj)
{
return BitConverter.ToInt32(obj, 0);
return Unsafe.ToInt32(obj, 0);
}
}
}
5 changes: 5 additions & 0 deletions src/neo-vm/StackItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public virtual BigInteger GetBigInteger()

public abstract byte[] GetByteArray();

public virtual int GetByteLength()
{
return GetByteArray().Length;
}

public override int GetHashCode()
{
unchecked
Expand Down
3 changes: 1 addition & 2 deletions src/neo-vm/Types/Boolean.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Numerics;

namespace Neo.VM.Types
Expand Down Expand Up @@ -30,7 +29,7 @@ public override bool Equals(StackItem other)
{
return false;
}
return GetByteArray().SequenceEqual(bytes_other);
return Unsafe.Equals(GetByteArray(), bytes_other);
}

public override BigInteger GetBigInteger()
Expand Down
5 changes: 2 additions & 3 deletions src/neo-vm/Types/ByteArray.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;

namespace Neo.VM.Types
{
Expand All @@ -25,14 +24,14 @@ public override bool Equals(StackItem other)
{
return false;
}
return value.SequenceEqual(bytes_other);
return Unsafe.Equals(value, bytes_other);
}

public override bool GetBoolean()
{
if (value.Length > ExecutionEngine.MaxSizeForBigInteger)
return true;
return value.Any(p => p != 0);
return Unsafe.NotZero(value);
}

public override byte[] GetByteArray()
Expand Down
11 changes: 9 additions & 2 deletions src/neo-vm/Types/Integer.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Numerics;

namespace Neo.VM.Types
Expand Down Expand Up @@ -27,7 +26,7 @@ public override bool Equals(StackItem other)
{
return false;
}
return GetByteArray().SequenceEqual(bytes_other);
return Unsafe.Equals(GetByteArray(), bytes_other);
}

public override BigInteger GetBigInteger()
Expand All @@ -44,5 +43,13 @@ public override byte[] GetByteArray()
{
return value.ToByteArray();
}

private int _length = -1;
public override int GetByteLength()
{
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
if (_length == -1)
_length = value.ToByteArray().Length;
return _length;
}
}
}
84 changes: 84 additions & 0 deletions src/neo-vm/Unsafe.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
using System;
using System.Runtime.CompilerServices;

namespace Neo.VM
{
internal static class Unsafe
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe public static bool Equals(byte[] x, byte[] y)
{
if (ReferenceEquals(x, y)) return true;
if (x is null || y is null) return false;
int len = x.Length;
if (len != y.Length) return false;
fixed (byte* xp = x, yp = y)
{
long* xlp = (long*)xp, ylp = (long*)yp;
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
for (; len >= 8; len -= 8)
{
if (*xlp != *ylp) return false;
xlp++;
ylp++;
}
byte* xbp = (byte*)xlp, ybp = (byte*)ylp;
for (; len > 0; len--)
{
if (*xbp != *ybp) return false;
xbp++;
ybp++;
}
}
return true;
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe public static void MemoryCopy(byte[] src, int srcOffset, byte[] dst, int dstOffset, int count)
{
fixed (byte* sp = &src[srcOffset], dp = &dst[dstOffset])
{
Buffer.MemoryCopy(sp, dp, dst.Length - dstOffset, count);
}
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe public static bool NotZero(byte[] x)
{
if (x is null)
throw new ArgumentNullException(nameof(x));
int len = x.Length;
if (len == 0) return false;
fixed (byte* xp = x)
{
long* xlp = (long*)xp;
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
for (; len >= 8; len -= 8)
{
if (*xlp != 0) return true;
xlp++;
}
byte* xbp = (byte*)xlp;
for (; len > 0; len--)
{
if (*xbp != 0) return true;
xbp++;
}
}
return false;
}

/// <summary>
/// Convert byte array to int32
/// </summary>
/// <param name="value">Value (must be checked before this call)</param>
/// <param name="startIndex">Start index</param>
/// <returns>Integer</returns>
[MethodImpl(MethodImplOptions.AggressiveInlining)]
unsafe public static int ToInt32(byte[] value, int startIndex)
{
fixed (byte* pbyte = &value[startIndex])
erikzhang marked this conversation as resolved.
Show resolved Hide resolved
{
return *(int*)pbyte;
}
}
}
}
9 changes: 2 additions & 7 deletions src/neo-vm/neo-vm.csproj
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Copyright>2016-2017 The Neo Project</Copyright>
<Copyright>2016-2019 The Neo Project</Copyright>
<AssemblyTitle>Neo.VM</AssemblyTitle>
<Description>Neo.VM</Description>
<Version>2.4.0</Version>
Expand All @@ -15,12 +15,7 @@
<RepositoryUrl>https://github.com/neo-project/neo-vm.git</RepositoryUrl>
<NetStandardImplicitPackageVersion Condition=" '$(TargetFramework)' == 'netstandard1.6' ">1.6.0</NetStandardImplicitPackageVersion>
<RootNamespace>Neo.VM</RootNamespace>
</PropertyGroup>

<PropertyGroup Condition="'$(Configuration)|$(TargetFramework)|$(Platform)'=='Release|netstandard1.6|AnyCPU'">
<DefineConstants>RELEASE;NETSTANDARD1_6</DefineConstants>
<DebugType>none</DebugType>
<DebugSymbols>False</DebugSymbols>
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup Condition=" '$(TargetFramework)' == 'net461' ">
Expand Down
185 changes: 185 additions & 0 deletions tests/neo-vm.Tests/Tests/OpCodes/Numeric/SHL.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
{
"category": "Numeric",
"name": "SHL",
"tests":
[
{
"name": "Exception - Above the limit 0 << 257",
"script": "0x0002010198",
"steps":
[
{
"actions":
[
"StepInto",
"StepInto"
],
"result":
{
"state": "Break",
"invocationStack":
[
{
"scriptHash": "0x2C9BA6F08FFCCB999A3FB8636A485A0D12BD2E54",
"instructionPointer": 4,
"nextInstruction": "SHL",
"evaluationStack":
[
{
"type": "ByteArray",
"value": "0x0101"
},
{
"type": "ByteArray",
"value": ""
}
]
}
]
}
},
{
"actions":
[
"StepInto"
],
"result":
{
"state": "Fault",
"invocationStack":
[
{
"scriptHash": "0x2C9BA6F08FFCCB999A3FB8636A485A0D12BD2E54",
"instructionPointer": 5,
"nextInstruction": "RET",
"evaluationStack":
[
{
"type": "ByteArray",
"value": ""
}
]
}
]
}
}
]
},
{
"name": "Exception - Below the limit 0 << -257",
"script": "0x0002FFFE98",
"steps":
[
{
"actions":
[
"StepInto",
"StepInto"
],
"result":
{
"state": "Break",
"invocationStack":
[
{
"scriptHash": "0x29351D375C5FEB0E1ED756CDEF8AB48260E8FCA9",
"instructionPointer": 4,
"nextInstruction": "SHL",
"evaluationStack":
[
{
"type": "ByteArray",
"value": "0xFFFE"
},
{
"type": "ByteArray",
"value": ""
}
]
}
]
}
},
{
"actions":
[
"StepInto"
],
"result":
{
"state": "Fault",
"invocationStack":
[
{
"scriptHash": "0x29351D375C5FEB0E1ED756CDEF8AB48260E8FCA9",
"instructionPointer": 5,
"nextInstruction": "RET",
"evaluationStack":
[
{
"type": "ByteArray",
"value": ""
}
]
}
]
}
}
]
},
{
"name": "Real test 0 << 256",
"script": "0x0002000198",
"steps":
[
{
"actions":
[
"StepInto",
"StepInto"
],
"result":
{
"state": "Break",
"invocationStack":
[
{
"scriptHash": "0x47DA2CD2329F2C882A4DB611D048D53F2CB07085",
"instructionPointer": 4,
"nextInstruction": "SHL",
"evaluationStack":
[
{
"type": "ByteArray",
"value": "0x0001"
},
{
"type": "ByteArray",
"value": ""
}
]
}
]
}
},
{
"actions":
[
"Execute"
],
"result":
{
"state": "Halt",
"resultStack":
[
{
"type": "Integer",
"value": 0
}
]
}
}
]
}
]
}
Loading