Skip to content

Commit f4371ff

Browse files
authored
Reduces heap allocations for the some byte[] uses (#1272)
1 parent f172ac5 commit f4371ff

File tree

2 files changed

+30
-2
lines changed

2 files changed

+30
-2
lines changed

src/Renci.SshNet/Common/SshData.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -221,7 +221,7 @@ protected ushort ReadUInt16()
221221
/// <exception cref="InvalidOperationException">Attempt to read past the end of the stream.</exception>
222222
protected uint ReadUInt32()
223223
{
224-
return Pack.BigEndianToUInt32(ReadBytes(4));
224+
return _stream.ReadUInt32();
225225
}
226226

227227
/// <summary>
@@ -233,7 +233,7 @@ protected uint ReadUInt32()
233233
/// <exception cref="InvalidOperationException">Attempt to read past the end of the stream.</exception>
234234
protected ulong ReadUInt64()
235235
{
236-
return Pack.BigEndianToUInt64(ReadBytes(8));
236+
return _stream.ReadUInt64();
237237
}
238238

239239
/// <summary>

src/Renci.SshNet/Common/SshDataStream.cs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,14 @@ public BigInteger ReadBigInt()
188188
/// </returns>
189189
public uint ReadUInt32()
190190
{
191+
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
192+
Span<byte> span = stackalloc byte[4];
193+
ReadBytes(span);
194+
return System.Buffers.Binary.BinaryPrimitives.ReadUInt32BigEndian(span);
195+
#else
191196
var data = ReadBytes(4);
192197
return Pack.BigEndianToUInt32(data);
198+
#endif // NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
193199
}
194200

195201
/// <summary>
@@ -200,8 +206,14 @@ public uint ReadUInt32()
200206
/// </returns>
201207
public ulong ReadUInt64()
202208
{
209+
#if NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
210+
Span<byte> span = stackalloc byte[8];
211+
ReadBytes(span);
212+
return System.Buffers.Binary.BinaryPrimitives.ReadUInt64BigEndian(span);
213+
#else
203214
var data = ReadBytes(8);
204215
return Pack.BigEndianToUInt64(data);
216+
#endif // NETSTANDARD2_1_OR_GREATER || NET6_0_OR_GREATER
205217
}
206218

207219
/// <summary>
@@ -264,5 +276,21 @@ private byte[] ReadBytes(int length)
264276

265277
return data;
266278
}
279+
280+
#if NETSTANDARD2_1 || NET6_0_OR_GREATER
281+
/// <summary>
282+
/// Reads data into the specified <paramref name="buffer" />.
283+
/// </summary>
284+
/// <param name="buffer">The buffer to read into.</param>
285+
/// <exception cref="ArgumentOutOfRangeException"><paramref name="buffer"/> is larger than the total of bytes available.</exception>
286+
private void ReadBytes(Span<byte> buffer)
287+
{
288+
var bytesRead = Read(buffer);
289+
if (bytesRead < buffer.Length)
290+
{
291+
throw new ArgumentOutOfRangeException(nameof(buffer), string.Format(CultureInfo.InvariantCulture, "The requested length ({0}) is greater than the actual number of bytes read ({1}).", buffer.Length, bytesRead));
292+
}
293+
}
294+
#endif // NETSTANDARD2_1 || NET6_0_OR_GREATER
267295
}
268296
}

0 commit comments

Comments
 (0)