From 75e8d5ad7e471bfe0ce5524a9d35292168823502 Mon Sep 17 00:00:00 2001 From: Stephen Toub Date: Tue, 19 Jun 2018 16:51:58 -0400 Subject: [PATCH] Remove some StringBuilder-related allocations from System.Net.Http (dotnet/corefx#30508) * Remove boxing and ToString allocations from RangeHeaderValue.ToString The current code uses ToString(object), boxng the long? and then calling ToString on it. By instead first checking whether the value is null and only calling ToString(long), we avoid both the box and the string allocations, for both From and To, relying on StringBuilder's ability to format a long directly into its buffer. * Avoid StringBuilder/char[]/string allocation/copy in DumpHeaders We can just write into the existing StringBuilder rather than creating a new one, appending to that, copying it to a string, and then appending that string to the original. Commit migrated from https://github.com/dotnet/corefx/commit/8c0487bfeff9229beca93dc480028b83d8e39705 --- .../System.Net.Http/src/System/Net/Http/Headers/HeaderUtilities.cs | 7 ++----- .../src/System/Net/Http/Headers/RangeHeaderValue.cs | 4 ++-- .../System.Net.Http/src/System/Net/Http/HttpRequestMessage.cs | 2 +- .../System.Net.Http/src/System/Net/Http/HttpResponseMessage.cs | 2 +- 4 files changed, 6 insertions(+), 9 deletions(-) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HeaderUtilities.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HeaderUtilities.cs index 75a8ef8..75d77e1 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HeaderUtilities.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/HeaderUtilities.cs @@ -369,16 +369,15 @@ namespace System.Net.Http.Headers return true; } - internal static string DumpHeaders(params HttpHeaders[] headers) + internal static void DumpHeaders(StringBuilder sb, params HttpHeaders[] headers) { - // Return all headers as string similar to: + // Appends all headers as string similar to: // { // HeaderName1: Value1 // HeaderName1: Value2 // HeaderName2: Value1 // ... // } - StringBuilder sb = new StringBuilder(); sb.Append("{\r\n"); for (int i = 0; i < headers.Length; i++) @@ -400,8 +399,6 @@ namespace System.Net.Http.Headers } sb.Append('}'); - - return sb.ToString(); } internal static bool IsValidEmailAddress(string value) diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/RangeHeaderValue.cs b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/RangeHeaderValue.cs index 3034bf0..554f65c 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/Headers/RangeHeaderValue.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/Headers/RangeHeaderValue.cs @@ -82,9 +82,9 @@ namespace System.Net.Http.Headers sb.Append(", "); } - sb.Append(range.From); + if (range.From.HasValue) sb.Append(range.From.GetValueOrDefault()); sb.Append('-'); - sb.Append(range.To); + if (range.To.HasValue) sb.Append(range.To.GetValueOrDefault()); } } diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpRequestMessage.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpRequestMessage.cs index 36bf667..1920738 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpRequestMessage.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpRequestMessage.cs @@ -174,7 +174,7 @@ namespace System.Net.Http sb.Append(_content == null ? "" : _content.GetType().ToString()); sb.Append(", Headers:\r\n"); - sb.Append(HeaderUtilities.DumpHeaders(_headers, _content == null ? null : _content.Headers)); + HeaderUtilities.DumpHeaders(sb, _headers, _content?.Headers); return sb.ToString(); } diff --git a/src/libraries/System.Net.Http/src/System/Net/Http/HttpResponseMessage.cs b/src/libraries/System.Net.Http/src/System/Net/Http/HttpResponseMessage.cs index e0dd6a1..6fae4e3 100644 --- a/src/libraries/System.Net.Http/src/System/Net/Http/HttpResponseMessage.cs +++ b/src/libraries/System.Net.Http/src/System/Net/Http/HttpResponseMessage.cs @@ -182,7 +182,7 @@ namespace System.Net.Http sb.Append(_content == null ? "" : _content.GetType().ToString()); sb.Append(", Headers:\r\n"); - sb.Append(HeaderUtilities.DumpHeaders(_headers, _content == null ? null : _content.Headers)); + HeaderUtilities.DumpHeaders(sb, _headers, _content?.Headers); return sb.ToString(); } -- 2.7.4