Skip to content

Commit 1f7d123

Browse files
committed
Less allocations.
1 parent 0eaf0ad commit 1f7d123

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/Version10/GdsDatabase.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -368,35 +368,35 @@ public virtual void ConnectionRequest(out int auxHandle, out string ipAddress, o
368368

369369
auxHandle = XdrStream.ReadInt32();
370370

371-
// garbage
372-
XdrStream.ReadBytes(8);
371+
var garbage1 = new byte[8];
372+
XdrStream.ReadBytes(garbage1);
373373

374374
var respLen = XdrStream.ReadInt32();
375375
respLen += respLen % 4;
376376

377-
// sin_family
378-
XdrStream.ReadBytes(2);
377+
var sin_family = new byte[2];
378+
XdrStream.ReadBytes(sin_family);
379379
respLen -= 2;
380380

381-
// sin_port
382-
var buffer = XdrStream.ReadBytes(2);
383-
portNumber = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(buffer, 0));
381+
var sin_port = new byte[2];
382+
XdrStream.ReadBytes(sin_port);
383+
portNumber = (ushort)IPAddress.NetworkToHostOrder(BitConverter.ToInt16(sin_port, 0));
384384
respLen -= 2;
385385

386386
// * The address returned by the server may be incorrect if it is behind a NAT box
387387
// * so we must use the address that was used to connect the main socket, not the
388388
// * address reported by the server.
389-
// sin_addr
390-
buffer = XdrStream.ReadBytes(4);
389+
var sin_addr = new byte[4];
390+
XdrStream.ReadBytes(sin_addr);
391391
//ipAddress = string.Format(
392392
// CultureInfo.InvariantCulture,
393393
// "{0}.{1}.{2}.{3}",
394394
// buffer[0], buffer[1], buffer[2], buffer[3]);
395395
ipAddress = _connection.IPAddress.ToString();
396396
respLen -= 4;
397397

398-
// garbage
399-
XdrStream.ReadBytes(respLen);
398+
var garbage2 = new byte[respLen];
399+
XdrStream.ReadBytes(garbage2);
400400

401401
XdrStream.ReadStatusVector();
402402
}

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/Version10/GdsEventManager.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,8 @@ public async Task WaitForEventsAsync(RemoteEvent remoteEvent)
5656
case IscCodes.op_event:
5757
var dbHandle = _database.XdrStream.ReadInt32();
5858
var buffer = _database.XdrStream.ReadBuffer();
59-
var ast = _database.XdrStream.ReadBytes(8);
59+
var ast = new byte[8];
60+
_database.XdrStream.ReadBytes(ast);
6061
var eventId = _database.XdrStream.ReadInt32();
6162

6263
remoteEvent.EventCounts(buffer);

Provider/src/FirebirdSql.Data.FirebirdClient/Client/Managed/XdrStream.cs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -334,9 +334,9 @@ private void ResetOperation()
334334

335335
#region XDR Read Methods
336336

337-
public byte[] ReadBytes(int count)
337+
public byte[] ReadBytes(byte[] buffer)
338338
{
339-
var buffer = new byte[count];
339+
var count = buffer.Length;
340340
if (count > 0)
341341
{
342342
var toRead = count;
@@ -353,9 +353,9 @@ public byte[] ReadBytes(int count)
353353
}
354354
return buffer;
355355
}
356-
public async Task<byte[]> ReadBytesAsync(int count)
356+
public async Task<byte[]> ReadBytesAsync(byte[] buffer)
357357
{
358-
var buffer = new byte[count];
358+
var count = buffer.Length;
359359
if (count > 0)
360360
{
361361
var toRead = count;
@@ -375,7 +375,8 @@ public async Task<byte[]> ReadBytesAsync(int count)
375375

376376
public byte[] ReadOpaque(int length)
377377
{
378-
var buffer = ReadBytes(length);
378+
var buffer = new byte[length];
379+
ReadBytes(buffer);
379380
var padLength = ((4 - length) & 3);
380381
if (padLength > 0)
381382
{
@@ -416,18 +417,26 @@ public short ReadInt16()
416417
return Convert.ToInt16(ReadInt32());
417418
}
418419

420+
private byte[] int32Buffer = new byte[4];
419421
public int ReadInt32()
420422
{
421-
return IPAddress.HostToNetworkOrder(BitConverter.ToInt32(ReadBytes(4), 0));
423+
Array.Clear(int32Buffer, 0, 4);
424+
ReadBytes(int32Buffer);
425+
return IPAddress.HostToNetworkOrder(BitConverter.ToInt32(int32Buffer, 0));
422426
}
423427
public async Task<int> ReadInt32Async()
424428
{
425-
return IPAddress.HostToNetworkOrder(BitConverter.ToInt32(await ReadBytesAsync(4).ConfigureAwait(false), 0));
429+
Array.Clear(int32Buffer, 0, 4);
430+
await ReadBytesAsync(int32Buffer).ConfigureAwait(false);
431+
return IPAddress.HostToNetworkOrder(BitConverter.ToInt32(int32Buffer, 0));
426432
}
427433

434+
private byte[] int64Buffer = new byte[8];
428435
public long ReadInt64()
429436
{
430-
return IPAddress.HostToNetworkOrder(BitConverter.ToInt64(ReadBytes(8), 0));
437+
Array.Clear(int64Buffer, 0, 8);
438+
ReadBytes(int64Buffer);
439+
return IPAddress.HostToNetworkOrder(BitConverter.ToInt64(int64Buffer, 0));
431440
}
432441

433442
public Guid ReadGuid()

0 commit comments

Comments
 (0)