-
-
Notifications
You must be signed in to change notification settings - Fork 162
Feature/#258 #271
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
Feature/#258 #271
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
b85bc14
#258 Adds new utility to provide substrings of a given string as Span…
9936939
#258 Updates LinkBuilder, RelatedAttrFilterQuery, RequestMiddleware, …
a48fc4c
#258 Fixes Error in ContainsMediaTypeParameters method's logic. Conte…
cfa785f
#258 Removes extra ToString() on the span and fixes naming to be more…
crfloyd 85bb9f4
#258 Adds extension method for splitting with spans. Updates usages a…
crfloyd 01735f0
refactor(LinkBuilder): reduce allocations
jaredcnance 4803fd0
Update .travis.yml
jaredcnance 2f0e481
benchamrk: don't duplicate the final version's definition
jaredcnance 6568c37
add benchmarks to RequestMiddleware
jaredcnance fbe1d1b
add benchmarks for IsRelationshipPath
jaredcnance 369860c
clean up unused changes
jaredcnance 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
16 changes: 16 additions & 0 deletions
16
...hmarks.LinkBuilder.LinkBuilder_GetNamespaceFromPath_Benchmarks-report-github.md
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,16 @@ | ||
| ``` ini | ||
|
|
||
| BenchmarkDotNet=v0.10.10, OS=Mac OS X 10.12 | ||
| Processor=Intel Core i5-5257U CPU 2.70GHz (Broadwell), ProcessorCount=4 | ||
| .NET Core SDK=2.1.4 | ||
| [Host] : .NET Core 2.0.5 (Framework 4.6.0.0), 64bit RyuJIT | ||
| Job-XFMVNE : .NET Core 2.0.5 (Framework 4.6.0.0), 64bit RyuJIT | ||
|
|
||
| LaunchCount=3 TargetCount=20 WarmupCount=10 | ||
|
|
||
| ``` | ||
| | Method | Mean | Error | StdDev | Gen 0 | Allocated | | ||
| |--------------------------- |-----------:|----------:|----------:|-------:|----------:| | ||
| | UsingSplit | 1,197.6 ns | 11.929 ns | 25.933 ns | 0.9251 | 1456 B | | ||
| | UsingSpanWithStringBuilder | 1,542.0 ns | 15.249 ns | 33.792 ns | 0.9460 | 1488 B | | ||
| | UsingSpanWithNoAlloc | 272.6 ns | 2.265 ns | 5.018 ns | 0.0863 | 136 B | |
90 changes: 90 additions & 0 deletions
90
benchmarks/LinkBuilder/LinkBuilder_ GetNamespaceFromPath_Benchmarks.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,90 @@ | ||
| using System; | ||
| using System.Diagnostics; | ||
| using System.Text; | ||
| using System.Threading; | ||
| using BenchmarkDotNet.Attributes; | ||
| using BenchmarkDotNet.Attributes.Exporters; | ||
| using BenchmarkDotNet.Attributes.Jobs; | ||
| using JsonApiDotNetCore.Extensions; | ||
|
|
||
| namespace Benchmarks.LinkBuilder | ||
| { | ||
| [MarkdownExporter, SimpleJob(launchCount : 3, warmupCount : 10, targetCount : 20), MemoryDiagnoser] | ||
| public class LinkBuilder_GetNamespaceFromPath_Benchmarks | ||
| { | ||
| private const string PATH = "/api/some-really-long-namespace-path/resources/current/articles"; | ||
| private const string ENTITY_NAME = "articles"; | ||
|
|
||
| [Benchmark] | ||
| public void UsingSplit() => GetNamespaceFromPath_BySplitting(PATH, ENTITY_NAME); | ||
|
|
||
| [Benchmark] | ||
| public void UsingSpanWithStringBuilder() => GetNamespaceFromPath_Using_Span_With_StringBuilder(PATH, ENTITY_NAME); | ||
|
|
||
| [Benchmark] | ||
| public void UsingSpanWithNoAlloc() => GetNamespaceFromPath_Using_Span_No_Alloc(PATH, ENTITY_NAME); | ||
|
|
||
| public static string GetNamespaceFromPath_BySplitting(string path, string entityName) | ||
| { | ||
| var nSpace = string.Empty; | ||
| var segments = path.Split('/'); | ||
|
|
||
| for (var i = 1; i < segments.Length; i++) | ||
| { | ||
| if (segments[i].ToLower() == entityName) | ||
| break; | ||
|
|
||
| nSpace += $"/{segments[i]}"; | ||
| } | ||
|
|
||
| return nSpace; | ||
| } | ||
|
|
||
| public static string GetNamespaceFromPath_Using_Span_No_Alloc(string path, string entityName) | ||
| { | ||
| var entityNameSpan = entityName.AsSpan(); | ||
| var pathSpan = path.AsSpan(); | ||
| const char delimiter = '/'; | ||
| for (var i = 0; i < pathSpan.Length; i++) | ||
| { | ||
| if(pathSpan[i].Equals(delimiter)) | ||
| { | ||
| var nextPosition = i+1; | ||
| if(pathSpan.Length > i + entityNameSpan.Length) | ||
| { | ||
| var possiblePathSegment = pathSpan.Slice(nextPosition, entityNameSpan.Length); | ||
| if (entityNameSpan.SequenceEqual(possiblePathSegment)) | ||
| { | ||
| // check to see if it's the last position in the string | ||
| // or if the next character is a / | ||
| var lastCharacterPosition = nextPosition + entityNameSpan.Length; | ||
|
|
||
| if(lastCharacterPosition == pathSpan.Length || pathSpan.Length >= lastCharacterPosition + 2 && pathSpan[lastCharacterPosition + 1].Equals(delimiter)) | ||
| { | ||
| return pathSpan.Slice(0, i).ToString(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return string.Empty; | ||
| } | ||
|
|
||
| public static string GetNamespaceFromPath_Using_Span_With_StringBuilder(string path, string entityName) | ||
| { | ||
| var sb = new StringBuilder(); | ||
| var entityNameSpan = entityName.AsSpan(); | ||
| var subSpans = path.SpanSplit('/'); | ||
| for (var i = 1; i < subSpans.Count; i++) | ||
| { | ||
| var span = subSpans[i]; | ||
| if (entityNameSpan.SequenceEqual(span)) | ||
| break; | ||
|
|
||
| sb.Append($"/{span.ToString()}"); | ||
| } | ||
| return sb.ToString(); | ||
| } | ||
| } | ||
| } |
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
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,69 @@ | ||
| using System; | ||
| using System.Collections.Generic; | ||
| using System.ComponentModel; | ||
| using System.Linq; | ||
| using JsonApiDotNetCore.Extensions; | ||
|
|
||
| namespace JsonApiDotNetCore.Internal | ||
| { | ||
| public readonly ref struct SpanSplitter | ||
| { | ||
| private readonly ReadOnlySpan<char> _span; | ||
| private readonly List<int> _delimeterIndexes; | ||
| private readonly List<Tuple<int, int>> _substringIndexes; | ||
|
|
||
| public int Count => _substringIndexes.Count(); | ||
| public ReadOnlySpan<char> this[int index] => GetSpanForSubstring(index + 1); | ||
|
|
||
| private SpanSplitter(ref string str, char delimeter) | ||
| { | ||
| _span = str.AsSpan(); | ||
| _delimeterIndexes = str.IndexesOf(delimeter).ToList(); | ||
| _substringIndexes = new List<Tuple<int, int>>(); | ||
| BuildSubstringIndexes(); | ||
| } | ||
|
|
||
| public static SpanSplitter Split(string str, char delimeter) | ||
| { | ||
| return new SpanSplitter(ref str, delimeter); | ||
| } | ||
|
|
||
| [EditorBrowsable(EditorBrowsableState.Never)] | ||
| public override bool Equals(object obj) => throw new NotSupportedException(); | ||
|
|
||
| [EditorBrowsable(EditorBrowsableState.Never)] | ||
| public override int GetHashCode() => throw new NotSupportedException(); | ||
|
|
||
| [EditorBrowsable(EditorBrowsableState.Never)] | ||
| public override string ToString() => throw new NotSupportedException(); | ||
|
|
||
| private ReadOnlySpan<char> GetSpanForSubstring(int substringNumber) | ||
| { | ||
| if (substringNumber > Count) | ||
| { | ||
| throw new ArgumentOutOfRangeException($"There are only {Count} substrings given the delimeter and base string provided"); | ||
| } | ||
|
|
||
| var indexes = _substringIndexes[substringNumber - 1]; | ||
| return _span.Slice(indexes.Item1, indexes.Item2); | ||
| } | ||
|
|
||
| private void BuildSubstringIndexes() | ||
| { | ||
| var start = 0; | ||
| var end = 0; | ||
| foreach (var index in _delimeterIndexes) | ||
| { | ||
| end = index; | ||
| if (start > end) break; | ||
| _substringIndexes.Add(new Tuple<int, int>(start, end - start)); | ||
| start = ++end; | ||
| } | ||
|
|
||
| if (end <= _span.Length) | ||
| { | ||
| _substringIndexes.Add(new Tuple<int, int>(start, _span.Length - start)); | ||
| } | ||
| } | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -3,6 +3,7 @@ | |||||||||||||||||||||||||
| using System.Linq; | ||||||||||||||||||||||||||
| using JsonApiDotNetCore.Builders; | ||||||||||||||||||||||||||
| using JsonApiDotNetCore.Configuration; | ||||||||||||||||||||||||||
| using JsonApiDotNetCore.Extensions; | ||||||||||||||||||||||||||
| using JsonApiDotNetCore.Internal; | ||||||||||||||||||||||||||
| using JsonApiDotNetCore.Internal.Generics; | ||||||||||||||||||||||||||
| using JsonApiDotNetCore.Internal.Query; | ||||||||||||||||||||||||||
|
|
@@ -64,7 +65,7 @@ public IJsonApiContext ApplyContext<T>(object controller) | |||||||||||||||||||||||||
| throw new JsonApiException(500, $"A resource has not been properly defined for type '{typeof(T)}'. Ensure it has been registered on the ContextGraph."); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| var context = _httpContextAccessor.HttpContext; | ||||||||||||||||||||||||||
| var path = context.Request.Path.Value.Split('/'); | ||||||||||||||||||||||||||
| var requestPath = context.Request.Path.Value; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| if (context.Request.Query.Count > 0) | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
|
|
@@ -75,10 +76,13 @@ public IJsonApiContext ApplyContext<T>(object controller) | |||||||||||||||||||||||||
| var linkBuilder = new LinkBuilder(this); | ||||||||||||||||||||||||||
| BasePath = linkBuilder.GetBasePath(context, _controllerContext.RequestEntity.EntityName); | ||||||||||||||||||||||||||
| PageManager = GetPageManager(); | ||||||||||||||||||||||||||
| IsRelationshipPath = path[path.Length - 2] == "relationships"; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| var pathSpans = requestPath.SpanSplit('/'); | ||||||||||||||||||||||||||
| IsRelationshipPath = pathSpans[pathSpans.Count - 2].ToString() == "relationships"; | ||||||||||||||||||||||||||
|
||||||||||||||||||||||||||
| Method | Mean | Error | StdDev | Gen 0 | Allocated |
|---|---|---|---|---|---|
| Original | 421.08 ns | 19.3905 ns | 54.0529 ns | 0.4725 | 744 B |
| Current PR | 697.65 ns | 11.9282 ns | 11.1576 ns | 0.5999 | 944 B |
| Proposal (fbe1d1b) | 52.23 ns | 0.8052 ns | 0.7532 ns | - | 0 B |
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.
So, it appears that the simple overhead of the
SpanSplittertype definition and its members has enough cost that we don't actually improve the performance. In general, it appears we can get much better performance by not creating a new abstraction. While I feel like your approach was elegant, it unfortunately did not have the desired result.In the coming commit, I was able to get this down to 0 allocations.