Skip to content

Commit 51edc67

Browse files
committed
Use Buffer.BlockCopy with byte[]
In particular after dotnet/coreclr#3118, Buffer.BlockCopy has less overhead than Array.Copy when copying byte[]s, such that there's no benefit to using Array.Copy and potential benefit to using Buffer.BlockCopy. This commit replaces usage of Array.Copy(byte[], ...) in corefx with Buffer.BlockCopy(byte[], ...). A lot of places were already using it. (In a few places where we weren't passing lower bounds to Array.Copy with T[] arguments, I added explicit lower bounds as well to avoid the overload needing to call GetLowerBound.)
1 parent 2cfbb08 commit 51edc67

34 files changed

Lines changed: 61 additions & 61 deletions

File tree

src/Common/src/System/Net/Logging/GlobalLog.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ public static void Dump(byte[] buffer, int offset, int length)
248248
}
249249

250250
var bufferSegment = new byte[length];
251-
Array.Copy(buffer, offset, bufferSegment, 0, length);
251+
Buffer.BlockCopy(buffer, offset, bufferSegment, 0, length);
252252
EventSourceLogging.Log.DebugDumpArray(bufferSegment);
253253
}
254254

src/Common/src/System/Security/Cryptography/RSAOpenSsl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ public override byte[] Decrypt(byte[] data, RSAEncryptionPadding padding)
108108
}
109109

110110
byte[] plainBytes = new byte[returnValue];
111-
Array.Copy(buf, 0, plainBytes, 0, returnValue);
111+
Buffer.BlockCopy(buf, 0, plainBytes, 0, returnValue);
112112
return plainBytes;
113113
}
114114

src/Microsoft.CSharp/src/Microsoft/CSharp/RuntimeBinder/Semantics/Symbols/SymbolManagerBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ public TypeArray AllocParams(params CType[] types)
400400
public TypeArray ConcatParams(CType[] prgtype1, CType[] prgtype2)
401401
{
402402
CType[] combined = new CType[prgtype1.Length + prgtype2.Length];
403-
Array.Copy(prgtype1, combined, prgtype1.Length);
403+
Array.Copy(prgtype1, 0, combined, 0, prgtype1.Length);
404404
Array.Copy(prgtype2, 0, combined, prgtype1.Length, prgtype2.Length);
405405
return AllocParams(combined);
406406
}

src/System.ComponentModel.Annotations/src/System/ComponentModel/DataAnnotations/UIHintAttribute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ public UIHintImplementation(string uiHint, string presentationLayer, params obje
100100
if (controlParameters != null)
101101
{
102102
_inputControlParameters = new object[controlParameters.Length];
103-
Array.Copy(controlParameters, _inputControlParameters, controlParameters.Length);
103+
Array.Copy(controlParameters, 0, _inputControlParameters, 0, controlParameters.Length);
104104
}
105105
}
106106

src/System.Data.SqlClient/src/System/Data/Common/DataRecordInternal.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ public override long GetBytes(int i, long dataIndex, byte[] buffer, int bufferIn
143143
}
144144

145145
// until arrays are 64 bit, we have to do these casts
146-
Array.Copy(data, ndataIndex, buffer, bufferIndex, (int)cbytes);
146+
Buffer.BlockCopy(data, ndataIndex, buffer, bufferIndex, (int)cbytes);
147147
}
148148
catch (Exception e)
149149
{

src/System.Data.SqlClient/src/System/Data/Sql/SqlMetaData.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,7 @@ public SqlBinary Adjust(SqlBinary value)
942942
{
943943
byte[] rgbValue = value.Value;
944944
byte[] rgbNewValue = new byte[MaxLength];
945-
Array.Copy(rgbValue, 0, rgbNewValue, 0, rgbValue.Length);
945+
Buffer.BlockCopy(rgbValue, 0, rgbNewValue, 0, rgbValue.Length);
946946
Array.Clear(rgbNewValue, rgbValue.Length, rgbNewValue.Length - rgbValue.Length);
947947
return new SqlBinary(rgbNewValue);
948948
}
@@ -963,7 +963,7 @@ public SqlBinary Adjust(SqlBinary value)
963963
{
964964
byte[] rgbValue = value.Value;
965965
byte[] rgbNewValue = new byte[MaxLength];
966-
Array.Copy(rgbValue, 0, rgbNewValue, 0, (int)MaxLength);
966+
Buffer.BlockCopy(rgbValue, 0, rgbNewValue, 0, (int)MaxLength);
967967
value = new SqlBinary(rgbNewValue);
968968
}
969969

@@ -1040,7 +1040,7 @@ public SqlBytes Adjust(SqlBytes value)
10401040
if (value.MaxLength < MaxLength)
10411041
{
10421042
byte[] rgbNew = new byte[MaxLength];
1043-
Array.Copy(value.Buffer, 0, rgbNew, 0, (int)oldLength);
1043+
Buffer.BlockCopy(value.Buffer, 0, rgbNew, 0, (int)oldLength);
10441044
value = new SqlBytes(rgbNew);
10451045
}
10461046

@@ -1380,7 +1380,7 @@ public byte[] Adjust(byte[] value)
13801380
if (value.Length < MaxLength)
13811381
{
13821382
byte[] rgbNewValue = new byte[MaxLength];
1383-
Array.Copy(value, 0, rgbNewValue, 0, value.Length);
1383+
Buffer.BlockCopy(value, 0, rgbNewValue, 0, value.Length);
13841384
Array.Clear(rgbNewValue, value.Length, (int)rgbNewValue.Length - value.Length);
13851385
return rgbNewValue;
13861386
}
@@ -1400,7 +1400,7 @@ public byte[] Adjust(byte[] value)
14001400
if (value.Length > MaxLength && Max != MaxLength)
14011401
{
14021402
byte[] rgbNewValue = new byte[MaxLength];
1403-
Array.Copy(value, 0, rgbNewValue, 0, (int)MaxLength);
1403+
Buffer.BlockCopy(value, 0, rgbNewValue, 0, (int)MaxLength);
14041404
value = rgbNewValue;
14051405
}
14061406

src/System.Data.SqlClient/src/System/Data/SqlClient/SNI/SNIPacket.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public SNIPacket Clone()
124124
{
125125
SNIPacket packet = new SNIPacket(null);
126126
packet._data = new byte[_length];
127-
Array.Copy(_data, 0, packet._data, 0, _length);
127+
Buffer.BlockCopy(_data, 0, packet._data, 0, _length);
128128
packet._length = _length;
129129

130130
return packet;

src/System.Data.SqlClient/src/System/Data/SqlClient/SqlDataReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1374,7 +1374,7 @@ private bool TryGetBytesInternal(int i, long dataIndex, byte[] buffer, int buffe
13741374
cbytes = length;
13751375
}
13761376

1377-
Array.Copy(data, ndataIndex, buffer, bufferIndex, cbytes);
1377+
Buffer.BlockCopy(data, ndataIndex, buffer, bufferIndex, cbytes);
13781378
}
13791379
catch (Exception e)
13801380
{

src/System.Data.SqlClient/src/System/Data/SqlClient/SqlSequentialTextReader.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,7 @@ private byte[] PrepareByteBuffer(int numberOfChars, out int byteBufferUsed)
371371
{
372372
// Otherwise, copy over the leftover buffer
373373
byteBuffer = new byte[byteBufferSize];
374-
Array.Copy(_leftOverBytes, byteBuffer, _leftOverBytes.Length);
374+
Buffer.BlockCopy(_leftOverBytes, 0, byteBuffer, 0, _leftOverBytes.Length);
375375
byteBufferUsed = _leftOverBytes.Length;
376376
}
377377
}
@@ -410,7 +410,7 @@ private int DecodeBytesToChars(byte[] inBuffer, int inBufferCount, char[] outBuf
410410
if ((!completed) && (bytesUsed < inBufferCount))
411411
{
412412
_leftOverBytes = new byte[inBufferCount - bytesUsed];
413-
Array.Copy(inBuffer, bytesUsed, _leftOverBytes, 0, _leftOverBytes.Length);
413+
Buffer.BlockCopy(inBuffer, bytesUsed, _leftOverBytes, 0, _leftOverBytes.Length);
414414
}
415415
else
416416
{

src/System.Data.SqlClient/src/System/Data/SqlClient/SqlStream.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ override public int Read(byte[] buffer, int offset, int count)
452452
cb = _cachedBytes[_currentArrayIndex].Length - _currentPosition;
453453
if (cb > count)
454454
cb = count;
455-
Array.Copy(_cachedBytes[_currentArrayIndex], _currentPosition, buffer, offset, cb);
455+
Buffer.BlockCopy(_cachedBytes[_currentArrayIndex], _currentPosition, buffer, offset, cb);
456456

457457
_currentPosition += cb;
458458
count -= (int)cb;

0 commit comments

Comments
 (0)