File tree 4 files changed +59
-7
lines changed
BenchmarkDotNet.Artifacts/results
src/JsonApiDotNetCore/Middleware 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 1
1
using BenchmarkDotNet . Running ;
2
2
using Benchmarks . LinkBuilder ;
3
3
using Benchmarks . Query ;
4
+ using Benchmarks . RequestMiddleware ;
4
5
using Benchmarks . Serialization ;
5
6
6
7
namespace Benchmarks {
@@ -10,7 +11,8 @@ static void Main(string[] args) {
10
11
typeof ( JsonApiDeserializer_Benchmarks ) ,
11
12
typeof ( JsonApiSerializer_Benchmarks ) ,
12
13
typeof ( QueryParser_Benchmarks ) ,
13
- typeof ( LinkBuilder_GetNamespaceFromPath_Benchmarks )
14
+ typeof ( LinkBuilder_GetNamespaceFromPath_Benchmarks ) ,
15
+ typeof ( ContainsMediaTypeParameters_Benchmarks )
14
16
} ) ;
15
17
switcher . Run ( args ) ;
16
18
}
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 1
1
using System ;
2
2
using System . Threading . Tasks ;
3
- using JsonApiDotNetCore . Extensions ;
4
3
using JsonApiDotNetCore . Internal ;
5
4
using Microsoft . AspNetCore . Http ;
6
5
using Microsoft . Extensions . Primitives ;
@@ -54,12 +53,23 @@ private static bool IsValidAcceptHeader(HttpContext context)
54
53
return true ;
55
54
}
56
55
57
- private static bool ContainsMediaTypeParameters ( string mediaType )
56
+ internal static bool ContainsMediaTypeParameters ( string mediaType )
58
57
{
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
+ ) ;
63
73
}
64
74
65
75
private static void FlushResponse ( HttpContext context , int statusCode )
You can’t perform that action at this time.
0 commit comments