Skip to content

Commit

Permalink
Use span in array writing to reduce bounds checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Daniel-Svensson committed Jun 29, 2022
1 parent 0e736f6 commit d1e850b
Showing 1 changed file with 8 additions and 8 deletions.
Expand Up @@ -866,29 +866,29 @@ public unsafe void UnsafeWriteArray(XmlBinaryNodeType nodeType, int count, byte*
public void WriteDateTimeArray(DateTime[] array, int offset, int count)
{
WriteArrayInfo(XmlBinaryNodeType.DateTimeTextWithEndElement, count);
for (int i = 0; i < count; i++)
foreach (var dateTime in array.AsSpan(offset, count))
{
WriteInt64(ToBinary(array[offset + i]));
WriteInt64(ToBinary(dateTime));
}
}

public void WriteGuidArray(Guid[] array, int offset, int count)
{
Span<byte> bytes = stackalloc byte[16];
WriteArrayInfo(XmlBinaryNodeType.GuidTextWithEndElement, count);
for (int i = 0; i < count; i++)
foreach (ref var guid in array.AsSpan(offset, count))
{
array[offset + i].TryWriteBytes(bytes);
WriteBytes(bytes);
var bytes = GetBuffer(16, out int bufferOffset).AsSpan(bufferOffset);
guid.TryWriteBytes(bytes);
Advance(16);
}
}

public void WriteTimeSpanArray(TimeSpan[] array, int offset, int count)
{
WriteArrayInfo(XmlBinaryNodeType.TimeSpanTextWithEndElement, count);
for (int i = 0; i < count; i++)
foreach (ref TimeSpan timespan in array.AsSpan(offset, count))
{
WriteInt64(array[offset + i].Ticks);
WriteInt64(timespan.Ticks);
}
}

Expand Down

0 comments on commit d1e850b

Please sign in to comment.