diff --git a/src/Nethermind/Nethermind.JsonRpc/PipeReaderExtensions.cs b/src/Nethermind/Nethermind.JsonRpc/PipeReaderExtensions.cs index 44aaa377457..1f57e3b7611 100644 --- a/src/Nethermind/Nethermind.JsonRpc/PipeReaderExtensions.cs +++ b/src/Nethermind/Nethermind.JsonRpc/PipeReaderExtensions.cs @@ -3,6 +3,7 @@ using System.Buffers; using System.IO.Pipelines; +using System.Runtime.CompilerServices; using System.Threading; using System.Threading.Tasks; @@ -15,11 +16,21 @@ public static async Task ReadToEndAsync(this PipeReader reader, Canc while (true) { ReadResult result = await reader.ReadAsync(cancellationToken).ConfigureAwait(false); - ReadOnlySequence buffer = result.Buffer; if (result.IsCompleted || result.IsCanceled) { return result; } + + // Separate method to shrink the async state machine by not including + // the ReadOnlySequence buffer in the main method + AdvanceReaderToEnd(reader, in result); + } + + [MethodImpl(MethodImplOptions.NoInlining)] + static void AdvanceReaderToEnd(PipeReader reader, in ReadResult result) + { + // Extract buffer reading to a separate method to reduce async state machine size + ReadOnlySequence buffer = result.Buffer; reader.AdvanceTo(buffer.Start, buffer.End); } }