Skip to content

Commit 878fcd4

Browse files
committed
netcore, netfx: merge WriteInt and SerializeInt
netcore: removed a simple WriteInt which did nothing besides call BinaryPrimitives. netcore: when there's space in the packet buffer, WriteInt will now write to it directly (rather than write to a stack-allocated span and copy.)
1 parent 06b292d commit 878fcd4

File tree

2 files changed

+13
-23
lines changed
  • src/Microsoft.Data.SqlClient

2 files changed

+13
-23
lines changed

src/Microsoft.Data.SqlClient/netcore/src/Microsoft/Data/SqlClient/TdsParser.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1748,16 +1748,17 @@ internal byte[] SerializeInt(int v, TdsParserStateObject stateObj)
17481748
Debug.Assert(sizeof(int) == stateObj._bIntBytes.Length);
17491749
}
17501750

1751-
WriteInt(stateObj._bIntBytes.AsSpan(), v);
1751+
BinaryPrimitives.WriteInt32LittleEndian(stateObj._bIntBytes, v);
17521752
return stateObj._bIntBytes;
17531753
}
17541754

17551755
internal void WriteInt(int v, TdsParserStateObject stateObj)
17561756
{
1757-
Span<byte> buffer = stackalloc byte[sizeof(int)];
1758-
WriteInt(buffer, v);
17591757
if ((stateObj._outBytesUsed + 4) > stateObj._outBuff.Length)
17601758
{
1759+
Span<byte> buffer = stackalloc byte[sizeof(int)];
1760+
1761+
BinaryPrimitives.WriteInt32LittleEndian(buffer, v);
17611762
// if all of the int doesn't fit into the buffer
17621763
for (int index = 0; index < sizeof(int); index++)
17631764
{
@@ -1767,16 +1768,11 @@ internal void WriteInt(int v, TdsParserStateObject stateObj)
17671768
else
17681769
{
17691770
// all of the int fits into the buffer
1770-
buffer.CopyTo(stateObj._outBuff.AsSpan(stateObj._outBytesUsed, sizeof(int)));
1771+
BinaryPrimitives.WriteInt32LittleEndian(stateObj._outBuff.AsSpan(stateObj._outBytesUsed, sizeof(int)), v);
17711772
stateObj._outBytesUsed += 4;
17721773
}
17731774
}
17741775

1775-
internal static void WriteInt(Span<byte> buffer, int value)
1776-
{
1777-
BinaryPrimitives.TryWriteInt32LittleEndian(buffer, value);
1778-
}
1779-
17801776
//
17811777
// Takes a float and writes it as a 32 bit float.
17821778
//

src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/TdsParser.cs

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1776,33 +1776,27 @@ internal byte[] SerializeInt(int v, TdsParserStateObject stateObj)
17761776
Debug.Assert(sizeof(int) == stateObj._bIntBytes.Length);
17771777
}
17781778

1779-
int current = 0;
1780-
byte[] bytes = stateObj._bIntBytes;
1781-
bytes[current++] = (byte)(v & 0xff);
1782-
bytes[current++] = (byte)((v >> 8) & 0xff);
1783-
bytes[current++] = (byte)((v >> 16) & 0xff);
1784-
bytes[current++] = (byte)((v >> 24) & 0xff);
1785-
return bytes;
1779+
BinaryPrimitives.WriteInt32LittleEndian(stateObj._bIntBytes, v);
1780+
return stateObj._bIntBytes;
17861781
}
17871782

17881783
internal void WriteInt(int v, TdsParserStateObject stateObj)
17891784
{
17901785
if ((stateObj._outBytesUsed + 4) > stateObj._outBuff.Length)
17911786
{
1787+
Span<byte> buffer = stackalloc byte[sizeof(int)];
1788+
1789+
BinaryPrimitives.WriteInt32LittleEndian(buffer, v);
17921790
// if all of the int doesn't fit into the buffer
1793-
for (int shiftValue = 0; shiftValue < sizeof(int) * 8; shiftValue += 8)
1791+
for (int index = 0; index < sizeof(int); index++)
17941792
{
1795-
stateObj.WriteByte((byte)((v >> shiftValue) & 0xff));
1793+
stateObj.WriteByte(buffer[index]);
17961794
}
17971795
}
17981796
else
17991797
{
18001798
// all of the int fits into the buffer
1801-
// NOTE: We don't use a loop here for performance
1802-
stateObj._outBuff[stateObj._outBytesUsed] = (byte)(v & 0xff);
1803-
stateObj._outBuff[stateObj._outBytesUsed + 1] = (byte)((v >> 8) & 0xff);
1804-
stateObj._outBuff[stateObj._outBytesUsed + 2] = (byte)((v >> 16) & 0xff);
1805-
stateObj._outBuff[stateObj._outBytesUsed + 3] = (byte)((v >> 24) & 0xff);
1799+
BinaryPrimitives.WriteInt32LittleEndian(stateObj._outBuff.AsSpan(stateObj._outBytesUsed, sizeof(int)), v);
18061800
stateObj._outBytesUsed += 4;
18071801
}
18081802
}

0 commit comments

Comments
 (0)