Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit 02cafa1

Browse files
committed
Common headers have common offsets
1 parent 82e1218 commit 02cafa1

10 files changed

Lines changed: 509 additions & 84 deletions

File tree

‎src/Microsoft.AspNet.Server.Kestrel/Http/Frame.cs‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,7 @@ private bool RequestUrlStartsWithPathBase(string requestUrl, out bool caseMatche
835835

836836
public static bool TakeMessageHeaders(SocketInput input, FrameRequestHeaders requestHeaders)
837837
{
838+
MemoryPoolIterator2 endName;
838839
var scan = input.ConsumingStart();
839840
var consumed = scan;
840841
try
@@ -844,8 +845,15 @@ public static bool TakeMessageHeaders(SocketInput input, FrameRequestHeaders req
844845
while (!scan.IsEnd)
845846
{
846847
var beginName = scan;
847-
scan.Seek(ref _vectorColons, ref _vectorCRs);
848-
var endName = scan;
848+
if (scan.Peek() == '\r' || scan.SeekCommonHeader())
849+
{
850+
endName = scan;
851+
}
852+
else
853+
{
854+
scan.Seek(ref _vectorColons, ref _vectorCRs);
855+
endName = scan;
856+
}
849857

850858
chFirst = scan.Take();
851859
var beginValue = scan;

‎src/Microsoft.AspNet.Server.Kestrel/Http/FrameHeaders.Generated.cs‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11

22
using System;
33
using System.Collections.Generic;
4-
using System.Text;
54
using Microsoft.AspNet.Server.Kestrel.Infrastructure;
65
using Microsoft.Extensions.Primitives;
76

‎src/Microsoft.AspNet.Server.Kestrel/Http/SocketOutput.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ public unsafe Task WriteAsync(
159159
}
160160

161161
number++;
162-
if (number > 10)
162+
if (number > 11)
163163
{
164164
number = 0;
165165
scheduleWrite = true;
Lines changed: 316 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,316 @@
1+
2+
using System;
3+
4+
namespace Microsoft.AspNet.Server.Kestrel.Infrastructure
5+
{
6+
public partial struct MemoryPoolIterator2
7+
{
8+
private const byte _colon = 58;
9+
private const int _headerAcce = 1701012321;
10+
private const int _headerAllo = 1869376609;
11+
private const int _headerAuth = 1752462689;
12+
private const int _headerCach = 1751343459;
13+
private const int _headerConn = 1852731235;
14+
private const int _headerCont = 1953394531;
15+
private const int _headerCook = 1802465123;
16+
private const int _headerDate = 1702125924;
17+
private const int _headerExpe = 1701869669;
18+
private const int _headerExpi = 1768978533;
19+
private const int _headerFrom = 1836020326;
20+
private const int _headerHost = 1953722216;
21+
private const int _headerIf_M = 1831691881;
22+
private const int _headerIf_N = 1848469097;
23+
private const int _headerIf_R = 1915577961;
24+
private const int _headerIf_U = 1965909609;
25+
private const int _headerKeep = 1885693291;
26+
private const int _headerLast = 1953718636;
27+
private const int _headerMax_ = 762863981;
28+
private const int _headerPrag = 1734439536;
29+
private const int _headerProx = 2020569712;
30+
private const int _headerRang = 1735287154;
31+
private const int _headerRefe = 1701209458;
32+
private const int _headerTrai = 1767993972;
33+
private const int _headerTran = 1851880052;
34+
private const int _headerUpgr = 1919381621;
35+
private const int _headerUser = 1919251317;
36+
private const int _headerWarn = 1852989815;
37+
38+
39+
public unsafe bool SeekCommonHeader()
40+
{
41+
if (BitConverter.IsLittleEndian != true)
42+
{
43+
return false;
44+
}
45+
46+
if (IsDefault)
47+
{
48+
return false;
49+
}
50+
51+
var block = _block;
52+
var index = _index;
53+
var following = block.End - index;
54+
55+
if (following < 4)
56+
{
57+
return false;
58+
}
59+
60+
var currentPointer = block.Pointer + index;
61+
var fourLowerChars = *(int*)(currentPointer) | 0x20202020;
62+
63+
switch (fourLowerChars)
64+
{
65+
case _headerHost:
66+
if (following >= 4 && *(currentPointer + 4) == _colon) // Host
67+
{
68+
_index = index + 4;
69+
return true;
70+
}
71+
return false;
72+
case _headerFrom:
73+
if (following >= 4 && *(currentPointer + 4) == _colon) // From
74+
{
75+
_index = index + 4;
76+
return true;
77+
}
78+
return false;
79+
case _headerDate:
80+
if (following >= 4 && *(currentPointer + 4) == _colon) // Date
81+
{
82+
_index = index + 4;
83+
return true;
84+
}
85+
return false;
86+
case _headerRang:
87+
if (following >= 5 && *(currentPointer + 5) == _colon) // Range
88+
{
89+
_index = index + 5;
90+
return true;
91+
}
92+
return false;
93+
case _headerAllo:
94+
if (following >= 5 && *(currentPointer + 5) == _colon) // Allow
95+
{
96+
_index = index + 5;
97+
return true;
98+
}
99+
return false;
100+
case _headerPrag:
101+
if (following >= 6 && *(currentPointer + 6) == _colon) // Pragma
102+
{
103+
_index = index + 6;
104+
return true;
105+
}
106+
return false;
107+
case _headerExpe:
108+
if (following >= 6 && *(currentPointer + 6) == _colon) // Expect
109+
{
110+
_index = index + 6;
111+
return true;
112+
}
113+
return false;
114+
case _headerCook:
115+
if (following >= 6 && *(currentPointer + 6) == _colon) // Cookie
116+
{
117+
_index = index + 6;
118+
return true;
119+
}
120+
return false;
121+
case _headerAcce:
122+
if (following >= 6 && *(currentPointer + 6) == _colon) // Accept
123+
{
124+
_index = index + 6;
125+
return true;
126+
}
127+
128+
if (following >= 14 && *(currentPointer + 14) == _colon) // Accept-Charset
129+
{
130+
_index = index + 14;
131+
return true;
132+
}
133+
134+
if (following >= 15 && *(currentPointer + 15) == _colon) // Accept-Language, Accept-Encoding
135+
{
136+
_index = index + 15;
137+
return true;
138+
}
139+
return false;
140+
case _headerWarn:
141+
if (following >= 7 && *(currentPointer + 7) == _colon) // Warning
142+
{
143+
_index = index + 7;
144+
return true;
145+
}
146+
return false;
147+
case _headerUpgr:
148+
if (following >= 7 && *(currentPointer + 7) == _colon) // Upgrade
149+
{
150+
_index = index + 7;
151+
return true;
152+
}
153+
return false;
154+
case _headerTrai:
155+
if (following >= 7 && *(currentPointer + 7) == _colon) // Trailer
156+
{
157+
_index = index + 7;
158+
return true;
159+
}
160+
return false;
161+
case _headerRefe:
162+
if (following >= 7 && *(currentPointer + 7) == _colon) // Referer
163+
{
164+
_index = index + 7;
165+
return true;
166+
}
167+
return false;
168+
case _headerExpi:
169+
if (following >= 7 && *(currentPointer + 7) == _colon) // Expires
170+
{
171+
_index = index + 7;
172+
return true;
173+
}
174+
return false;
175+
case _headerIf_R:
176+
if (following >= 8 && *(currentPointer + 8) == _colon) // If-Range
177+
{
178+
_index = index + 8;
179+
return true;
180+
}
181+
return false;
182+
case _headerIf_M:
183+
if (following >= 8 && *(currentPointer + 8) == _colon) // If-Match
184+
{
185+
_index = index + 8;
186+
return true;
187+
}
188+
189+
if (following >= 17 && *(currentPointer + 17) == _colon) // If-Modified-Since
190+
{
191+
_index = index + 17;
192+
return true;
193+
}
194+
return false;
195+
case _headerTran:
196+
if (following >= 9 && *(currentPointer + 9) == _colon) // Translate
197+
{
198+
_index = index + 9;
199+
return true;
200+
}
201+
202+
if (following >= 17 && *(currentPointer + 17) == _colon) // Transfer-Encoding
203+
{
204+
_index = index + 17;
205+
return true;
206+
}
207+
return false;
208+
case _headerUser:
209+
if (following >= 10 && *(currentPointer + 10) == _colon) // User-Agent
210+
{
211+
_index = index + 10;
212+
return true;
213+
}
214+
return false;
215+
case _headerKeep:
216+
if (following >= 10 && *(currentPointer + 10) == _colon) // Keep-Alive
217+
{
218+
_index = index + 10;
219+
return true;
220+
}
221+
return false;
222+
case _headerConn:
223+
if (following >= 10 && *(currentPointer + 10) == _colon) // Connection
224+
{
225+
_index = index + 10;
226+
return true;
227+
}
228+
return false;
229+
case _headerCont:
230+
if (following >= 11 && *(currentPointer + 11) == _colon) // Content-MD5
231+
{
232+
_index = index + 11;
233+
return true;
234+
}
235+
236+
if (following >= 12 && *(currentPointer + 12) == _colon) // Content-Type
237+
{
238+
_index = index + 12;
239+
return true;
240+
}
241+
242+
if (following >= 13 && *(currentPointer + 13) == _colon) // Content-Range
243+
{
244+
_index = index + 13;
245+
return true;
246+
}
247+
248+
if (following >= 14 && *(currentPointer + 14) == _colon) // Content-Length
249+
{
250+
_index = index + 14;
251+
return true;
252+
}
253+
254+
if (following >= 16 && *(currentPointer + 16) == _colon) // Content-Location, Content-Language, Content-Encoding
255+
{
256+
_index = index + 16;
257+
return true;
258+
}
259+
return false;
260+
case _headerMax_:
261+
if (following >= 12 && *(currentPointer + 12) == _colon) // Max-Forwards
262+
{
263+
_index = index + 12;
264+
return true;
265+
}
266+
return false;
267+
case _headerLast:
268+
if (following >= 13 && *(currentPointer + 13) == _colon) // Last-Modified
269+
{
270+
_index = index + 13;
271+
return true;
272+
}
273+
return false;
274+
case _headerIf_N:
275+
if (following >= 13 && *(currentPointer + 13) == _colon) // If-None-Match
276+
{
277+
_index = index + 13;
278+
return true;
279+
}
280+
return false;
281+
case _headerCach:
282+
if (following >= 13 && *(currentPointer + 13) == _colon) // Cache-Control
283+
{
284+
_index = index + 13;
285+
return true;
286+
}
287+
return false;
288+
case _headerAuth:
289+
if (following >= 13 && *(currentPointer + 13) == _colon) // Authorization
290+
{
291+
_index = index + 13;
292+
return true;
293+
}
294+
return false;
295+
case _headerProx:
296+
if (following >= 19 && *(currentPointer + 19) == _colon) // Proxy-Authorization
297+
{
298+
_index = index + 19;
299+
return true;
300+
}
301+
return false;
302+
case _headerIf_U:
303+
if (following >= 19 && *(currentPointer + 19) == _colon) // If-Unmodified-Since
304+
{
305+
_index = index + 19;
306+
return true;
307+
}
308+
return false;
309+
310+
default:
311+
return false;
312+
}
313+
314+
}
315+
}
316+
}

‎src/Microsoft.AspNet.Server.Kestrel/Infrastructure/MemoryPoolIterator2.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Microsoft.AspNet.Server.Kestrel.Infrastructure
99
{
10-
public struct MemoryPoolIterator2
10+
public partial struct MemoryPoolIterator2
1111
{
1212
private readonly static int _vectorSpan = Vector<byte>.Count;
1313

@@ -249,9 +249,9 @@ public unsafe int Seek(ref Vector<byte> byte0Vector, ref Vector<byte> byte1Vecto
249249
return -1;
250250
}
251251

252-
var following = _block.End - _index;
253252
var block = _block;
254253
var index = _index;
254+
var following = block.End - index;
255255
byte[] array;
256256
int byte0Index = int.MaxValue;
257257
int byte1Index = int.MaxValue;

‎src/Microsoft.AspNet.Server.Kestrel/Networking/UvWriteReq.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public class UvWriteReq : UvRequest
1919

2020
private Action<UvWriteReq, int, Exception, object> _callback;
2121
private object _state;
22-
private const int BUFFER_COUNT = 16;
22+
private const int BUFFER_COUNT = 32;
2323

2424
private GCHandle _pinUvWriteReq;
2525
private GCHandle _pinBufferArray;

‎src/Microsoft.AspNet.Server.Kestrel/project.json‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@
7474
"scripts": {
7575
"prepare": [
7676
"dnu restore ../../tools/Microsoft.AspNet.Server.Kestrel.GeneratedCode",
77-
"dnx -p ../../tools/Microsoft.AspNet.Server.Kestrel.GeneratedCode run Http/FrameHeaders.Generated.cs Http/Frame.Generated.cs"
77+
"dnx -p ../../tools/Microsoft.AspNet.Server.Kestrel.GeneratedCode run Http/FrameHeaders.Generated.cs Http/Frame.Generated.cs Infrastructure/MemoryPoolIterator2.Generated.cs"
7878
],
7979
"postrestore": [
8080
"dnu restore ../../tools/Microsoft.AspNet.Server.Kestrel.LibuvCopier",

0 commit comments

Comments
 (0)