Remove some StringBuilder-related allocations from System.Net.Http (dotnet/corefx...
authorStephen Toub <stoub@microsoft.com>
Tue, 19 Jun 2018 20:51:58 +0000 (16:51 -0400)
committerGitHub <noreply@github.com>
Tue, 19 Jun 2018 20:51:58 +0000 (16:51 -0400)
* 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

src/libraries/System.Net.Http/src/System/Net/Http/Headers/HeaderUtilities.cs
src/libraries/System.Net.Http/src/System/Net/Http/Headers/RangeHeaderValue.cs
src/libraries/System.Net.Http/src/System/Net/Http/HttpRequestMessage.cs
src/libraries/System.Net.Http/src/System/Net/Http/HttpResponseMessage.cs

index 75a8ef8..75d77e1 100644 (file)
@@ -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)
index 3034bf0..554f65c 100644 (file)
@@ -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());
                 }
             }
 
index 36bf667..1920738 100644 (file)
@@ -174,7 +174,7 @@ namespace System.Net.Http
             sb.Append(_content == null ? "<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();
         }
index e0dd6a1..6fae4e3 100644 (file)
@@ -182,7 +182,7 @@ namespace System.Net.Http
             sb.Append(_content == null ? "<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();
         }