Skip to content

Commit 5741ac7

Browse files
committed
Use span comparison for request dispatching
1 parent f6fc284 commit 5741ac7

File tree

1 file changed

+49
-13
lines changed

1 file changed

+49
-13
lines changed

src/BenchmarksApps/TechEmpower/Kestrel/BenchmarkApp.cs

Lines changed: 49 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,50 @@ public Task ProcessRequestAsync(IFeatureCollection features)
2020
var req = features.GetRequestFeature();
2121
var res = features.GetResponseFeature();
2222

23-
if (req.Method != "GET")
23+
//if (req.Method != "GET")
24+
//{
25+
// res.StatusCode = StatusCodes.Status405MethodNotAllowed;
26+
// return Task.CompletedTask;
27+
//}
28+
29+
var pathSpan = req.Path.AsSpan();
30+
if (Paths.IsPath(pathSpan, Paths.Plaintext))
2431
{
25-
res.StatusCode = StatusCodes.Status405MethodNotAllowed;
26-
return Task.CompletedTask;
32+
return Plaintext(res, features);
2733
}
28-
29-
return req.Path switch
34+
else if (Paths.IsPath(pathSpan, Paths.Json))
35+
{
36+
return Json(res, features);
37+
}
38+
else if (pathSpan.StartsWith("json-string", StringComparison.OrdinalIgnoreCase))
39+
{
40+
return JsonString(res, features);
41+
}
42+
else if (pathSpan.StartsWith("json-utf8bytes", StringComparison.OrdinalIgnoreCase))
43+
{
44+
return JsonUtf8Bytes(res, features);
45+
}
46+
else if (pathSpan.StartsWith("json-chunked", StringComparison.OrdinalIgnoreCase))
3047
{
31-
"/plaintext" => Plaintext(res, features),
32-
"/json" => Json(res, features),
33-
"/json-string" => JsonString(res, features),
34-
"/json-utf8bytes" => JsonUtf8Bytes(res, features),
35-
"/json-chunked" => JsonChunked(res, features),
36-
"/" => Index(res, features),
37-
_ => NotFound(res, features),
38-
};
48+
return JsonChunked(res, features);
49+
}
50+
else if (pathSpan.IsEmpty || pathSpan.Equals("/", StringComparison.OrdinalIgnoreCase))
51+
{
52+
return Index(res, features);
53+
}
54+
55+
return NotFound(res, features);
56+
57+
//return req.Path switch
58+
//{
59+
// "/plaintext" => Plaintext(res, features),
60+
// "/json" => Json(res, features),
61+
// "/json-string" => JsonString(res, features),
62+
// "/json-utf8bytes" => JsonUtf8Bytes(res, features),
63+
// "/json-chunked" => JsonChunked(res, features),
64+
// "/" => Index(res, features),
65+
// _ => NotFound(res, features),
66+
//};
3967
}
4068

4169
private static Task NotFound(IHttpResponseFeature res, IFeatureCollection features)
@@ -168,4 +196,12 @@ private partial class JsonContext : JsonSerializerContext
168196
{
169197

170198
}
199+
200+
private static class Paths
201+
{
202+
public static ReadOnlySpan<char> Plaintext => "/plaintext";
203+
public static ReadOnlySpan<char> Json => "/json";
204+
205+
public static bool IsPath(ReadOnlySpan<char> path, ReadOnlySpan<char> targetPath) => path.SequenceEqual(targetPath);
206+
}
171207
}

0 commit comments

Comments
 (0)