-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Optimize HTTP method lookup in routing jumptable #51803
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 all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
84c7543
47569 - Using FrozenDictionary in HttpMethodDictionaryPolicyJumpTable
55b547b
OrdinalIgnoreCase is not seem to be preserved by ToFrozenDictionary m…
40102b1
Using switch expression to find the destination of known HTTP Methods
114458f
Review feedback
1233cb2
Updating benchmarks
c6424fc
Cleaning up unused code
37e50c8
Refactor HttpMethodDestinationsLookup
JamesNK 9d68c3f
Merge pull request #198 from dotnet/jamesnk/known-methods-refactor
ladeak 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
32 changes: 32 additions & 0 deletions
32
src/Http/Routing/perf/Microbenchmarks/Matching/HttpMethodMatcherPolicyBenchmark.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using BenchmarkDotNet.Attributes; | ||
| using Microsoft.AspNetCore.Http; | ||
|
|
||
| namespace Microsoft.AspNetCore.Routing.Matching; | ||
|
|
||
| public class HttpMethodMatcherPolicyBenchmark | ||
| { | ||
| private static string[] TestHttpMethods = ["*", HttpMethods.Get, HttpMethods.Connect, HttpMethods.Delete, HttpMethods.Head, HttpMethods.Options, HttpMethods.Patch, HttpMethods.Put, HttpMethods.Post, HttpMethods.Trace, "MERGE"]; | ||
| private HttpMethodMatcherPolicy _jumpTableBuilder = new(); | ||
| private List<PolicyJumpTableEdge> _edges = new(); | ||
|
|
||
| [Params(3, 5, 11)] | ||
| public int DestinationCount { get; set; } | ||
|
|
||
| [GlobalSetup] | ||
| public void Setup() | ||
| { | ||
| for (int i = 0; i < DestinationCount; i++) | ||
| { | ||
| _edges.Add(new PolicyJumpTableEdge(new HttpMethodMatcherPolicy.EdgeKey(TestHttpMethods[i], false), i + 1)); | ||
| } | ||
| } | ||
|
|
||
| [Benchmark] | ||
| public PolicyJumpTable BuildJumpTable() | ||
| { | ||
| return _jumpTableBuilder.BuildJumpTable(1, _edges); | ||
| } | ||
| } |
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
131 changes: 131 additions & 0 deletions
131
src/Http/Routing/src/Matching/HttpMethodDestinationsLookup.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,131 @@ | ||
| // Licensed to the .NET Foundation under one or more agreements. | ||
| // The .NET Foundation licenses this file to you under the MIT license. | ||
|
|
||
| using Microsoft.AspNetCore.Http; | ||
|
|
||
| namespace Microsoft.AspNetCore.Routing.Matching; | ||
|
|
||
| internal sealed class HttpMethodDestinationsLookup | ||
| { | ||
| private readonly int _exitDestination; | ||
|
|
||
| private readonly int _connectDestination; | ||
| private readonly int _deleteDestination; | ||
| private readonly int _getDestination; | ||
| private readonly int _headDestination; | ||
| private readonly int _optionsDestination; | ||
| private readonly int _patchDestination; | ||
| private readonly int _postDestination; | ||
| private readonly int _putDestination; | ||
| private readonly int _traceDestination; | ||
| private readonly Dictionary<string, int>? _extraDestinations; | ||
|
|
||
| public HttpMethodDestinationsLookup(List<KeyValuePair<string, int>> destinations, int exitDestination) | ||
| { | ||
| _exitDestination = exitDestination; | ||
|
|
||
| int? connectDestination = null; | ||
| int? deleteDestination = null; | ||
| int? getDestination = null; | ||
| int? headDestination = null; | ||
| int? optionsDestination = null; | ||
| int? patchDestination = null; | ||
| int? postDestination = null; | ||
| int? putDestination = null; | ||
| int? traceDestination = null; | ||
|
|
||
| foreach (var (method, destination) in destinations) | ||
| { | ||
| if (method.Length >= 3) // 3 == smallest known method | ||
| { | ||
| switch (method[0] | 0x20) | ||
| { | ||
| case 'c' when method.Equals(HttpMethods.Connect, StringComparison.OrdinalIgnoreCase): | ||
| connectDestination = destination; | ||
| continue; | ||
| case 'd' when method.Equals(HttpMethods.Delete, StringComparison.OrdinalIgnoreCase): | ||
| deleteDestination = destination; | ||
| continue; | ||
| case 'g' when method.Equals(HttpMethods.Get, StringComparison.OrdinalIgnoreCase): | ||
| getDestination = destination; | ||
| continue; | ||
| case 'h' when method.Equals(HttpMethods.Head, StringComparison.OrdinalIgnoreCase): | ||
| headDestination = destination; | ||
| continue; | ||
| case 'o' when method.Equals(HttpMethods.Options, StringComparison.OrdinalIgnoreCase): | ||
| optionsDestination = destination; | ||
| continue; | ||
| case 'p': | ||
| if (method.Equals(HttpMethods.Put, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| putDestination = destination; | ||
| continue; | ||
| } | ||
| else if (method.Equals(HttpMethods.Post, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| postDestination = destination; | ||
| continue; | ||
| } | ||
| else if (method.Equals(HttpMethods.Patch, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| patchDestination = destination; | ||
| continue; | ||
| } | ||
| break; | ||
| case 't' when method.Equals(HttpMethods.Trace, StringComparison.OrdinalIgnoreCase): | ||
| traceDestination = destination; | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| _extraDestinations ??= new Dictionary<string, int>(StringComparer.OrdinalIgnoreCase); | ||
| _extraDestinations.Add(method, destination); | ||
| } | ||
|
|
||
| _connectDestination = connectDestination ?? _exitDestination; | ||
| _deleteDestination = deleteDestination ?? _exitDestination; | ||
| _getDestination = getDestination ?? _exitDestination; | ||
| _headDestination = headDestination ?? _exitDestination; | ||
| _optionsDestination = optionsDestination ?? _exitDestination; | ||
| _patchDestination = patchDestination ?? _exitDestination; | ||
| _postDestination = postDestination ?? _exitDestination; | ||
| _putDestination = putDestination ?? _exitDestination; | ||
| _traceDestination = traceDestination ?? _exitDestination; | ||
| } | ||
|
|
||
| public int GetDestination(string method) | ||
| { | ||
| // Implementation skeleton taken from https://github.com/dotnet/runtime/blob/13b43155c31beb844b1b04766fea65235ccd8363/src/libraries/System.Net.Http/src/System/Net/Http/HttpMethod.cs#L179 | ||
| if (method.Length >= 3) // 3 == smallest known method | ||
| { | ||
| (var matchedMethod, var destination) = (method[0] | 0x20) switch | ||
| { | ||
| 'c' => (HttpMethods.Connect, _connectDestination), | ||
| 'd' => (HttpMethods.Delete, _deleteDestination), | ||
| 'g' => (HttpMethods.Get, _getDestination), | ||
| 'h' => (HttpMethods.Head, _headDestination), | ||
| 'o' => (HttpMethods.Options, _optionsDestination), | ||
| 'p' => method.Length switch | ||
| { | ||
| 3 => (HttpMethods.Put, _putDestination), | ||
| 4 => (HttpMethods.Post, _postDestination), | ||
| _ => (HttpMethods.Patch, _patchDestination), | ||
| }, | ||
| 't' => (HttpMethods.Trace, _traceDestination), | ||
| _ => (null, 0), | ||
| }; | ||
|
|
||
| if (matchedMethod is not null && method.Equals(matchedMethod, StringComparison.OrdinalIgnoreCase)) | ||
| { | ||
| return destination; | ||
| } | ||
| } | ||
|
|
||
| if (_extraDestinations != null && _extraDestinations.TryGetValue(method, out var extraDestination)) | ||
| { | ||
| return extraDestination; | ||
| } | ||
|
|
||
| return _exitDestination; | ||
| } | ||
| } | ||
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
Oops, something went wrong.
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.
I'm wondering if we can speed-up the
method.Equals(matchedMethod, StringComparison.OrdinalIgnoreCase)since we know we are targeting a well-known method. Maybe we can leverage the fact that it's very likely ASCII on the header to perform two memory comparisons and or the results for the individual characters?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.
Do you mean to compare with upper-case and lower-case, OR the results and check if all values set to 1? I actually wonder if there is something like this in System.Text.Ascii already that I could use straight away. And I assume we cannot expect it as ASCII, so if the match fails, fall back to regular string comparison, right?
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.
Yeah, there is potential to speed this up:
SequenceEqual. If it doesn't match then fallback to string.Equals + OrdinalIgnoreCase.However, this PR has a nice improvement and sets up good infrastructure. Merge now and iterate in the future.
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 would have the two versions of the method in a buffer, (upper and lowercase), compare against it and or the results. If done with simd it's about 4 ops.
Disclaimer that I haven't done it myself, but I know in other places we use things like SWAR (Simd within a register) so it's likely useful to apply it here too, rely on SIMD instructions directly or use any specialized helper.
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 am happy to come explore @javiercn , I have written some vectorized code in the past, let me see if I can do something "simple". But Ascii type also uses vectorization.
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.
It would be a bit more complicated because mix case is valid, e.g.
Post.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.
@JamesNK and @javiercn I promised to come back to this thread. TLDR; none of the above suggestions were performing better to what is already merged.
Using ASCII
Relevant code:
ASCII Results
Using SequenceEquals Ordinal comparison
Relevant code:
SequenceEqual Results
Vector
A slightly different implementation I take more optimistic approach that only compares with upper-case vectorized, and if no match falls back to regular approach. The problem with vectors is that they are a bit more difficult to create when the size of the vector is larger than the input.
So what I ended up doing is creating vectorized versions of the known HTTP verbs and a vector mask corresponding to their length.
I was considering to implement the lower-case comparison too but given the performance tests indicate this solution is already slower to the
string.Equalseven when the input is upper cased, extending it won't get any faster.Vectorized Results: