From c5a6b1284342b5cb96ff2c6dfcacd4d23e8bb7f7 Mon Sep 17 00:00:00 2001 From: Peter Butzhammer Date: Thu, 25 Apr 2024 10:24:14 +0200 Subject: [PATCH] Use ArrayPool as a write buffer --- Sharp7.Rx/Sharp7Plc.cs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/Sharp7.Rx/Sharp7Plc.cs b/Sharp7.Rx/Sharp7Plc.cs index a9e7c62..f250ad8 100644 --- a/Sharp7.Rx/Sharp7Plc.cs +++ b/Sharp7.Rx/Sharp7Plc.cs @@ -1,4 +1,5 @@ -using System.Diagnostics; +using System.Buffers; +using System.Diagnostics; using System.Reactive; using System.Reactive.Disposables; using System.Reactive.Linq; @@ -20,6 +21,7 @@ public class Sharp7Plc : IPlc private readonly CacheVariableNameParser variableNameParser = new CacheVariableNameParser(new VariableNameParser()); private bool disposed; private Sharp7Connector s7Connector; + private static readonly ArrayPool arrayPool = ArrayPool.Shared; /// @@ -159,11 +161,17 @@ public class Sharp7Plc : IPlc } else { - // TODO: Use ArrayPool.Rent() once we drop Framwework support - var bytes = new byte[address.BufferLength]; - ValueConverter.WriteToBuffer(bytes, value, address); + var buffer = arrayPool.Rent(address.BufferLength); + try + { + ValueConverter.WriteToBuffer(buffer, value, address); - await s7Connector.WriteBytes(address.Operand, address.Start, bytes, address.DbNo, token); + await s7Connector.WriteBytes(address.Operand, address.Start, buffer, address.DbNo, token); + } + finally + { + ArrayPool.Shared.Return(buffer); + } } }