Skip to content
This repository was archived by the owner on Nov 20, 2018. It is now read-only.
Next Next commit
do not throw in GetDisplayUrl() if one of the data parts is null
  • Loading branch information
filipw committed Oct 30, 2018
commit 8c57f334538796c849a7abc07e57a1fc4d11b26a
4 changes: 2 additions & 2 deletions src/Microsoft.AspNetCore.Http.Extensions/UriHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,8 @@ public static string GetDisplayUrl(this HttpRequest request)
var queryString = request.QueryString.Value;

// PERF: Calculate string length to allocate correct buffer size for StringBuilder.
var length = request.Scheme.Length + SchemeDelimiter.Length + host.Length
+ pathBase.Length + path.Length + queryString.Length;
var length = request.Scheme?.Length ?? 0 + SchemeDelimiter.Length + host?.Length ?? 0
+ pathBase?.Length ?? 0 + path?.Length ?? 0 + queryString?.Length ?? 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That’s a lot of null checks in a calculation that is done for “PERF” reasons 😕

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you are right I improved the code and it should be better now, there appears to be no impact at all on perf

BenchmarkDotNet=v0.11.1, OS=Windows 10.0.17134.345 (1803/April2018Update/Redstone4) Intel Core i7-8850H CPU 2.60GHz (Max: 2.59GHz) (Coffee Lake), 1 CPU, 6 logical and 6 physical cores .NET Core SDK=2.2.100-preview2-009404 [Host] : .NET Core 2.1.5 (CoreCLR 4.6.26919.02, CoreFX 4.6.26919.02), 64bit RyuJIT DefaultJob : .NET Core 2.1.5 (CoreCLR 4.6.26919.02, CoreFX 4.6.26919.02), 64bit RyuJIT Method | Mean | Error | StdDev | Median | ---------------------- |---------:|---------:|----------:|---------:| GetDisplayUrlOld | 895.2 ns | 39.06 ns | 112.68 ns | 832.6 ns | GetDisplayUrlNew | 854.9 ns | 20.18 ns | 52.08 ns | 840.2 ns | 

return new StringBuilder(length)
.Append(request.Scheme)
Expand Down