Skip to content

Commit

Permalink
Use RecyclableMemoryStream for MemoryStreams (#6371)
Browse files Browse the repository at this point in the history
* Use RecyclableMemoryStream for MemoryStreams

* Use regular MemoryStream for recorder

* leaveOpen: true

* Use for recorder also
  • Loading branch information
benaadams committed Dec 15, 2023
1 parent bce3244 commit 9bed546
Show file tree
Hide file tree
Showing 7 changed files with 159 additions and 131 deletions.
1 change: 1 addition & 0 deletions src/Nethermind/Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<PackageVersion Include="Microsoft.Extensions.Logging.Abstractions" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.Logging.Console" Version="8.0.0" />
<PackageVersion Include="Microsoft.Extensions.ObjectPool" Version="8.0.0" />
<PackageVersion Include="Microsoft.IO.RecyclableMemoryStream" Version="3.0.0" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.8.0" />
<PackageVersion Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.19.5" />
<PackageVersion Include="Nethermind.Crypto.Pairings" Version="1.0.1" />
Expand Down
1 change: 1 addition & 0 deletions src/Nethermind/Nethermind.Core/Nethermind.Core.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
<PackageReference Include="Nethermind.Numerics.Int256" />
<PackageReference Include="TestableIO.System.IO.Abstractions.Wrappers" />
<PackageReference Include="System.IdentityModel.Tokens.Jwt" />
<PackageReference Include="Microsoft.IO.RecyclableMemoryStream" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\Nethermind.Logging\Nethermind.Logging.csproj" />
Expand Down
12 changes: 12 additions & 0 deletions src/Nethermind/Nethermind.Core/Resettables/RecyclableStream.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2023 Demerzel Solutions Limited
// SPDX-License-Identifier: LGPL-3.0-only

using Microsoft.IO;

namespace Nethermind.Core.Resettables;

public class RecyclableStream
{
private static readonly RecyclableMemoryStreamManager _manager = new RecyclableMemoryStreamManager();
public static RecyclableMemoryStream GetStream(string tag) => _manager.GetStream(tag);
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// SPDX-License-Identifier: LGPL-3.0-only

using System.IO.Compression;

using Nethermind.Core.Resettables;
using Nethermind.Evm.Tracing.ParityStyle;
using Nethermind.Logging;
using Nethermind.Serialization.Json;
Expand Down Expand Up @@ -49,8 +51,8 @@ public byte[] Serialize(IReadOnlyCollection<ParityLikeTxTrace> traces)

CheckDepth(traces);

using MemoryStream output = new();
using (GZipStream compressionStream = new(output, CompressionMode.Compress))
using MemoryStream output = RecyclableStream.GetStream("Parity");
using (GZipStream compressionStream = new(output, CompressionMode.Compress, leaveOpen: true))
{
_jsonSerializer.Serialize(compressionStream, traces);
}
Expand Down
257 changes: 133 additions & 124 deletions src/Nethermind/Nethermind.JsonRpc/JsonRpcProcessor.cs

Large diffs are not rendered by default.

10 changes: 6 additions & 4 deletions src/Nethermind/Nethermind.KeyStore/AesEncrypter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;

using Nethermind.Core.Resettables;
using Nethermind.KeyStore.Config;
using Nethermind.Logging;

Expand Down Expand Up @@ -41,7 +43,7 @@ public byte[] Encrypt(byte[] content, byte[] key, byte[] iv, string cipherType)
}
case "aes-128-ctr":
{
using var outputEncryptedStream = new MemoryStream();
using var outputEncryptedStream = RecyclableStream.GetStream("aes-128-ctr-encrypt");
using var inputStream = new MemoryStream(content);
AesCtr(key, iv, inputStream, outputEncryptedStream);
outputEncryptedStream.Position = 0;
Expand Down Expand Up @@ -78,7 +80,7 @@ public byte[] Decrypt(byte[] cipher, byte[] key, byte[] iv, string cipherType)
case "aes-128-ctr":
{
using var outputEncryptedStream = new MemoryStream(cipher);
using var outputDecryptedStream = new MemoryStream();
using var outputDecryptedStream = RecyclableStream.GetStream("aes-128-ctr-decrypt");
AesCtr(key, iv, outputEncryptedStream, outputDecryptedStream);
outputDecryptedStream.Position = 0;
return outputDecryptedStream.ToArray();
Expand All @@ -96,9 +98,9 @@ public byte[] Decrypt(byte[] cipher, byte[] key, byte[] iv, string cipherType)

private byte[] Execute(ICryptoTransform cryptoTransform, byte[] data)
{
using (var memoryStream = new MemoryStream())
using (var memoryStream = RecyclableStream.GetStream(nameof(AesEncrypter)))
{
using (var cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write))
using (var cryptoStream = new CryptoStream(memoryStream, cryptoTransform, CryptoStreamMode.Write, leaveOpen: true))
{
cryptoStream.Write(data, 0, data.Length);
cryptoStream.FlushFinalBlock();
Expand Down
3 changes: 2 additions & 1 deletion src/Nethermind/Nethermind.Runner/JsonRpc/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
using Nethermind.Config;
using Nethermind.Core.Authentication;
using Nethermind.Core.Extensions;
using Nethermind.Core.Resettables;
using Nethermind.HealthChecks;
using Nethermind.JsonRpc;
using Nethermind.JsonRpc.Modules;
Expand Down Expand Up @@ -167,7 +168,7 @@ void SerializeTimeoutException(IJsonRpcService service, IBufferWriter<byte> resu
JsonRpcContext jsonRpcContext = JsonRpcContext.Http(jsonRpcUrl);
await foreach (JsonRpcResult result in jsonRpcProcessor.ProcessAsync(request, jsonRpcContext))
{
Stream stream = jsonRpcConfig.BufferResponses ? new MemoryStream() : null;
using Stream stream = jsonRpcConfig.BufferResponses ? RecyclableStream.GetStream("http") : null;
ICountingBufferWriter resultWriter = stream is not null ? new CountingStreamPipeWriter(stream) : new CountingPipeWriter(ctx.Response.BodyWriter);
try
{
Expand Down

0 comments on commit 9bed546

Please sign in to comment.