Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite DbBuffer Read/Write methods with stackalloc #88395

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,34 @@ internal byte[] ReadBytes(int offset, byte[] destination, int startIndex, int le
return destination;
}

internal Span<byte> ReadBytes(int offset, Span<byte> destination)
{
offset += BaseOffset;
Validate(offset, destination.Length);
Debug.Assert(0 == offset % ADP.PtrSize, "invalid alignment");

bool mustRelease = false;

try
{
DangerousAddRef(ref mustRelease);

IntPtr ptr = ADP.IntPtrOffset(DangerousGetHandle(), offset);
unsafe
{
new ReadOnlySpan<byte>(ptr.ToPointer(), destination.Length).CopyTo(destination);
}
}
finally
{
if (mustRelease)
{
DangerousRelease();
}
}
return destination;
}

internal char ReadChar(int offset)
{
short value = ReadInt16(offset);
Expand Down Expand Up @@ -257,6 +285,33 @@ internal void ReadInt16Array(int offset, short[] destination, int startIndex, in
}
}
}
internal void ReadInt16Array(int offset, Span<short> destination)
{
offset += BaseOffset;
Validate(offset, 2 * destination.Length);
Debug.Assert(0 == offset % ADP.PtrSize, "invalid alignment");
Debug.Assert(null != destination, "null destination");
Poppyto marked this conversation as resolved.
Show resolved Hide resolved

bool mustRelease = false;

try
{
DangerousAddRef(ref mustRelease);

IntPtr ptr = ADP.IntPtrOffset(DangerousGetHandle(), offset);
unsafe
{
new ReadOnlySpan<short>(ptr.ToPointer(), destination.Length).CopyTo(destination);
}
}
finally
{
if (mustRelease)
{
DangerousRelease();
}
}
}

internal int ReadInt32(int offset)
{
Expand Down Expand Up @@ -455,6 +510,34 @@ internal void WriteBytes(int offset, byte[] source, int startIndex, int length)
}
}

internal void WriteBytes(int offset, ReadOnlySpan<byte> source)
{
offset += BaseOffset;
Validate(offset, source.Length);
Debug.Assert(0 == offset % ADP.PtrSize, "invalid alignment");
Debug.Assert(null != source, "null source");
Poppyto marked this conversation as resolved.
Show resolved Hide resolved

bool mustRelease = false;

try
{
DangerousAddRef(ref mustRelease);

IntPtr ptr = ADP.IntPtrOffset(DangerousGetHandle(), offset);
unsafe
{
source.CopyTo(new Span<byte>(ptr.ToPointer(), source.Length));
}
}
finally
{
if (mustRelease)
{
DangerousRelease();
}
}
}

internal void WriteCharArray(int offset, char[] source, int startIndex, int length)
{
offset += BaseOffset;
Expand Down Expand Up @@ -536,6 +619,34 @@ internal void WriteInt16Array(int offset, short[] source, int startIndex, int le
}
}

internal void WriteInt16Array(int offset, ReadOnlySpan<short> source)
{
offset += BaseOffset;
Validate(offset, 2 * source.Length);
Debug.Assert(0 == offset % ADP.PtrSize, "invalid alignment");
Debug.Assert(null != source, "null source");
Poppyto marked this conversation as resolved.
Show resolved Hide resolved

bool mustRelease = false;

try
{
DangerousAddRef(ref mustRelease);

IntPtr ptr = ADP.IntPtrOffset(DangerousGetHandle(), offset);
unsafe
{
source.CopyTo(new Span<short>(ptr.ToPointer(), source.Length));
}
}
finally
{
if (mustRelease)
{
DangerousRelease();
}
}
}

internal void WriteInt32(int offset, int value)
{
offset += BaseOffset;
Expand Down Expand Up @@ -661,119 +772,141 @@ internal void ZeroMemory()

internal Guid ReadGuid(int offset)
{
// faster than Marshal.PtrToStructure(offset, typeof(Guid))
byte[] buffer = new byte[16];
ReadBytes(offset, buffer, 0, 16);
return new Guid(buffer);
offset += BaseOffset;
Poppyto marked this conversation as resolved.
Show resolved Hide resolved
Debug.Assert(0 == offset % ADP.PtrSize, "invalid alignment");
Poppyto marked this conversation as resolved.
Show resolved Hide resolved
bool mustRelease = false;
try
{
DangerousAddRef(ref mustRelease);
IntPtr ptr = ADP.IntPtrOffset(DangerousGetHandle(), offset);
unsafe
{
return new Guid(new ReadOnlySpan<byte>(ptr.ToPointer(), sizeof(Guid)));
}
Poppyto marked this conversation as resolved.
Show resolved Hide resolved
}
finally
{
if (mustRelease)
{
DangerousRelease();
}
}
}
internal void WriteGuid(int offset, Guid value)
{
Poppyto marked this conversation as resolved.
Show resolved Hide resolved
// faster than Marshal.Copy(value.GetByteArray()
StructureToPtr(offset, value);
ReadOnlySpan<Guid> spanGuid = MemoryMarshal.CreateReadOnlySpan(ref Unsafe.AsRef(value), 1);
WriteBytes(offset, MemoryMarshal.AsBytes(spanGuid));
Poppyto marked this conversation as resolved.
Show resolved Hide resolved
}

internal DateTime ReadDate(int offset)
{
short[] buffer = new short[3];
ReadInt16Array(offset, buffer, 0, 3);
Span<short> spanBuffer = stackalloc short[3];
ReadInt16Array(offset, spanBuffer.Slice(0, 3));
return new DateTime(
unchecked((ushort)buffer[0]), // Year
unchecked((ushort)buffer[1]), // Month
unchecked((ushort)buffer[2])); // Day
unchecked((ushort)spanBuffer[0]), // Year
unchecked((ushort)spanBuffer[1]), // Month
unchecked((ushort)spanBuffer[2])); // Day
Comment on lines +819 to +824
Copy link
Contributor

@xtqqczze xtqqczze Jul 7, 2023

Choose a reason for hiding this comment

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

This could also be rewritten as unsafe method:

[StructLayout(LayoutKind.Sequential)]
public unsafe readonly struct DateData
{
    public readonly ushort Year;
    public readonly ushort Month;
    public readonly ushort Day;
}
DateData data = *(DateData*)addr;
return new DateTime(data.Year, data.Month, data.Day);

}
internal void WriteDate(int offset, DateTime value)
{
short[] buffer = new short[3] {
Span<short> spanBuffer = stackalloc short[3] {
unchecked((short)value.Year),
unchecked((short)value.Month),
unchecked((short)value.Day),
};
WriteInt16Array(offset, buffer, 0, 3);
WriteInt16Array(offset, spanBuffer.Slice(0, 3));
}

internal TimeSpan ReadTime(int offset)
{
short[] buffer = new short[3];
ReadInt16Array(offset, buffer, 0, 3);
Span<short> spanBuffer = stackalloc short[3];
ReadInt16Array(offset, spanBuffer.Slice(0, 3));
return new TimeSpan(
unchecked((ushort)buffer[0]), // Hours
unchecked((ushort)buffer[1]), // Minutes
unchecked((ushort)buffer[2])); // Seconds
unchecked((ushort)spanBuffer[0]), // Hours
unchecked((ushort)spanBuffer[1]), // Minutes
unchecked((ushort)spanBuffer[2])); // Seconds
}
internal void WriteTime(int offset, TimeSpan value)
{
short[] buffer = new short[3] {
Span<short> spanBuffer = stackalloc short[3] {
unchecked((short)value.Hours),
unchecked((short)value.Minutes),
unchecked((short)value.Seconds),
};
WriteInt16Array(offset, buffer, 0, 3);
WriteInt16Array(offset, spanBuffer.Slice(0, 3));
}

internal DateTime ReadDateTime(int offset)
{
short[] buffer = new short[6];
ReadInt16Array(offset, buffer, 0, 6);
Span<short> spanBuffer = stackalloc short[6];
ReadInt16Array(offset, spanBuffer.Slice(0, 6));
int ticks = ReadInt32(offset + 12);
DateTime value = new DateTime(
unchecked((ushort)buffer[0]), // Year
unchecked((ushort)buffer[1]), // Month
unchecked((ushort)buffer[2]), // Day
unchecked((ushort)buffer[3]), // Hours
unchecked((ushort)buffer[4]), // Minutes
unchecked((ushort)buffer[5])); // Seconds
unchecked((ushort)spanBuffer[0]), // Year
unchecked((ushort)spanBuffer[1]), // Month
unchecked((ushort)spanBuffer[2]), // Day
unchecked((ushort)spanBuffer[3]), // Hours
unchecked((ushort)spanBuffer[4]), // Minutes
unchecked((ushort)spanBuffer[5])); // Seconds
return value.AddTicks(ticks / 100);
}
internal void WriteDateTime(int offset, DateTime value)
{
int ticks = (int)(value.Ticks % 10000000L) * 100;
short[] buffer = new short[6] {
Span<short> spanBuffer = stackalloc short[6] {
unchecked((short)value.Year),
unchecked((short)value.Month),
unchecked((short)value.Day),
unchecked((short)value.Hour),
unchecked((short)value.Minute),
unchecked((short)value.Second),
};
WriteInt16Array(offset, buffer, 0, 6);
WriteInt16Array(offset, spanBuffer.Slice(0, 6));
WriteInt32(offset + 12, ticks);
}

internal decimal ReadNumeric(int offset)
{
byte[] bits = new byte[20];
ReadBytes(offset, bits, 1, 19);
Span<byte> spanBits = stackalloc byte[20];
ReadBytes(offset, spanBits.Slice(1, 19));

int[] buffer = new int[4];
buffer[3] = ((int)bits[2]) << 16; // scale
if (0 == bits[3])
Span<int> spanBuffer = stackalloc int[4];
spanBuffer[3] = ((int)spanBits[2]) << 16; // scale
if (0 == spanBits[3])
{
buffer[3] |= unchecked((int)0x80000000); //sign
spanBuffer[3] |= unchecked((int)0x80000000); //sign
}
buffer[0] = BitConverter.ToInt32(bits, 4); // low
buffer[1] = BitConverter.ToInt32(bits, 8); // mid
buffer[2] = BitConverter.ToInt32(bits, 12); // high
if (0 != BitConverter.ToInt32(bits, 16))
spanBuffer[0] = BitConverter.ToInt32(spanBits.Slice(4)); // low
spanBuffer[1] = BitConverter.ToInt32(spanBits.Slice(8)); // mid
spanBuffer[2] = BitConverter.ToInt32(spanBits.Slice(12)); // high
if (0 != BitConverter.ToInt32(spanBits.Slice(16)))
{
throw ADP.NumericToDecimalOverflow();
}
return new decimal(buffer);
return new decimal(spanBuffer);
}

internal void WriteNumeric(int offset, decimal value, byte precision)
{
int[] tmp = decimal.GetBits(value);
byte[] buffer = new byte[20];

buffer[1] = precision;
Buffer.BlockCopy(tmp, 14, buffer, 2, 2); // copy sign and scale
buffer[3] = (byte)((0 == buffer[3]) ? 1 : 0); // flip sign for native
Buffer.BlockCopy(tmp, 0, buffer, 4, 12);
buffer[16] = 0;
buffer[17] = 0;
buffer[18] = 0;
buffer[19] = 0;
WriteBytes(offset, buffer, 1, 19);
Span<byte> spanBuffer = stackalloc byte[20];

Span<byte> spanByteTmp = MemoryMarshal.Cast<int, byte>(tmp.AsSpan());
Span<int> spanIntBuffer = MemoryMarshal.Cast<byte, int>(spanBuffer);

spanBuffer[1] = precision;
//Buffer.BlockCopy(tmp, 14, buffer, 2, 2); // copy sign and scale
spanByteTmp.Slice(14, 2).CopyTo(spanBuffer.Slice(2, 2));// copy sign and scale

spanBuffer[3] = (byte)((0 == spanBuffer[3]) ? 1 : 0); // flip sign for native
//Buffer.BlockCopy(tmp, 0, buffer, 4, 12);
spanByteTmp.Slice(0, 12).CopyTo(spanBuffer.Slice(4, 12));

spanBuffer[16] = 0;
spanBuffer[17] = 0;
spanBuffer[18] = 0;
spanBuffer[19] = 0;
WriteBytes(offset, spanBuffer.Slice(1, 19));
}

[ConditionalAttribute("DEBUG")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,15 +332,15 @@ internal HandleRef PtrOffset(int offset, int length)

internal void WriteODBCDateTime(int offset, DateTime value)
{
short[] buffer = new short[6] {
Span<short> spanBuffer = stackalloc short[6] {
unchecked((short)value.Year),
unchecked((short)value.Month),
unchecked((short)value.Day),
unchecked((short)value.Hour),
unchecked((short)value.Minute),
unchecked((short)value.Second),
};
WriteInt16Array(offset, buffer, 0, 6);
WriteInt16Array(offset, spanBuffer.Slice(0, 6));
WriteInt32(offset + 12, value.Millisecond * 1000000); //fraction
}
}
Expand Down
Loading