From: Cory Nelson Date: Thu, 11 Jul 2019 02:55:38 +0000 (-0700) Subject: Optimize out an array allocation X-Git-Tag: submit/tizen/20210909.063632~11031^2~968^2~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=ccb94e59b7c88b8bf4e7a8f48c13979955f930cd;p=platform%2Fupstream%2Fdotnet%2Fruntime.git Optimize out an array allocation Commit migrated from https://github.com/dotnet/corefx/commit/69b57934d5fa13147b44908e5a9d9d2e01e5d9bc --- diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs index 0f7523a..78ef3c2 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/Http2Connection.cs @@ -581,8 +581,7 @@ namespace System.Net.Http // Send PING ACK // Don't wait for completion, which could happen asynchronously. - byte[] pingContent = _incomingBuffer.ActiveMemory.Slice(0, FrameHeader.PingLength).ToArray(); - LogExceptions(SendPingAckAsync(pingContent)); + LogExceptions(SendPingAckAsync(_incomingBuffer.ActiveMemory.Slice(0, FrameHeader.PingLength))); _incomingBuffer.Discard(frameHeader.Length); } @@ -792,6 +791,10 @@ namespace System.Net.Http { Debug.Assert(pingContent.Length == FrameHeader.PingLength); + // copy pingContent before we go async so the caller can + // discard their buffer without waiting for us to complete. + long pingContentLong = BitConverter.ToInt64(pingContent.Span); + Memory writeBuffer = await StartWriteAsync(FrameHeader.Size + FrameHeader.PingLength).ConfigureAwait(false); if (NetEventSource.IsEnabled) Trace("Started writing."); @@ -799,7 +802,7 @@ namespace System.Net.Http frameHeader.WriteTo(writeBuffer); writeBuffer = writeBuffer.Slice(FrameHeader.Size); - pingContent.CopyTo(writeBuffer); + BitConverter.TryWriteBytes(writeBuffer.Span, pingContentLong); FinishWrite(mustFlush: false); }