Skip to content

Caching common HttpResults types #40965

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

Merged
merged 19 commits into from
Apr 8, 2022
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<Description>ASP.NET Core types that implement Microsoft.AspNetCore.Http.IResult.</Description>
Expand All @@ -17,6 +17,11 @@
<Compile Include="$(SharedSourceRoot)ResultsHelpers\*.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)RangeHelper\RangeHelper.cs" LinkBase="Shared" />
<Compile Include="$(SharedSourceRoot)ProblemDetailsDefaults.cs" LinkBase="Shared" />
<Compile Update="ResultsCache.StatusCodes.cs">
<DesignTime>True</DesignTime>
<AutoGen>True</AutoGen>
<DependentUpon>ResultsCache.StatusCodes.tt</DependentUpon>
</Compile>
</ItemGroup>

<ItemGroup>
Expand All @@ -28,4 +33,16 @@
<InternalsVisibleTo Include="Microsoft.AspNetCore.Http.Results.Tests" />
</ItemGroup>

<ItemGroup>
<None Update="ResultsCache.StatusCodes.tt">
<Generator>TextTemplatingFileGenerator</Generator>
<LastGenOutput>ResultsCache.StatusCodes.cs</LastGenOutput>
</None>
</ItemGroup>

<ItemGroup>
<!-- This is the T4 template service and is added by VS anytime you modify a T4 template. Required for .tt files. -->
<Service Include="{508349b6-6b84-4df5-91f0-309beebad82d}" />
Copy link
Member

Choose a reason for hiding this comment

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

ew, but OK 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

I don't like it too, and I can actually remove but it will come back, and the next dev will need to remove it again. If that is fine i would prefer to remove it, including the Compile/None items added by VS.

</ItemGroup>

</Project>
16 changes: 8 additions & 8 deletions src/Http/Http.Results/src/Results.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,61 +495,61 @@ public static IResult RedirectToRoute(string? routeName = null, object? routeVal
/// <param name="statusCode">The status code to set on the response.</param>
/// <returns>The created <see cref="StatusCodeHttpResult"/> object for the response.</returns>
public static IResult StatusCode(int statusCode)
=> new StatusCodeHttpResult(statusCode);
=> ResultsCache.StatusCode(statusCode);

/// <summary>
/// Produces a <see cref="StatusCodes.Status404NotFound"/> response.
/// </summary>
/// <param name="value">The value to be included in the HTTP response body.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
public static IResult NotFound(object? value = null)
=> new NotFoundObjectHttpResult(value);
=> value is null ? ResultsCache.NotFound : new NotFoundObjectHttpResult(value);

/// <summary>
/// Produces a <see cref="StatusCodes.Status401Unauthorized"/> response.
Copy link
Contributor

Choose a reason for hiding this comment

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

Doesn't this actually return a cached UnauthorizedHttpResult instance❔ Same for other special cases in src/Http/Http.Results/src/ResultsCache.cs e.g. the NotFoundObjectHttpResult just above.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sorry, I am confusing about your question. This will return a cached NotFoundObjectHttpResult not UnauthorizedHttpResult. Maybe I am missing something.

Copy link
Contributor

Choose a reason for hiding this comment

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

My comment is about the <summary/> after the overload that returns NotFoundObjectHttpResult. I probably should have commented on that earlier overload.

In any case, I was confused and thought the <summary/> was about the return type. StatusCodes.Status404NotFound, StatusCodes.Status401Unauthorized, et cetera are of course ints not IResults.

In retrospect, my suggestion should have been to make the return type (NotFoundObjectHttpResult, UnauthorizedHttpResult, et cetera) clear as well.

Copy link
Member Author

@brunolins16 brunolins16 Apr 7, 2022

Choose a reason for hiding this comment

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

I agree about the return type but that is a problem that we have right now because this class was shipped already but we are working on it #41009

Copy link
Contributor

Choose a reason for hiding this comment

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

My suggestion was about the doc comments, not the method signature e.g.

Suggested change
/// Produces a <see cref="StatusCodes.Status401Unauthorized"/> response.
/// Returns an <see cref="UnauthorizedHttpResult"/> that produces a <see cref="StatusCodes.Status401Unauthorized"/> response.

Again, this is a suggestion and could be done later. You're not otherwise changing return types or updating <summary/> elements in this PR.

/// </summary>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
public static IResult Unauthorized()
=> new UnauthorizedHttpResult();
=> ResultsCache.Unauthorized;

/// <summary>
/// Produces a <see cref="StatusCodes.Status400BadRequest"/> response.
/// </summary>
/// <param name="error">An error object to be included in the HTTP response body.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
public static IResult BadRequest(object? error = null)
=> new BadRequestObjectHttpResult(error);
=> error is null ? ResultsCache.BadRequest : new BadRequestObjectHttpResult(error);

/// <summary>
/// Produces a <see cref="StatusCodes.Status409Conflict"/> response.
/// </summary>
/// <param name="error">An error object to be included in the HTTP response body.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
public static IResult Conflict(object? error = null)
=> new ConflictObjectHttpResult(error);
=> error is null ? ResultsCache.Conflict : new ConflictObjectHttpResult(error);

/// <summary>
/// Produces a <see cref="StatusCodes.Status204NoContent"/> response.
/// </summary>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
public static IResult NoContent()
=> new NoContentHttpResult();
=> ResultsCache.NoContent;

/// <summary>
/// Produces a <see cref="StatusCodes.Status200OK"/> response.
/// </summary>
/// <param name="value">The value to be included in the HTTP response body.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
public static IResult Ok(object? value = null)
=> new OkObjectHttpResult(value);
=> value is null ? ResultsCache.Ok : new OkObjectHttpResult(value);

/// <summary>
/// Produces a <see cref="StatusCodes.Status422UnprocessableEntity"/> response.
/// </summary>
/// <param name="error">An error object to be included in the HTTP response body.</param>
/// <returns>The created <see cref="IResult"/> for the response.</returns>
public static IResult UnprocessableEntity(object? error = null)
=> new UnprocessableEntityObjectHttpResult(error);
=> error is null ? ResultsCache.UnprocessableEntity : new UnprocessableEntityObjectHttpResult(error);

/// <summary>
/// Produces a <see cref="ProblemDetails"/> response.
Expand Down
154 changes: 154 additions & 0 deletions src/Http/Http.Results/src/ResultsCache.StatusCodes.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// <auto-generated />

#nullable enable

namespace Microsoft.AspNetCore.Http;

using System.CodeDom.Compiler;
using System.Runtime.CompilerServices;

[GeneratedCode("TextTemplatingFileGenerator", "")]
internal partial class ResultsCache
{
private static StatusCodeHttpResult? _status100Continue;
Copy link
Member

Choose a reason for hiding this comment

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

Should these be static readonlys

Suggested change
private static StatusCodeHttpResult? _status100Continue;
private static readonly StatusCodeHttpResult? s_status100Continue = new(StatusCodes.Status100Continue);

And in the switch below

        return statusCode switch
        {
            StatusCodes.Status100Continue => s_status100Continue,
            ...

That way the lazy-initialization is shifted to the runtime, and by beforefieldinit it's done on first access.

With tiered-compilation hot StatusCodeHttpResults could already be initialized, so no extra (runtime-) check is needed anymore.
Otherwise it's pretty the same if there's a ?? new(StatusCodes.XYZ) or if that check is done by the runtime (I guess).


Of course the change in the tt-file below.

Copy link
Member Author

Choose a reason for hiding this comment

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

@gfoidl I really appreciate your comment. I did not try the tiered-compilation thing yet however the beforefieldinit indeed do a lazy-initialization but based on my tests when the initialization happens all the ~ 60 fields are initialized and all memory is allocated. The performance improvement is minimum compared with the null check approach.

Initial allocation cost (First time access)

Method Gen 0 Gen 1 Allocated
StaticCacheWithSwitchExpression 0.0095 - 2,000 B
DynamicCacheWithSwitchExpression 0.0001 - 24 B
Method Mean Error StdDev Gen 0 Allocated
StaticCacheWithSwitchExpression 1.867 ns 0.0045 ns 0.0042 ns - -
DynamicCacheWithSwitchExpression 1.889 ns 0.0143 ns 0.0119 ns - -

Copy link
Member

Choose a reason for hiding this comment

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

Drop _status100Continue, that's a response code that's only ever returned by the server, not the app.

private static StatusCodeHttpResult? _status101SwitchingProtocols;
private static StatusCodeHttpResult? _status102Processing;
private static StatusCodeHttpResult? _status200OK;
private static StatusCodeHttpResult? _status201Created;
private static StatusCodeHttpResult? _status202Accepted;
private static StatusCodeHttpResult? _status203NonAuthoritative;
private static StatusCodeHttpResult? _status204NoContent;
private static StatusCodeHttpResult? _status205ResetContent;
private static StatusCodeHttpResult? _status206PartialContent;
private static StatusCodeHttpResult? _status207MultiStatus;
private static StatusCodeHttpResult? _status208AlreadyReported;
private static StatusCodeHttpResult? _status226IMUsed;
private static StatusCodeHttpResult? _status300MultipleChoices;
private static StatusCodeHttpResult? _status301MovedPermanently;
private static StatusCodeHttpResult? _status302Found;
private static StatusCodeHttpResult? _status303SeeOther;
private static StatusCodeHttpResult? _status304NotModified;
private static StatusCodeHttpResult? _status305UseProxy;
private static StatusCodeHttpResult? _status306SwitchProxy;
private static StatusCodeHttpResult? _status307TemporaryRedirect;
private static StatusCodeHttpResult? _status308PermanentRedirect;
private static StatusCodeHttpResult? _status400BadRequest;
private static StatusCodeHttpResult? _status401Unauthorized;
private static StatusCodeHttpResult? _status402PaymentRequired;
private static StatusCodeHttpResult? _status403Forbidden;
private static StatusCodeHttpResult? _status404NotFound;
private static StatusCodeHttpResult? _status405MethodNotAllowed;
private static StatusCodeHttpResult? _status406NotAcceptable;
private static StatusCodeHttpResult? _status407ProxyAuthenticationRequired;
private static StatusCodeHttpResult? _status408RequestTimeout;
private static StatusCodeHttpResult? _status409Conflict;
private static StatusCodeHttpResult? _status410Gone;
private static StatusCodeHttpResult? _status411LengthRequired;
private static StatusCodeHttpResult? _status412PreconditionFailed;
private static StatusCodeHttpResult? _status413RequestEntityTooLarge;
private static StatusCodeHttpResult? _status414RequestUriTooLong;
private static StatusCodeHttpResult? _status415UnsupportedMediaType;
private static StatusCodeHttpResult? _status416RequestedRangeNotSatisfiable;
private static StatusCodeHttpResult? _status417ExpectationFailed;
private static StatusCodeHttpResult? _status418ImATeapot;
private static StatusCodeHttpResult? _status419AuthenticationTimeout;
private static StatusCodeHttpResult? _status421MisdirectedRequest;
private static StatusCodeHttpResult? _status422UnprocessableEntity;
private static StatusCodeHttpResult? _status423Locked;
private static StatusCodeHttpResult? _status424FailedDependency;
private static StatusCodeHttpResult? _status426UpgradeRequired;
private static StatusCodeHttpResult? _status428PreconditionRequired;
private static StatusCodeHttpResult? _status429TooManyRequests;
private static StatusCodeHttpResult? _status431RequestHeaderFieldsTooLarge;
private static StatusCodeHttpResult? _status451UnavailableForLegalReasons;
private static StatusCodeHttpResult? _status500InternalServerError;
private static StatusCodeHttpResult? _status501NotImplemented;
private static StatusCodeHttpResult? _status502BadGateway;
private static StatusCodeHttpResult? _status503ServiceUnavailable;
private static StatusCodeHttpResult? _status504GatewayTimeout;
private static StatusCodeHttpResult? _status505HttpVersionNotsupported;
private static StatusCodeHttpResult? _status506VariantAlsoNegotiates;
private static StatusCodeHttpResult? _status507InsufficientStorage;
private static StatusCodeHttpResult? _status508LoopDetected;
private static StatusCodeHttpResult? _status510NotExtended;
private static StatusCodeHttpResult? _status511NetworkAuthenticationRequired;

internal static StatusCodeHttpResult StatusCode(int statusCode)
{
if ( statusCode is (< 100) or (> 511))
Copy link
Member

Choose a reason for hiding this comment

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

PerfectionFlawlessGIF

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if ( statusCode is (< 100) or (> 511))
if (statusCode is (< 100) or (> 511))

Copy link
Member

Choose a reason for hiding this comment

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

For reference: Perf-goodness will come for free with dotnet/roslyn#60534

{
// No HTTP status code assigned outside the 100..599 range
// so, it will not be available in the cache
return new StatusCodeHttpResult(statusCode);
}

return statusCode switch
{
StatusCodes.Status100Continue => _status100Continue ??= new(StatusCodes.Status100Continue),
StatusCodes.Status101SwitchingProtocols => _status101SwitchingProtocols ??= new(StatusCodes.Status101SwitchingProtocols),
StatusCodes.Status102Processing => _status102Processing ??= new(StatusCodes.Status102Processing),
StatusCodes.Status200OK => _status200OK ??= new(StatusCodes.Status200OK),
StatusCodes.Status201Created => _status201Created ??= new(StatusCodes.Status201Created),
StatusCodes.Status202Accepted => _status202Accepted ??= new(StatusCodes.Status202Accepted),
StatusCodes.Status203NonAuthoritative => _status203NonAuthoritative ??= new(StatusCodes.Status203NonAuthoritative),
StatusCodes.Status204NoContent => _status204NoContent ??= new(StatusCodes.Status204NoContent),
StatusCodes.Status205ResetContent => _status205ResetContent ??= new(StatusCodes.Status205ResetContent),
StatusCodes.Status206PartialContent => _status206PartialContent ??= new(StatusCodes.Status206PartialContent),
StatusCodes.Status207MultiStatus => _status207MultiStatus ??= new(StatusCodes.Status207MultiStatus),
StatusCodes.Status208AlreadyReported => _status208AlreadyReported ??= new(StatusCodes.Status208AlreadyReported),
StatusCodes.Status226IMUsed => _status226IMUsed ??= new(StatusCodes.Status226IMUsed),
StatusCodes.Status300MultipleChoices => _status300MultipleChoices ??= new(StatusCodes.Status300MultipleChoices),
StatusCodes.Status301MovedPermanently => _status301MovedPermanently ??= new(StatusCodes.Status301MovedPermanently),
StatusCodes.Status302Found => _status302Found ??= new(StatusCodes.Status302Found),
StatusCodes.Status303SeeOther => _status303SeeOther ??= new(StatusCodes.Status303SeeOther),
StatusCodes.Status304NotModified => _status304NotModified ??= new(StatusCodes.Status304NotModified),
StatusCodes.Status305UseProxy => _status305UseProxy ??= new(StatusCodes.Status305UseProxy),
StatusCodes.Status306SwitchProxy => _status306SwitchProxy ??= new(StatusCodes.Status306SwitchProxy),
StatusCodes.Status307TemporaryRedirect => _status307TemporaryRedirect ??= new(StatusCodes.Status307TemporaryRedirect),
StatusCodes.Status308PermanentRedirect => _status308PermanentRedirect ??= new(StatusCodes.Status308PermanentRedirect),
StatusCodes.Status400BadRequest => _status400BadRequest ??= new(StatusCodes.Status400BadRequest),
StatusCodes.Status401Unauthorized => _status401Unauthorized ??= new(StatusCodes.Status401Unauthorized),
StatusCodes.Status402PaymentRequired => _status402PaymentRequired ??= new(StatusCodes.Status402PaymentRequired),
StatusCodes.Status403Forbidden => _status403Forbidden ??= new(StatusCodes.Status403Forbidden),
StatusCodes.Status404NotFound => _status404NotFound ??= new(StatusCodes.Status404NotFound),
StatusCodes.Status405MethodNotAllowed => _status405MethodNotAllowed ??= new(StatusCodes.Status405MethodNotAllowed),
StatusCodes.Status406NotAcceptable => _status406NotAcceptable ??= new(StatusCodes.Status406NotAcceptable),
StatusCodes.Status407ProxyAuthenticationRequired => _status407ProxyAuthenticationRequired ??= new(StatusCodes.Status407ProxyAuthenticationRequired),
StatusCodes.Status408RequestTimeout => _status408RequestTimeout ??= new(StatusCodes.Status408RequestTimeout),
StatusCodes.Status409Conflict => _status409Conflict ??= new(StatusCodes.Status409Conflict),
StatusCodes.Status410Gone => _status410Gone ??= new(StatusCodes.Status410Gone),
StatusCodes.Status411LengthRequired => _status411LengthRequired ??= new(StatusCodes.Status411LengthRequired),
StatusCodes.Status412PreconditionFailed => _status412PreconditionFailed ??= new(StatusCodes.Status412PreconditionFailed),
StatusCodes.Status413RequestEntityTooLarge => _status413RequestEntityTooLarge ??= new(StatusCodes.Status413RequestEntityTooLarge),
StatusCodes.Status414RequestUriTooLong => _status414RequestUriTooLong ??= new(StatusCodes.Status414RequestUriTooLong),
StatusCodes.Status415UnsupportedMediaType => _status415UnsupportedMediaType ??= new(StatusCodes.Status415UnsupportedMediaType),
StatusCodes.Status416RequestedRangeNotSatisfiable => _status416RequestedRangeNotSatisfiable ??= new(StatusCodes.Status416RequestedRangeNotSatisfiable),
StatusCodes.Status417ExpectationFailed => _status417ExpectationFailed ??= new(StatusCodes.Status417ExpectationFailed),
StatusCodes.Status418ImATeapot => _status418ImATeapot ??= new(StatusCodes.Status418ImATeapot),
StatusCodes.Status419AuthenticationTimeout => _status419AuthenticationTimeout ??= new(StatusCodes.Status419AuthenticationTimeout),
StatusCodes.Status421MisdirectedRequest => _status421MisdirectedRequest ??= new(StatusCodes.Status421MisdirectedRequest),
StatusCodes.Status422UnprocessableEntity => _status422UnprocessableEntity ??= new(StatusCodes.Status422UnprocessableEntity),
StatusCodes.Status423Locked => _status423Locked ??= new(StatusCodes.Status423Locked),
StatusCodes.Status424FailedDependency => _status424FailedDependency ??= new(StatusCodes.Status424FailedDependency),
StatusCodes.Status426UpgradeRequired => _status426UpgradeRequired ??= new(StatusCodes.Status426UpgradeRequired),
StatusCodes.Status428PreconditionRequired => _status428PreconditionRequired ??= new(StatusCodes.Status428PreconditionRequired),
StatusCodes.Status429TooManyRequests => _status429TooManyRequests ??= new(StatusCodes.Status429TooManyRequests),
StatusCodes.Status431RequestHeaderFieldsTooLarge => _status431RequestHeaderFieldsTooLarge ??= new(StatusCodes.Status431RequestHeaderFieldsTooLarge),
StatusCodes.Status451UnavailableForLegalReasons => _status451UnavailableForLegalReasons ??= new(StatusCodes.Status451UnavailableForLegalReasons),
StatusCodes.Status500InternalServerError => _status500InternalServerError ??= new(StatusCodes.Status500InternalServerError),
StatusCodes.Status501NotImplemented => _status501NotImplemented ??= new(StatusCodes.Status501NotImplemented),
StatusCodes.Status502BadGateway => _status502BadGateway ??= new(StatusCodes.Status502BadGateway),
StatusCodes.Status503ServiceUnavailable => _status503ServiceUnavailable ??= new(StatusCodes.Status503ServiceUnavailable),
StatusCodes.Status504GatewayTimeout => _status504GatewayTimeout ??= new(StatusCodes.Status504GatewayTimeout),
StatusCodes.Status505HttpVersionNotsupported => _status505HttpVersionNotsupported ??= new(StatusCodes.Status505HttpVersionNotsupported),
StatusCodes.Status506VariantAlsoNegotiates => _status506VariantAlsoNegotiates ??= new(StatusCodes.Status506VariantAlsoNegotiates),
StatusCodes.Status507InsufficientStorage => _status507InsufficientStorage ??= new(StatusCodes.Status507InsufficientStorage),
StatusCodes.Status508LoopDetected => _status508LoopDetected ??= new(StatusCodes.Status508LoopDetected),
StatusCodes.Status510NotExtended => _status510NotExtended ??= new(StatusCodes.Status510NotExtended),
StatusCodes.Status511NetworkAuthenticationRequired => _status511NetworkAuthenticationRequired ??= new(StatusCodes.Status511NetworkAuthenticationRequired),
_ => new StatusCodeHttpResult(statusCode),
};
}
}
Loading