Skip to content

Commit 64b67f0

Browse files
committed
In JavaScriptEngineSwitcher.ChakraCore improved a performance of the UnicodeToAnsi method of EncodingHelpers class
1 parent d0c6836 commit 64b67f0

File tree

3 files changed

+35
-9
lines changed

3 files changed

+35
-9
lines changed

src/JavaScriptEngineSwitcher.ChakraCore/Helpers/EncodingHelpers.cs

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,11 @@ public static string UnicodeToAnsi(string value, out int byteCount)
2525
string result;
2626
int valueLength = value.Length;
2727
Encoding utf8Encoding = Encoding.UTF8;
28+
#if NETFULL
29+
Encoding ansiEncoding = Encoding.Default;
30+
#else
2831
Encoding ansiEncoding = Encoding.GetEncoding(0);
32+
#endif
2933

3034
var byteArrayPool = ArrayPool<byte>.Shared;
3135
int bufferLength = utf8Encoding.GetByteCount(value);
@@ -34,7 +38,7 @@ public static string UnicodeToAnsi(string value, out int byteCount)
3438

3539
try
3640
{
37-
#if NET471 || NETSTANDARD || NETCOREAPP2_1
41+
#if NET45 || NET471 || NETSTANDARD || NETCOREAPP2_1
3842
result = ConvertStringInternal(utf8Encoding, ansiEncoding, value, valueLength, buffer, bufferLength);
3943
#else
4044
utf8Encoding.GetBytes(value, 0, valueLength, buffer, 0);
@@ -50,7 +54,7 @@ public static string UnicodeToAnsi(string value, out int byteCount)
5054

5155
return result;
5256
}
53-
#if NET471 || NETSTANDARD || NETCOREAPP2_1
57+
#if NET45 || NET471 || NETSTANDARD || NETCOREAPP2_1
5458

5559
private static unsafe string ConvertStringInternal(Encoding srcEncoding, Encoding dstEncoding, string s,
5660
int charCount, byte[] bytes, int byteCount)
@@ -59,10 +63,37 @@ private static unsafe string ConvertStringInternal(Encoding srcEncoding, Encodin
5963
fixed (byte* pBytes = bytes)
6064
{
6165
srcEncoding.GetBytes(pString, charCount, pBytes, byteCount);
66+
#if NET471 || NETSTANDARD || NETCOREAPP2_1
6267
string result = dstEncoding.GetString(pBytes, byteCount);
6368

6469
return result;
6570
}
71+
#else
72+
}
73+
74+
int resultLength = dstEncoding.GetCharCount(bytes, 0, byteCount);
75+
var charArrayPool = ArrayPool<char>.Shared;
76+
char[] resultChars = charArrayPool.Rent(resultLength + 1);
77+
resultChars[resultLength] = '\0';
78+
79+
string result;
80+
81+
try
82+
{
83+
fixed (byte* pBytes = bytes)
84+
fixed (char* pResultChars = resultChars)
85+
{
86+
dstEncoding.GetChars(pBytes, byteCount, pResultChars, resultLength);
87+
result = new string(pResultChars, 0, resultLength);
88+
}
89+
}
90+
finally
91+
{
92+
charArrayPool.Return(resultChars);
93+
}
94+
95+
return result;
96+
#endif
6697
}
6798
#endif
6899
}

src/JavaScriptEngineSwitcher.ChakraCore/JavaScriptEngineSwitcher.ChakraCore.csproj

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ This package does not contain the native implementations of ChakraCore. Therefor
2222
* JavaScriptEngineSwitcher.ChakraCore.Native.osx-x64</Description>
2323
<PackageIconUrl>https://raw.githubusercontent.com/Taritsyn/JavaScriptEngineSwitcher/master/Icons/JavaScriptEngineSwitcher_ChakraCore_Logo128x128.png</PackageIconUrl>
2424
<PackageTags>JavaScriptEngineSwitcher;JavaScript;ECMAScript;ChakraCore</PackageTags>
25-
<PackageReleaseNotes>1. Reduced a memory consumption in cases, where not used the embedding of objects and types;
26-
2. Fixed a wrong implementation of destruction of the embedded delegates;
27-
3. Accelerated a conversion of script types to host types.</PackageReleaseNotes>
25+
<PackageReleaseNotes>Improved a performance of the `UnicodeToAnsi` method of `EncodingHelpers` class.</PackageReleaseNotes>
2826
</PropertyGroup>
2927

3028
<Import Project="../../build/common.props" />

src/JavaScriptEngineSwitcher.ChakraCore/readme.txt

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,7 @@
3030
=============
3131
RELEASE NOTES
3232
=============
33-
1. Reduced a memory consumption in cases, where not used the embedding of objects
34-
and types;
35-
2. Fixed a wrong implementation of destruction of the embedded delegates;
36-
3. Accelerated a conversion of script types to host types.
33+
Improved a performance of the `UnicodeToAnsi` method of `EncodingHelpers` class.
3734

3835
=============
3936
DOCUMENTATION

0 commit comments

Comments
 (0)