-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RedirectToHttpsRule: opportunity for performance improvement #28826
Comments
RedirectToHttpsRule should be updated to use UriHelper.BuildAbsolute and we can focus any optimization efforts over there. It already has a few improvements over this code. |
Yes! And I'm working on a proposal to improve the performance of that too. |
We've moved this issue to the Backlog milestone. This means that it is not going to be worked on for the coming release. We will reassess the backlog following the current release and consider this item at that time. To learn more about our issue management process and to have better expectation regarding different types of issues you can read our Triage Process. |
I think this could also be improvde: if (SSLPort.HasValue && SSLPort.Value > 0)
{
// a specific SSL port is specified
host = new HostString(host.Host, SSLPort.Value);
}
else
{
// clear the port
host = new HostString(host.Host);
} If the `SSLPort was sanitized in a constructor: public RedirectToHttpsRule(int statusCode, int? sslPort)
{
StatusCode = statusCode;
SSLPort = (sslPort.HasValue && sslPort.GetValueOrDefault() > 0) ? sslPort.GetValueOrDefault() : 0;
}
public int StatusCode{ get; }
public int SSLPort { get; } That code could become: if (SSLPort > 0)
{
// a specific SSL port is specified
host = new HostString(host.Host, SSLPort);
}
else
{
// clear the port
host = new HostString(host.Host);
} Less memory used and less processing time. |
Are there any unit tests for this? If so, where? |
Found! MiddlewareTests.cs |
Summary
While debugging some code, I noticed that
RedirectToHttpsRule
is instantiating aStringBuilder
and boxing structs on every request.Motivation and goals
This is in a hot path for applications that use redirection to HTTPS.
Detailed design
Most uses of
HostString
,PathString
andQueryString
useToString()
orToUriComponent()
to avoid boxing. This is not the case forRedirectToHttpsRule
:new StringBuilder().Append("https://").Append(host).Append(req.PathBase).Append(req.Path).Append(req.QueryString);
So, I started playing with options:
BenchmarkDotNet=v0.12.1, OS=Windows 10.0.21277
Intel Core i7-8650U CPU 1.90GHz (Kaby Lake R), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=5.0.200-preview.20614.14
[Host] : .NET Core 5.0.1 (CoreCLR 5.0.120.57516, CoreFX 5.0.120.57516), X64 RyuJIT
DefaultJob : .NET Core 5.0.1 (CoreCLR 5.0.120.57516, CoreFX 5.0.120.57516), X64 RyuJIT
string.Concat
, as it is, is not always faster or allocates less memory than usingstring.Format
without boxing.Crazy2
is the fastest option.The text was updated successfully, but these errors were encountered: