File tree Expand file tree Collapse file tree 4 files changed +59
-7
lines changed
BenchmarkDotNet.Artifacts/results
src/JsonApiDotNetCore/Middleware Expand file tree Collapse file tree 4 files changed +59
-7
lines changed Original file line number Diff line number Diff line change 1+ ``` ini
2+
3+ BenchmarkDotNet =v0.10.10, OS =Mac OS X 10.12
4+ Processor =Intel Core i5-5257U CPU 2.70GHz (Broadwell), ProcessorCount =4
5+ .NET Core SDK =2.1.4
6+ [Host] : .NET Core 2.0.5 (Framework 4.6.0.0), 64bit RyuJIT
7+ DefaultJob : .NET Core 2.0.5 (Framework 4.6.0.0), 64bit RyuJIT
8+
9+
10+ ```
11+ | Method | Mean | Error | StdDev | Gen 0 | Allocated |
12+ | ----------- | ----------:| ----------:| ----------:| -------:| ----------:|
13+ | UsingSplit | 157.28 ns | 2.9689 ns | 5.8602 ns | 0.2134 | 336 B |
14+ | Current | 39.96 ns | 0.6489 ns | 0.6070 ns | - | 0 B |
Original file line number Diff line number Diff line change 11using BenchmarkDotNet . Running ;
22using Benchmarks . LinkBuilder ;
33using Benchmarks . Query ;
4+ using Benchmarks . RequestMiddleware ;
45using Benchmarks . Serialization ;
56
67namespace Benchmarks {
@@ -10,7 +11,8 @@ static void Main(string[] args) {
1011 typeof ( JsonApiDeserializer_Benchmarks ) ,
1112 typeof ( JsonApiSerializer_Benchmarks ) ,
1213 typeof ( QueryParser_Benchmarks ) ,
13- typeof ( LinkBuilder_GetNamespaceFromPath_Benchmarks )
14+ typeof ( LinkBuilder_GetNamespaceFromPath_Benchmarks ) ,
15+ typeof ( ContainsMediaTypeParameters_Benchmarks )
1416 } ) ;
1517 switcher . Run ( args ) ;
1618 }
Original file line number Diff line number Diff line change 1+ using System ;
2+ using BenchmarkDotNet . Attributes ;
3+ using BenchmarkDotNet . Attributes . Exporters ;
4+ using JsonApiDotNetCore . Internal ;
5+
6+ namespace Benchmarks . RequestMiddleware
7+ {
8+ [ MarkdownExporter , MemoryDiagnoser ]
9+ public class ContainsMediaTypeParameters_Benchmarks
10+ {
11+ private const string MEDIA_TYPE = "application/vnd.api+json; version=1" ;
12+
13+ [ Benchmark ]
14+ public void UsingSplit ( ) => UsingSplitImpl ( MEDIA_TYPE ) ;
15+
16+ [ Benchmark ]
17+ public void Current ( )
18+ => JsonApiDotNetCore . Middleware . RequestMiddleware . ContainsMediaTypeParameters ( MEDIA_TYPE ) ;
19+
20+ private bool UsingSplitImpl ( string mediaType )
21+ {
22+ var mediaTypeArr = mediaType . Split ( ';' ) ;
23+ return ( mediaTypeArr [ 0 ] == Constants . ContentType && mediaTypeArr . Length == 2 ) ;
24+ }
25+ }
26+ }
Original file line number Diff line number Diff line change 11using System ;
22using System . Threading . Tasks ;
3- using JsonApiDotNetCore . Extensions ;
43using JsonApiDotNetCore . Internal ;
54using Microsoft . AspNetCore . Http ;
65using Microsoft . Extensions . Primitives ;
@@ -54,12 +53,23 @@ private static bool IsValidAcceptHeader(HttpContext context)
5453 return true ;
5554 }
5655
57- private static bool ContainsMediaTypeParameters ( string mediaType )
56+ internal static bool ContainsMediaTypeParameters ( string mediaType )
5857 {
59- const char delimeter = ';' ;
60- var subSpans = mediaType . SpanSplit ( delimeter ) ;
61- if ( subSpans . Count == 0 ) return false ;
62- return subSpans . Count == 2 && subSpans [ 0 ] . ToString ( ) == Constants . ContentType ;
58+ var incomingMediaTypeSpan = mediaType . AsSpan ( ) ;
59+
60+ // if the content type is not application/vnd.api+json then continue on
61+ if ( incomingMediaTypeSpan . Length < Constants . ContentType . Length )
62+ return false ;
63+
64+ var incomingContentType = incomingMediaTypeSpan . Slice ( 0 , Constants . ContentType . Length ) ;
65+ if ( incomingContentType . SequenceEqual ( Constants . ContentType . AsSpan ( ) ) == false )
66+ return false ;
67+
68+ // anything appended to "application/vnd.api+json;" will be considered a media type param
69+ return (
70+ incomingMediaTypeSpan . Length >= Constants . ContentType . Length + 2
71+ && incomingMediaTypeSpan [ Constants . ContentType . Length ] == ';'
72+ ) ;
6373 }
6474
6575 private static void FlushResponse ( HttpContext context , int statusCode )
You can’t perform that action at this time.
0 commit comments