From 52f5c1daea71707d35b0bb73f623c96665d6ca8c Mon Sep 17 00:00:00 2001 From: Kaur-Parminder Date: Thu, 21 Oct 2021 14:42:53 -0700 Subject: [PATCH] Use Buffer.BlockCopy with byte[] This change is based on changes done https://github.com/dotnet/corefx/commit/3ab3bd6efcba669bf04d42f738fe5a495ec62940. I have replaced Array.Copy wherever there is byte[]. Buffer.BlockCopy has less overhead than Array.Copy when copying byte[]s. --- .../src/Microsoft/Data/SqlClient/SqlDataReader.cs | 2 +- .../Data/SqlClient/SqlDelegatedTransaction.cs | 2 +- .../Data/SqlClient/SqlSequentialTextReader.cs | 4 ++-- .../netfx/src/Microsoft/Data/SqlClient/SqlStream.cs | 2 +- .../src/Microsoft/Data/SqlClient/Server/SqlMetaData.cs | 10 +++++----- .../Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs | 2 +- .../ManualTests/SQL/DataStreamTest/DataStreamTest.cs | 2 +- .../ManualTests/SQL/ParameterTest/StreamInputParam.cs | 2 +- .../tests/tools/TDS/TDS/TDSStream.cs | 2 +- 9 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDataReader.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDataReader.cs index 4063f78fd7..eb4f0a1ced 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDataReader.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDataReader.cs @@ -2047,7 +2047,7 @@ private bool TryGetBytesInternal(int i, long dataIndex, byte[] buffer, int buffe cbytes = length; } - Array.Copy(data, ndataIndex, buffer, bufferIndex, cbytes); + Buffer.BlockCopy(data, ndataIndex, buffer, bufferIndex, cbytes); } catch (Exception e) { diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDelegatedTransaction.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDelegatedTransaction.cs index a72605e86e..bf7f86c3a7 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDelegatedTransaction.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlDelegatedTransaction.cs @@ -585,7 +585,7 @@ private void ValidateActiveOnConnection(SqlInternalConnection connection) private Guid GetGlobalTxnIdentifierFromToken() { byte[] txnGuid = new byte[16]; - Array.Copy(_connection.PromotedDTCToken, _globalTransactionsTokenVersionSizeInBytes /* Skip the version */, txnGuid, 0, txnGuid.Length); + Buffer.BlockCopy(_connection.PromotedDTCToken, _globalTransactionsTokenVersionSizeInBytes /* Skip the version */, txnGuid, 0, txnGuid.Length); return new Guid(txnGuid); } } diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlSequentialTextReader.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlSequentialTextReader.cs index b1b4a4fed2..504fd99934 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlSequentialTextReader.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlSequentialTextReader.cs @@ -372,7 +372,7 @@ private byte[] PrepareByteBuffer(int numberOfChars, out int byteBufferUsed) { // Otherwise, copy over the leftover buffer byteBuffer = new byte[byteBufferSize]; - Array.Copy(_leftOverBytes, byteBuffer, _leftOverBytes.Length); + Buffer.BlockCopy(_leftOverBytes, 0, byteBuffer, 0,_leftOverBytes.Length); byteBufferUsed = _leftOverBytes.Length; } } @@ -411,7 +411,7 @@ private int DecodeBytesToChars(byte[] inBuffer, int inBufferCount, char[] outBuf if ((!completed) && (bytesUsed < inBufferCount)) { _leftOverBytes = new byte[inBufferCount - bytesUsed]; - Array.Copy(inBuffer, bytesUsed, _leftOverBytes, 0, _leftOverBytes.Length); + Buffer.BlockCopy(inBuffer, bytesUsed, _leftOverBytes, 0, _leftOverBytes.Length); } else { diff --git a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlStream.cs b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlStream.cs index 4e0f723361..9896fee14c 100644 --- a/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlStream.cs +++ b/src/Microsoft.Data.SqlClient/netfx/src/Microsoft/Data/SqlClient/SqlStream.cs @@ -452,7 +452,7 @@ override public int Read(byte[] buffer, int offset, int count) cb = _cachedBytes[_currentArrayIndex].Length - _currentPosition; if (cb > count) cb = count; - Array.Copy(_cachedBytes[_currentArrayIndex], _currentPosition, buffer, offset, cb); + Buffer.BlockCopy(_cachedBytes[_currentArrayIndex], _currentPosition, buffer, offset, cb); _currentPosition += cb; count -= (int)cb; diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/SqlMetaData.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/SqlMetaData.cs index 051b31d79b..0a0be7eeb3 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/SqlMetaData.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/SqlMetaData.cs @@ -1286,7 +1286,7 @@ public SqlBinary Adjust(SqlBinary value) { byte[] rgbValue = value.Value; byte[] rgbNewValue = new byte[MaxLength]; - Array.Copy(rgbValue, rgbNewValue, rgbValue.Length); + Buffer.BlockCopy(rgbValue, 0, rgbNewValue, 0, rgbValue.Length); Array.Clear(rgbNewValue, rgbValue.Length, rgbNewValue.Length - rgbValue.Length); return new SqlBinary(rgbNewValue); } @@ -1308,7 +1308,7 @@ public SqlBinary Adjust(SqlBinary value) { byte[] rgbValue = value.Value; byte[] rgbNewValue = new byte[MaxLength]; - Array.Copy(rgbValue, rgbNewValue, (int)MaxLength); + Buffer.BlockCopy(rgbValue,0, rgbNewValue,0, (int)MaxLength); value = new SqlBinary(rgbNewValue); } @@ -1400,7 +1400,7 @@ public SqlBytes Adjust(SqlBytes value) if (value.MaxLength < MaxLength) { byte[] rgbNew = new byte[MaxLength]; - Array.Copy(value.Buffer, rgbNew, (int)oldLength); + Buffer.BlockCopy(value.Buffer, 0,rgbNew,0, (int)oldLength); value = new SqlBytes(rgbNew); } @@ -1929,7 +1929,7 @@ public byte[] Adjust(byte[] value) if (value.Length < MaxLength) { byte[] rgbNewValue = new byte[MaxLength]; - Array.Copy(value, rgbNewValue, value.Length); + Buffer.BlockCopy(value, 0, rgbNewValue, 0, value.Length); Array.Clear(rgbNewValue, value.Length, (int)rgbNewValue.Length - value.Length); return rgbNewValue; } @@ -1950,7 +1950,7 @@ public byte[] Adjust(byte[] value) if (value.Length > MaxLength && Max != MaxLength) { byte[] rgbNewValue = new byte[MaxLength]; - Array.Copy(value, rgbNewValue, (int)MaxLength); + Buffer.BlockCopy(value, 0, rgbNewValue, 0, (int)MaxLength); value = rgbNewValue; } diff --git a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs index f53396cdaa..a566728b72 100644 --- a/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs +++ b/src/Microsoft.Data.SqlClient/src/Microsoft/Data/SqlClient/Server/ValueUtilsSmi.cs @@ -124,7 +124,7 @@ private static long GetBytesConversion(SmiEventSink_Default sink, ITypedGettersV length = CheckXetParameters(metaData.SqlDbType, metaData.MaxLength * sizeof(char), value.Length, fieldOffset, buffer.Length, bufferOffset, length); - Array.Copy(value.Value, checked((int)fieldOffset), buffer, bufferOffset, length); + Buffer.BlockCopy(value.Value, checked((int)fieldOffset), buffer, bufferOffset, length); return length; } diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/DataStreamTest/DataStreamTest.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/DataStreamTest/DataStreamTest.cs index 2c5bce8c82..870222cfa1 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/DataStreamTest/DataStreamTest.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/DataStreamTest/DataStreamTest.cs @@ -130,7 +130,7 @@ private static byte[] CreateBinaryTable(SqlConnection connection, string tableNa while (position < data.Length) { int copyCount = Math.Min(pattern.Length, data.Length - position); - Array.Copy(pattern, 0, data, position, copyCount); + Buffer.BlockCopy(pattern, 0, data, position, copyCount); position += copyCount; } diff --git a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ParameterTest/StreamInputParam.cs b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ParameterTest/StreamInputParam.cs index 3d15b4346b..2b47054bf4 100644 --- a/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ParameterTest/StreamInputParam.cs +++ b/src/Microsoft.Data.SqlClient/tests/ManualTests/SQL/ParameterTest/StreamInputParam.cs @@ -94,7 +94,7 @@ private int ReadInternal(byte[] buffer, int offset, int count) } if (_errorPos >= _pos && _errorPos < _pos + nRead) throw new CustomStreamException(); - Array.Copy(_data, _pos, buffer, offset, nRead); + Buffer.BlockCopy(_data, _pos, buffer, offset, nRead); _pos += nRead; return nRead; } diff --git a/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS/TDSStream.cs b/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS/TDSStream.cs index 90a295bbc1..94abbf5818 100644 --- a/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS/TDSStream.cs +++ b/src/Microsoft.Data.SqlClient/tests/tools/TDS/TDS/TDSStream.cs @@ -349,7 +349,7 @@ public override void Write(byte[] buffer, int offset, int count) if (packetDataToWrite > 0) { // Append new data to the end of the packet data - Array.Copy(buffer, bufferWrittenPosition + offset, _outgoingPacket, OutgoingPacketHeader.Length, packetDataToWrite); + Buffer.BlockCopy(buffer, bufferWrittenPosition + offset, _outgoingPacket, OutgoingPacketHeader.Length, packetDataToWrite); // Register that we've written new data bufferWrittenPosition += packetDataToWrite;