Skip to content

Commit aa56491

Browse files
author
Ahmad Noman Musleh
committed
To avoid too many array allocations now client uses array pool in TCP read method
1 parent 1f73b53 commit aa56491

2 files changed

Lines changed: 8 additions & 4 deletions

File tree

src/OpenAPI.Net/OpenAPI.Net.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<PackageTags>cTrader, Open API, Spotware</PackageTags>
1010
<Description>A .NET RX library for Spotware Open API</Description>
1111
<PackageId>Spotware.OpenAPI.Net</PackageId>
12-
<Version>1.3.9</Version>
12+
<Version>1.4.0-rc0</Version>
1313
<Platforms>AnyCPU</Platforms>
1414
<Company>Spotware</Company>
1515
<Authors>Spotware</Authors>

src/OpenAPI.Net/OpenClient.cs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
using Websocket.Client;
1414
using System.Net.WebSockets;
1515
using System.Threading.Channels;
16+
using System.Buffers;
1617

1718
namespace OpenAPI.Net
1819
{
@@ -376,11 +377,12 @@ private async Task StartSendingMessages(CancellationToken cancellationToken)
376377
/// <returns>Task</returns>
377378
private async void ReadTcp(CancellationToken cancellationToken)
378379
{
380+
var lengthArray = new byte[sizeof(int)];
381+
379382
try
380383
{
381384
while (!IsDisposed)
382385
{
383-
var lengthArray = new byte[sizeof(int)];
384386

385387
var readBytes = 0;
386388

@@ -400,7 +402,7 @@ private async void ReadTcp(CancellationToken cancellationToken)
400402

401403
if (length <= 0) continue;
402404

403-
var data = new byte[length];
405+
var data = ArrayPool<byte>.Shared.Rent(length);
404406

405407
readBytes = 0;
406408

@@ -414,7 +416,9 @@ private async void ReadTcp(CancellationToken cancellationToken)
414416
}
415417
while (readBytes < length);
416418

417-
var message = ProtoMessage.Parser.ParseFrom(data);
419+
var message = ProtoMessage.Parser.ParseFrom(data, 0, length);
420+
421+
ArrayPool<byte>.Shared.Return(data);
418422

419423
OnNext(message);
420424
}

0 commit comments

Comments
 (0)