Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
5d09005
Speed up text encoding
Daniel-Svensson Jul 7, 2022
63c760c
Update implementation
Daniel-Svensson Jul 18, 2022
196ce48
Add tests for binary xml strings
Daniel-Svensson Jul 26, 2022
65e7029
Merge branch 'binary_xml_text' of https://github.com/Daniel-Svensson/…
Daniel-Svensson Jul 26, 2022
4d8078a
limit counting code to 256 bit vectors
Daniel-Svensson Jul 26, 2022
6e5aabb
reword comment
Daniel-Svensson Aug 3, 2022
70fa189
rename test
Daniel-Svensson Aug 3, 2022
b34d259
move bytesmax
Daniel-Svensson Aug 3, 2022
5df5ae0
Fix bytesMax after moving variable initialization
Daniel-Svensson Aug 4, 2022
a790fbb
use unicode escape value in test
Daniel-Svensson Aug 4, 2022
2b82ac8
fix test typo "*" -> "+"
Daniel-Svensson Aug 4, 2022
301e531
Update src/libraries/System.Private.DataContractSerialization/src/Sys…
Daniel-Svensson Aug 12, 2022
5a21306
Remvoe vectorized code from UnsafeGetUTF8Length
Daniel-Svensson Aug 12, 2022
8a3de26
Merge remote-tracking branch 'upstream/main' into binary_xml_text
Daniel-Svensson Aug 13, 2022
048cade
Fix overfload
Daniel-Svensson Sep 8, 2022
8297311
Merge commit '080f708e7018f6c0529b6c875a44d84fc4d74419' into binary_x…
Daniel-Svensson Oct 24, 2022
287e737
use for loop which seems faster
Daniel-Svensson Oct 24, 2022
0d2a9bb
merge up to net8 preview1
Daniel-Svensson Mar 3, 2023
ab29682
remove vector loop
Daniel-Svensson Mar 6, 2023
251391f
make sealed encoding to allow devirtualisation
Daniel-Svensson Mar 11, 2023
a590739
back some changes
Daniel-Svensson Mar 20, 2023
46b6314
use uint for UnsafeGetUTF8Chars comparison
Daniel-Svensson Mar 25, 2023
82f8880
revert more changes
Daniel-Svensson Mar 26, 2023
d78aade
Fix cutoff based on new measurements
Daniel-Svensson Mar 26, 2023
3b20be8
use BinaryPrimitives.ReverseEndianness as suggested
Daniel-Svensson Mar 26, 2023
9c86b05
Update cutoff from 24 to 32 chars before calling, due to regression f…
Daniel-Svensson Mar 27, 2023
ccfb008
Remove sealed encoding since it only improves XmlConvert
Daniel-Svensson Apr 2, 2023
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
Next Next commit
Speed up text encoding
  • Loading branch information
Daniel-Svensson committed Jul 7, 2022
commit 5d09005ecc396afb5eaedf78c7787654e4e7f0cf
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@
<Reference Include="System.Reflection.Emit.Lightweight" />
<Reference Include="System.Reflection.Primitives" />
<Reference Include="System.Runtime" />
<Reference Include="System.Runtime.Intrinsics" />
<Reference Include="System.Runtime.Serialization.Formatters" />
<Reference Include="System.Runtime.Serialization.Primitives" />
<Reference Include="System.Text.Encoding.Extensions" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

using System.IO;
using System.Text;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using System.Runtime.Serialization;
using System.Threading.Tasks;

Expand All @@ -16,6 +18,8 @@ internal abstract class XmlStreamNodeWriter : XmlNodeWriter
private bool _ownsStream;
private const int bufferLength = 512;
private const int maxBytesPerChar = 3;
private const int CharsPerLong = 4;
private const ulong LongNonAsciiMask = 0xff80ff80ff80ff80;
private Encoding? _encoding;
private static readonly UTF8Encoding s_UTF8Encoding = new UTF8Encoding(false, true);

Expand Down Expand Up @@ -56,18 +60,6 @@ public int Position
}
}

private int GetByteCount(char[] chars)
{
if (_encoding == null)
{
return s_UTF8Encoding.GetByteCount(chars);
}
else
{
return _encoding.GetByteCount(chars);
}
}

protected byte[] GetBuffer(int count, out int offset)
{
DiagnosticUtility.DebugAssert(count >= 0 && count <= bufferLength, "");
Expand Down Expand Up @@ -344,37 +336,75 @@ protected unsafe void UnsafeWriteUnicodeChars(char* chars, int charCount)

protected unsafe int UnsafeGetUnicodeChars(char* chars, int charCount, byte[] buffer, int offset)
{
char* charsMax = chars + charCount;
while (chars < charsMax)
if (BitConverter.IsLittleEndian)
{
char value = *chars++;
buffer[offset++] = (byte)value;
value >>= 8;
buffer[offset++] = (byte)value;
new ReadOnlySpan<byte>((byte*)chars, 2 * charCount)
.CopyTo(buffer.AsSpan(offset));
}
else
{
char* charsMax = chars + charCount;
while (chars < charsMax)
{
char value = *chars++;
buffer[offset++] = (byte)value;
buffer[offset++] = (byte)(value >> 8);
}
}

return charCount * 2;
}

protected unsafe int UnsafeGetUTF8Length(char* chars, int charCount)
{
char* charsMax = chars + charCount;
while (chars < charsMax)

// This method is only called from 2 places and will use length of at least (128/3 and 256/3) respectivly
// AVX is faster for at least 2048 chars, probably more
// for other cases the encoding path is better optimized than any fast path done here.
if (Avx.IsSupported)
{
if (*chars >= 0x80)
break;
char* simdMax = charsMax - (Vector256<ushort>.Count - 1);
char* longMax = charsMax - (CharsPerLong - 1);

chars++;
}
var mask = Vector256.Create((ushort)0xff80);
while (chars < simdMax)
{
var l = Vector256.Load((ushort*)chars);
if (!Avx.TestZ(l, mask))
{
if (Sse41.TestZ(l.GetLower(), mask.GetLower()))
chars += Vector128<ushort>.Count;
goto NonAscii;
}

if (chars == charsMax)
return charCount;
chars += Vector256<ushort>.Count;
}

char[] chArray = new char[charsMax - chars];
for (int i = 0; i < chArray.Length; i++)
{
chArray[i] = chars[i];
while (chars < longMax)
{
if ((*(ulong*)chars & LongNonAsciiMask) != 0)
goto NonAscii;

chars += CharsPerLong;
}

while (chars < charsMax)
{
if (*chars >= 0x80)
goto NonAscii;

chars++;
}

return charCount;
}
return (int)(chars - (charsMax - charCount)) + GetByteCount(chArray);

NonAscii:
int numRemaining = (int)(charsMax - chars);
int numAscii = charCount - numRemaining;

return numAscii + (_encoding ?? s_UTF8Encoding).GetByteCount(chars, numRemaining);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are the possible values of _encoding? Can it be something other than Utf8?

Note that it better to call Encoding.UTF8.GetBytes directly without caching the encoding locally. Encoding.UTF8.GetBytes allows devitalization optimization to kick in that eliminates the overhead of Encoding being an abstract type.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It can be passed by the user when creating a text XmlDictionaryWriter, but it is only set to _encoding if the codepage is the same as utf8.
So in theory it can be any encoding class even if unlikely .

for s_encoding it does not use the default constructor but passes is (false, true) so I did no dare to do that change.
If it does not change the behaviour then that can be a simple follow up fix.

}

protected unsafe int UnsafeGetUTF8Chars(char* chars, int charCount, byte[] buffer, int offset)
Expand All @@ -386,36 +416,64 @@ protected unsafe int UnsafeGetUTF8Chars(char* chars, int charCount, byte[] buffe
byte* bytes = _bytes;
byte* bytesMax = &bytes[buffer.Length - offset];
char* charsMax = &chars[charCount];
char* simdMax = &chars[charCount - (Vector128<ushort>.Count - 1)];
char* longMax = &chars[charCount - (CharsPerLong - 1)];

while (true)
if (Sse41.IsSupported)
{
while (chars < charsMax)
if (chars < simdMax)
{
char t = *chars;
if (t >= 0x80)
break;

*bytes = (byte)t;
bytes++;
chars++;
var mask = Vector128.Create(unchecked((short)0xff80));
do
{
var v = Sse2.LoadVector128((short*)chars);
if (!Sse41.TestZ(v, mask))
goto NonAscii;

Sse2.StoreScalar((long*)bytes, Sse2.PackUnsignedSaturate(v, v).AsInt64());
bytes += Vector128<ushort>.Count;
chars += Vector128<ushort>.Count;
} while (chars < simdMax);
}
}
// Directly jump to system encoding for larger strings, since it is faster even for the all Ascii case
else if ((BitConverter.IsLittleEndian && charCount > 60)
|| (!BitConverter.IsLittleEndian && charCount > 16))
{
goto NonAscii;
}

if (chars >= charsMax)
break;

char* charsStart = chars;
while (chars < charsMax && *chars >= 0x80)
if (BitConverter.IsLittleEndian)
{
while (chars < longMax)
{
chars++;
ulong l = *(ulong*)chars;
if ((l & LongNonAsciiMask) != 0)
goto NonAscii;

// 0x00dd00cc_00bb00aa => 0x00ddddcc_ccbbbbaa
l |= (l >> 8);
*(ushort*)bytes = (ushort)l;
*(ushort*)(bytes + 2) = (ushort)(l >> 32);
bytes += CharsPerLong;
chars += CharsPerLong;
}
}

bytes += (_encoding ?? s_UTF8Encoding).GetBytes(charsStart, (int)(chars - charsStart), bytes, (int)(bytesMax - bytes));
while (chars < charsMax)
{
char t = *chars;
if (t >= 0x80)
goto NonAscii;

if (chars >= charsMax)
break;
*bytes = (byte)t;
bytes++;
chars++;
}

return (int)(bytes - _bytes);
NonAscii:
return (int)(bytes - _bytes) + (_encoding ?? s_UTF8Encoding).GetBytes(chars, (int)(charsMax - chars), bytes, (int)(bytesMax - bytes));
}
}
return 0;
Expand Down