Skip to content

Commit 00a3364

Browse files
authored
Small spanifications (#667)
1 parent 22d1467 commit 00a3364

File tree

2 files changed

+42
-68
lines changed

2 files changed

+42
-68
lines changed

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

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1619,47 +1619,49 @@ internal byte[] SerializeInt(int v, TdsParserStateObject stateObj)
16191619
{
16201620
if (null == stateObj._bIntBytes)
16211621
{
1622-
stateObj._bIntBytes = new byte[4];
1622+
stateObj._bIntBytes = new byte[sizeof(int)];
16231623
}
16241624
else
16251625
{
1626-
Debug.Assert(4 == stateObj._bIntBytes.Length);
1626+
Debug.Assert(sizeof(int) == stateObj._bIntBytes.Length);
16271627
}
16281628

1629-
int current = 0;
1630-
byte[] bytes = stateObj._bIntBytes;
1631-
bytes[current++] = (byte)(v & 0xff);
1632-
bytes[current++] = (byte)((v >> 8) & 0xff);
1633-
bytes[current++] = (byte)((v >> 16) & 0xff);
1634-
bytes[current++] = (byte)((v >> 24) & 0xff);
1635-
return bytes;
1629+
WriteInt(stateObj._bIntBytes.AsSpan(), v);
1630+
return stateObj._bIntBytes;
16361631
}
16371632

1638-
//
1639-
// Takes an int and writes it as an int.
1640-
//
16411633
internal void WriteInt(int v, TdsParserStateObject stateObj)
16421634
{
1635+
Span<byte> buffer = stackalloc byte[sizeof(int)];
1636+
WriteInt(buffer, v);
16431637
if ((stateObj._outBytesUsed + 4) > stateObj._outBuff.Length)
16441638
{
16451639
// if all of the int doesn't fit into the buffer
1646-
for (int shiftValue = 0; shiftValue < sizeof(int) * 8; shiftValue += 8)
1640+
for (int index = 0; index < sizeof(int); index++)
16471641
{
1648-
stateObj.WriteByte((byte)((v >> shiftValue) & 0xff));
1642+
stateObj.WriteByte(buffer[index]);
16491643
}
16501644
}
16511645
else
16521646
{
16531647
// all of the int fits into the buffer
1654-
// NOTE: We don't use a loop here for performance
1655-
stateObj._outBuff[stateObj._outBytesUsed] = (byte)(v & 0xff);
1656-
stateObj._outBuff[stateObj._outBytesUsed + 1] = (byte)((v >> 8) & 0xff);
1657-
stateObj._outBuff[stateObj._outBytesUsed + 2] = (byte)((v >> 16) & 0xff);
1658-
stateObj._outBuff[stateObj._outBytesUsed + 3] = (byte)((v >> 24) & 0xff);
1648+
buffer.CopyTo(stateObj._outBuff.AsSpan(stateObj._outBytesUsed, sizeof(int)));
16591649
stateObj._outBytesUsed += 4;
16601650
}
16611651
}
16621652

1653+
internal static void WriteInt(Span<byte> buffer, int value)
1654+
{
1655+
#if netcoreapp
1656+
BitConverter.TryWriteBytes(buffer, value);
1657+
#else
1658+
buffer[0] = (byte)(value & 0xff);
1659+
buffer[1] = (byte)((value >> 8) & 0xff);
1660+
buffer[2] = (byte)((value >> 16) & 0xff);
1661+
buffer[3] = (byte)((value >> 24) & 0xff);
1662+
#endif
1663+
}
1664+
16631665
//
16641666
// Takes a float and writes it as a 32 bit float.
16651667
//

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

Lines changed: 21 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,107 +1403,87 @@ internal bool TryReadChar(out char value)
14031403
{
14041404
Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
14051405

1406-
byte[] buffer;
1407-
int offset;
1406+
Span<byte> buffer = stackalloc byte[2];
14081407
if (((_inBytesUsed + 2) > _inBytesRead) || (_inBytesPacket < 2))
14091408
{
14101409
// If the char isn't fully in the buffer, or if it isn't fully in the packet,
14111410
// then use ReadByteArray since the logic is there to take care of that.
1412-
1413-
if (!TryReadByteArray(_bTmp, 2))
1411+
if (!TryReadByteArray(buffer, 2))
14141412
{
14151413
value = '\0';
14161414
return false;
14171415
}
1418-
1419-
buffer = _bTmp;
1420-
offset = 0;
14211416
}
14221417
else
14231418
{
14241419
// The entire char is in the packet and in the buffer, so just return it
14251420
// and take care of the counters.
1426-
1427-
buffer = _inBuff;
1428-
offset = _inBytesUsed;
1429-
1421+
buffer = _inBuff.AsSpan(_inBytesUsed, 2);
14301422
_inBytesUsed += 2;
14311423
_inBytesPacket -= 2;
14321424
}
14331425

14341426
AssertValidState();
1435-
value = (char)((buffer[offset + 1] << 8) + buffer[offset]);
1427+
value = (char)((buffer[1] << 8) + buffer[0]);
14361428
return true;
14371429
}
14381430

14391431
internal bool TryReadInt16(out short value)
14401432
{
14411433
Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
14421434

1443-
byte[] buffer;
1444-
int offset;
1435+
Span<byte> buffer = stackalloc byte[2];
14451436
if (((_inBytesUsed + 2) > _inBytesRead) || (_inBytesPacket < 2))
14461437
{
14471438
// If the int16 isn't fully in the buffer, or if it isn't fully in the packet,
14481439
// then use ReadByteArray since the logic is there to take care of that.
1449-
1450-
if (!TryReadByteArray(_bTmp, 2))
1440+
if (!TryReadByteArray(buffer, 2))
14511441
{
14521442
value = default;
14531443
return false;
14541444
}
1455-
1456-
buffer = _bTmp;
1457-
offset = 0;
14581445
}
14591446
else
14601447
{
14611448
// The entire int16 is in the packet and in the buffer, so just return it
14621449
// and take care of the counters.
1463-
1464-
buffer = _inBuff;
1465-
offset = _inBytesUsed;
1466-
1450+
buffer = _inBuff.AsSpan(_inBytesUsed,2);
14671451
_inBytesUsed += 2;
14681452
_inBytesPacket -= 2;
14691453
}
14701454

14711455
AssertValidState();
1472-
value = (short)((buffer[offset + 1] << 8) + buffer[offset]);
1456+
value = (short)((buffer[1] << 8) + buffer[0]);
14731457
return true;
14741458
}
14751459

14761460
internal bool TryReadInt32(out int value)
14771461
{
14781462
Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
1463+
Span<byte> buffer = stackalloc byte[4];
14791464
if (((_inBytesUsed + 4) > _inBytesRead) || (_inBytesPacket < 4))
14801465
{
14811466
// If the int isn't fully in the buffer, or if it isn't fully in the packet,
14821467
// then use ReadByteArray since the logic is there to take care of that.
1483-
1484-
if (!TryReadByteArray(_bTmp, 4))
1468+
if (!TryReadByteArray(buffer, 4))
14851469
{
14861470
value = 0;
14871471
return false;
14881472
}
1489-
1490-
AssertValidState();
1491-
value = BitConverter.ToInt32(_bTmp, 0);
1492-
return true;
14931473
}
14941474
else
14951475
{
14961476
// The entire int is in the packet and in the buffer, so just return it
14971477
// and take care of the counters.
1498-
1499-
value = BitConverter.ToInt32(_inBuff, _inBytesUsed);
1500-
1478+
buffer = _inBuff.AsSpan(_inBytesUsed, 4);
15011479
_inBytesUsed += 4;
15021480
_inBytesPacket -= 4;
1503-
1504-
AssertValidState();
1505-
return true;
15061481
}
1482+
1483+
AssertValidState();
1484+
value = (buffer[3] << 24) + (buffer[2] <<16) + (buffer[1] << 8) + buffer[0];
1485+
return true;
1486+
15071487
}
15081488

15091489
// This method is safe to call when doing async without snapshot
@@ -1559,36 +1539,28 @@ internal bool TryReadUInt16(out ushort value)
15591539
{
15601540
Debug.Assert(_syncOverAsync || !_asyncReadWithoutSnapshot, "This method is not safe to call when doing sync over async");
15611541

1562-
byte[] buffer;
1563-
int offset;
1542+
Span<byte> buffer = stackalloc byte[2];
15641543
if (((_inBytesUsed + 2) > _inBytesRead) || (_inBytesPacket < 2))
15651544
{
15661545
// If the uint16 isn't fully in the buffer, or if it isn't fully in the packet,
15671546
// then use ReadByteArray since the logic is there to take care of that.
1568-
1569-
if (!TryReadByteArray(_bTmp, 2))
1547+
if (!TryReadByteArray(buffer, 2))
15701548
{
15711549
value = default;
15721550
return false;
15731551
}
1574-
1575-
buffer = _bTmp;
1576-
offset = 0;
15771552
}
15781553
else
15791554
{
15801555
// The entire uint16 is in the packet and in the buffer, so just return it
15811556
// and take care of the counters.
1582-
1583-
buffer = _inBuff;
1584-
offset = _inBytesUsed;
1585-
1557+
buffer = _inBuff.AsSpan(_inBytesUsed, 2);
15861558
_inBytesUsed += 2;
15871559
_inBytesPacket -= 2;
15881560
}
15891561

15901562
AssertValidState();
1591-
value = (ushort)((buffer[offset + 1] << 8) + buffer[offset]);
1563+
value = (ushort)((buffer[1] << 8) + buffer[0]);
15921564
return true;
15931565
}
15941566

@@ -3627,8 +3599,8 @@ private void SniWriteStatisticsAndTracing()
36273599
statistics.RequestNetworkServerTimer();
36283600
}
36293601
}
3630-
[Conditional("DEBUG")]
36313602

3603+
[Conditional("DEBUG")]
36323604
private void AssertValidState()
36333605
{
36343606
if (_inBytesUsed < 0 || _inBytesRead < 0)

0 commit comments

Comments
 (0)