-
-
Notifications
You must be signed in to change notification settings - Fork 14
Faster Guard APIs #7
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
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
cd23ea6
Added ThrowHelper type
Sergio0694 d81731e
Updated the Guard APIs
Sergio0694 e61bb6f
Removed unnecessary type constraints
Sergio0694 9eccb6a
Added specific int/long Guard APIs
Sergio0694 96bf1ef
Merge branch 'master' into sp/faster-guard-apis
JimBobSquarePants cc399f9
Added T4 template for Guard APIs
Sergio0694 64faeab
Fix T4, Add InliningOptions, Fix some tests.
JimBobSquarePants 2cbd2bc
Fix tests, remove InliningOptions
JimBobSquarePants File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -286,3 +286,4 @@ __pycache__/ | |
| *.btm.cs | ||
| *.odx.cs | ||
| *.xsd.cs | ||
| src/SharedInfrastructure/Guard.Numeric.cs | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| // Copyright (c) Six Labors and contributors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
|
|
||
| // Uncomment this for verbose profiler results. DO NOT PUSH TO MAIN! | ||
| // #define PROFILING | ||
| using System.Runtime.CompilerServices; | ||
|
|
||
| namespace SixLabors | ||
| { | ||
| /// <summary> | ||
| /// Global inlining options. Helps temporarily disable inlining for better profiler output. | ||
| /// </summary> | ||
| internal static class InliningOptions | ||
| { | ||
| #if PROFILING | ||
| public const MethodImplOptions HotPath = MethodImplOptions.NoInlining; | ||
| public const MethodImplOptions ShortMethod = MethodImplOptions.NoInlining; | ||
| #else | ||
| #if SUPPORTS_HOTPATH | ||
| public const MethodImplOptions HotPath = MethodImplOptions.AggressiveOptimization; | ||
| #else | ||
| public const MethodImplOptions HotPath = MethodImplOptions.AggressiveInlining; | ||
| #endif | ||
| public const MethodImplOptions ShortMethod = MethodImplOptions.AggressiveInlining; | ||
| #endif | ||
| public const MethodImplOptions ColdPath = MethodImplOptions.NoInlining; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,13 +1,12 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> | ||
| <PropertyGroup Label="Globals"> | ||
| <ProjectGuid>68a8cc40-6aed-4e96-b524-31b1158fdeea</ProjectGuid> | ||
| <MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion> | ||
| </PropertyGroup> | ||
| <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
| <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" /> | ||
| <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" /> | ||
| <PropertyGroup /> | ||
| <Import Project="SharedInfrastructure.projitems" Label="Shared" /> | ||
| <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" /> | ||
| <PropertyGroup Label="Globals"> | ||
| <ProjectGuid>68a8cc40-6aed-4e96-b524-31b1158fdeea</ProjectGuid> | ||
| <MinimumVisualStudioVersion>14.0</MinimumVisualStudioVersion> | ||
| </PropertyGroup> | ||
| <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> | ||
| <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.Default.props" /> | ||
| <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.Common.props" /> | ||
| <Import Project="SharedInfrastructure.projitems" Label="Shared" /> | ||
| <Import Project="$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)\CodeSharing\Microsoft.CodeSharing.CSharp.targets" /> | ||
| </Project> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JimBobSquarePants Possibly dumb quesiton, why are we just using
MethodImplOptions.AggressiveOptimizationin this case instead ofMethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimization? I've seen them used at the same time quote often around CoreCLR, and I don't think that the optimization flag implies inlining as well - don't we want both those JIT perks for hot paths? 🤔Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've only seen them used as flags
MethodImplOptions.AggressiveInlining | MethodImplOptions.AggressiveOptimizationI haven't managed to find any documentation on the new attribute at all, nor could I find the issue that introduced it so I thought I better keep them separate as the original enum defines.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe we should combine them. @antonfirsov Do you have any insight?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found the original CoreFX issue.
dotnet/runtime#27370
Dunno why I couldn't before.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Sergio0694 Looks like you definitely want to keep them separate. Relevant conversation starts here.
dotnet/runtime#27370 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JimBobSquarePants I'm not sure what you mean there, what exception message? I mean, I can see the throw methods for the out of range errors with a formatted message already, what are you referring to here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Sergio0694 I suppose inlining is not the only magic provided by
AggressiveOptimization, but you're rightGuardmethods are so short, there really isn't anything to agressively optimize, and if you say it might bring other risks, let's leave it out for now!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not 100% sure since that conversation was really long and I could just read up to some point, but apparently there were a lot of various implications depending on the scenario. The main takeaway I got was this:
•
AggressiveInliningjust pushes the JIT to inline the (marked) callee into its caller•
AggressiveOptimizationpushes the JIT to try to inline callees from their (marked) caller (the one with the attribute). It also suggests to go straight to the best JIT tiering possible.In out case, the
GuardAPIs don't call functions that could benefit from inlinig, in fact all the helpers should not be inlined, and are marked as such. We only care about thoseGuardAPIs themselves being inlined into their callers, so yeah I'd say (at least for now) the safe bet would be to just useAggressiveInlining👍There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Sergio0694 What I mean is that the unit tests expect a different value for the message than the actual output. Shall I just update the test to match?
https://github.com/SixLabors/SharedInfrastructure/pull/7/files#diff-f73475a089d950d0e0a672480a854ebcR45
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah my bad, I didn't consider the fact the tests were also matching the exact error message as well. Sure thing, yeah that would be perfect then 👍