forked from openrasta/openrasta-core
-
Notifications
You must be signed in to change notification settings - Fork 0
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
OR: performance tune Match function [PLAT-23274] #2
Open
jackmott
wants to merge
5
commits into
master
Choose a base branch
from
PLAT-23274-openrasta-match
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
76652db
OR: performance tune Match function [PLAT-23274]
jackmott 5187750
OR: revert broken candidateSegment changes [PLAT-23274]
jackmott b8b5286
OR: remove unused using [PLAT-23274]
jackmott 620e311
OR: eliminate anonymous class to reduce allocations [PLAT-23274]
jackmott 5b98222
OR: pull the removetrailingslash outside the loop [PLAT-23274]
jackmott 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 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 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 |
---|---|---|
|
@@ -52,38 +52,49 @@ public void MakeReadOnly(bool allowDuplicateEquivalentUriTemplates) | |
_keyValuePairsReadOnly = _keyValuePairs.AsReadOnly(); | ||
} | ||
|
||
string RemoveTrailingSlash(string str) | ||
{ | ||
return str.LastIndexOf('/') == str.Length - 1 ? str.Substring(0, str.Length - 1) : str; | ||
} | ||
|
||
public Collection<UriTemplateMatch> Match(Uri uri) | ||
{ | ||
var lastMaxLiteralSegmentCount = 0; | ||
var matches = new Collection<UriTemplateMatch>(); | ||
foreach (var template in KeyValuePairs) | ||
if (BaseAddress != null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If I'm reading this right, if |
||
{ | ||
// TODO: discard uri templates with fragment identifiers until tests are implemented | ||
if (template.Key.Fragment.Any()) continue; | ||
var potentialMatch = template.Key.Match(BaseAddress, uri); | ||
var baseLeft = BaseAddress.GetLeftPart(UriPartial.Authority); | ||
var baseSegments = BaseAddress.Segments.Select(RemoveTrailingSlash).ToArray(); | ||
foreach (var template in KeyValuePairs) | ||
{ | ||
// TODO: discard uri templates with fragment identifiers until tests are implemented | ||
if (template.Key.Fragment.Any()) continue; | ||
|
||
if (potentialMatch == null) continue; | ||
UriTemplateMatch potentialMatch = template.Key.Match(BaseAddress, baseLeft, baseSegments, uri); | ||
|
||
// this calculates and keep only what matches the maximum possible amount of literal segments | ||
var currentMaxLiteralSegmentCount = potentialMatch.RelativePathSegments.Count | ||
- potentialMatch.WildcardPathSegments.Count; | ||
for (var i = 0; i < potentialMatch.PathSegmentVariables.Count; i++) | ||
if (potentialMatch.QueryParameters == null || | ||
potentialMatch.QueryStringVariables[potentialMatch.PathSegmentVariables.GetKey(i)] == null) | ||
currentMaxLiteralSegmentCount -= 1; | ||
if (potentialMatch == null) continue; | ||
|
||
potentialMatch.Data = template.Value; | ||
// this calculates and keep only what matches the maximum possible amount of literal segments | ||
var currentMaxLiteralSegmentCount = potentialMatch.RelativePathSegments.Count | ||
- potentialMatch.WildcardPathSegments.Count; | ||
for (var i = 0; i < potentialMatch.PathSegmentVariables.Count; i++) | ||
if (potentialMatch.QueryParameters == null || | ||
potentialMatch.QueryStringVariables[potentialMatch.PathSegmentVariables.GetKey(i)] == null) | ||
currentMaxLiteralSegmentCount -= 1; | ||
|
||
if (currentMaxLiteralSegmentCount > lastMaxLiteralSegmentCount) | ||
{ | ||
lastMaxLiteralSegmentCount = currentMaxLiteralSegmentCount; | ||
} | ||
else if (currentMaxLiteralSegmentCount < lastMaxLiteralSegmentCount) | ||
{ | ||
continue; | ||
} | ||
potentialMatch.Data = template.Value; | ||
|
||
matches.Add(potentialMatch); | ||
if (currentMaxLiteralSegmentCount > lastMaxLiteralSegmentCount) | ||
{ | ||
lastMaxLiteralSegmentCount = currentMaxLiteralSegmentCount; | ||
} | ||
else if (currentMaxLiteralSegmentCount < lastMaxLiteralSegmentCount) | ||
{ | ||
continue; | ||
} | ||
|
||
matches.Add(potentialMatch); | ||
} | ||
} | ||
|
||
return SortByMatchQuality(matches).ToCollection(); | ||
|
@@ -103,15 +114,20 @@ IEnumerable<UriTemplateMatch> SortByMatchQuality(Collection<UriTemplateMatch> ma | |
public UriTemplateMatch MatchSingle(Uri uri) | ||
{ | ||
UriTemplateMatch singleMatch = null; | ||
foreach (var segmentKey in KeyValuePairs) | ||
if (BaseAddress != null) | ||
{ | ||
UriTemplateMatch potentialMatch = segmentKey.Key.Match(BaseAddress, uri); | ||
if (potentialMatch != null && singleMatch != null) | ||
throw new UriTemplateMatchException("Several matching templates were found."); | ||
if (potentialMatch != null) | ||
var baseLeft = BaseAddress.GetLeftPart(UriPartial.Authority); | ||
var baseSegments = BaseAddress.Segments.Select(RemoveTrailingSlash).ToArray(); | ||
foreach (var segmentKey in KeyValuePairs) | ||
{ | ||
singleMatch = potentialMatch; | ||
singleMatch.Data = segmentKey.Value; | ||
UriTemplateMatch potentialMatch = segmentKey.Key.Match(BaseAddress,baseLeft,baseSegments,uri); | ||
if (potentialMatch != null && singleMatch != null) | ||
throw new UriTemplateMatchException("Several matching templates were found."); | ||
if (potentialMatch != null) | ||
{ | ||
singleMatch = potentialMatch; | ||
singleMatch.Data = segmentKey.Value; | ||
} | ||
} | ||
} | ||
|
||
|
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.
Not your code but this could be simplified to
!string.Equals(_segments[i].Text, unescapedText, StringComparison.OrdinalIgnoreCase)
.