diff --git a/shadowsocks-csharp/Encryption/Stream/StreamTableNativeEncryptor.cs b/shadowsocks-csharp/Encryption/Stream/StreamTableNativeEncryptor.cs index 7f1a6494..697e0b17 100644 --- a/shadowsocks-csharp/Encryption/Stream/StreamTableNativeEncryptor.cs +++ b/shadowsocks-csharp/Encryption/Stream/StreamTableNativeEncryptor.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Runtime.CompilerServices; using System.Text; using System.Threading.Tasks; @@ -25,11 +26,13 @@ namespace Shadowsocks.Encryption.Stream { _encryptTable[i] = (byte)i; } + Span t = _encryptTable; // copy array 1024 times? excuse me? for (int i = 1; i < 1024; i++) { - _encryptTable = MergeSort(_encryptTable, a, i); + t = MergeSort(t, a, i); } + _encryptTable = t.ToArray(); for (int i = 0; i < 256; i++) { _decryptTable[_encryptTable[i]] = (byte)i; @@ -103,37 +106,32 @@ namespace Shadowsocks.Encryption.Stream #region Table private byte[] _encryptTable = new byte[256]; private byte[] _decryptTable = new byte[256]; + private byte[] _tmp = new byte[256]; + [MethodImpl(MethodImplOptions.AggressiveInlining)] private static long Compare(byte x, byte y, ulong a, int i) { return (long)(a % (ulong)(x + i)) - (long)(a % (ulong)(y + i)); } - private byte[] MergeSort(byte[] array, ulong a, int j) + byte[] buf = new byte[1024]; + private Span MergeSort(Span array, ulong a, int j) { if (array.Length == 1) { return array; } int middle = array.Length / 2; - byte[] left = new byte[middle]; - for (int i = 0; i < middle; i++) - { - left[i] = array[i]; - } - byte[] right = new byte[array.Length - middle]; - for (int i = 0; i < array.Length - middle; i++) - { - right[i] = array[i + middle]; - } - left = MergeSort(left, a, j); - right = MergeSort(right, a, j); + + Span left = MergeSort(array.Slice(0, middle), a, j);; + Span right = MergeSort(array.Slice(middle), a, j); int leftptr = 0; int rightptr = 0; // why a new array? - byte[] sorted = new byte[array.Length]; + Span sorted = new byte[array.Length];// buf.AsSpan().Slice(0,array.Length); // // _tmp; + for (int k = 0; k < array.Length; k++) { if (rightptr == right.Length || ((leftptr < left.Length) && (Compare(left[leftptr], right[rightptr], a, j) <= 0))) diff --git a/shadowsocks-csharp/shadowsocks-csharp.csproj b/shadowsocks-csharp/shadowsocks-csharp.csproj index 7387b4d0..700ed4f9 100644 --- a/shadowsocks-csharp/shadowsocks-csharp.csproj +++ b/shadowsocks-csharp/shadowsocks-csharp.csproj @@ -12,7 +12,7 @@ Shadowsocks shadowsocks.ico Shadowsocks.Program - enable + disable diff --git a/shadowsocks-windows.sln b/shadowsocks-windows.sln index 8ca60bd7..65f53fa7 100644 --- a/shadowsocks-windows.sln +++ b/shadowsocks-windows.sln @@ -26,27 +26,17 @@ EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU - Debug|x86 = Debug|x86 Release|Any CPU = Release|Any CPU - Release|x86 = Release|x86 EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Debug|Any CPU.Build.0 = Debug|Any CPU - {8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Debug|x86.ActiveCfg = Debug|Any CPU - {8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Debug|x86.Build.0 = Debug|Any CPU {8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Release|Any CPU.ActiveCfg = Release|Any CPU {8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Release|Any CPU.Build.0 = Release|Any CPU - {8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Release|x86.ActiveCfg = Release|Any CPU - {8C02D2F7-7CDB-4D55-9F25-CD03EF4AA062}.Release|x86.Build.0 = Release|Any CPU {45913187-0685-4903-B250-DCEF0479CD86}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {45913187-0685-4903-B250-DCEF0479CD86}.Debug|Any CPU.Build.0 = Debug|Any CPU - {45913187-0685-4903-B250-DCEF0479CD86}.Debug|x86.ActiveCfg = Debug|Any CPU - {45913187-0685-4903-B250-DCEF0479CD86}.Debug|x86.Build.0 = Debug|Any CPU {45913187-0685-4903-B250-DCEF0479CD86}.Release|Any CPU.ActiveCfg = Release|Any CPU {45913187-0685-4903-B250-DCEF0479CD86}.Release|Any CPU.Build.0 = Release|Any CPU - {45913187-0685-4903-B250-DCEF0479CD86}.Release|x86.ActiveCfg = Release|Any CPU - {45913187-0685-4903-B250-DCEF0479CD86}.Release|x86.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/test/CryptographyTest.cs b/test/CryptographyTest.cs index 599023d4..bfd2abda 100644 --- a/test/CryptographyTest.cs +++ b/test/CryptographyTest.cs @@ -155,10 +155,7 @@ namespace Shadowsocks.Test [TestMethod] public void TestNativeTableEncryption() { - // Too slow, run once to save CPU - var enc = new StreamTableNativeEncryptor("table", "barfoo!"); - var dec = new StreamTableNativeEncryptor("table", "barfoo!"); - RunEncryptionRound(enc, dec); + TestEncryptionMethod(typeof(StreamTableNativeEncryptor), "table"); } [TestMethod] public void TestStreamAesBouncyCastleEncryption()