Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use RecyclableMemoryStream for MemoryStreams #6371

Merged
merged 4 commits into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -168,7 +169,7 @@ void SerializeTimeoutException(IJsonRpcService service, IBufferWriter<byte> resu
long totalResponseSize = 0;
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