Faster unsafe pointers

This commit is contained in:
Thom Kiesewetter 2015-12-29 14:41:46 +01:00
parent 9edd6f60b9
commit 8fdfef460a
4 changed files with 1223 additions and 1199 deletions

View File

@ -719,7 +719,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
} }
protected override StringValues GetValueFast(string key) protected override StringValues GetValueFast(string key)
{ {
switch(key.Length) switch (key.Length)
{ {
case 13: case 13:
{ {
@ -1289,7 +1289,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
} }
protected override bool TryGetValueFast(string key, out StringValues value) protected override bool TryGetValueFast(string key, out StringValues value)
{ {
switch(key.Length) switch (key.Length)
{ {
case 13: case 13:
{ {
@ -1938,7 +1938,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
} }
protected override void SetValueFast(string key, StringValues value) protected override void SetValueFast(string key, StringValues value)
{ {
switch(key.Length) switch (key.Length)
{ {
case 13: case 13:
{ {
@ -2299,7 +2299,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
} }
protected override void AddValueFast(string key, StringValues value) protected override void AddValueFast(string key, StringValues value)
{ {
switch(key.Length) switch (key.Length)
{ {
case 13: case 13:
{ {
@ -2824,7 +2824,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
} }
protected override bool RemoveFast(string key) protected override bool RemoveFast(string key)
{ {
switch(key.Length) switch (key.Length)
{ {
case 13: case 13:
{ {
@ -3939,8 +3939,13 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
public unsafe void Append(byte[] keyBytes, int keyOffset, int keyLength, string value) public unsafe void Append(byte[] keyBytes, int keyOffset, int keyLength, string value)
{ {
fixed(byte* ptr = keyBytes) { var pUB = ptr + keyOffset; var pUL = (ulong*)pUB; var pUI = (uint*)pUB; var pUS = (ushort*)pUB; fixed (byte* ptr = &keyBytes[keyOffset])
switch(keyLength) {
var pUB = ptr;
var pUL = (ulong*)pUB;
var pUI = (uint*)pUB;
var pUS = (ushort*)pUB;
switch (keyLength)
{ {
case 13: case 13:
{ {
@ -4583,7 +4588,8 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
} }
} }
break; break;
}} }
}
var key = System.Text.Encoding.ASCII.GetString(keyBytes, keyOffset, keyLength); var key = System.Text.Encoding.ASCII.GetString(keyBytes, keyOffset, keyLength);
StringValues existing; StringValues existing;
Unknown.TryGetValue(key, out existing); Unknown.TryGetValue(key, out existing);
@ -5631,7 +5637,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
} }
protected override StringValues GetValueFast(string key) protected override StringValues GetValueFast(string key)
{ {
switch(key.Length) switch (key.Length)
{ {
case 13: case 13:
{ {
@ -6053,7 +6059,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
} }
protected override bool TryGetValueFast(string key, out StringValues value) protected override bool TryGetValueFast(string key, out StringValues value)
{ {
switch(key.Length) switch (key.Length)
{ {
case 13: case 13:
{ {
@ -6532,7 +6538,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
} }
protected override void SetValueFast(string key, StringValues value) protected override void SetValueFast(string key, StringValues value)
{ {
switch(key.Length) switch (key.Length)
{ {
case 13: case 13:
{ {
@ -6805,7 +6811,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
} }
protected override void AddValueFast(string key, StringValues value) protected override void AddValueFast(string key, StringValues value)
{ {
switch(key.Length) switch (key.Length)
{ {
case 13: case 13:
{ {
@ -7198,7 +7204,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
} }
protected override bool RemoveFast(string key) protected override bool RemoveFast(string key)
{ {
switch(key.Length) switch (key.Length)
{ {
case 13: case 13:
{ {
@ -8030,7 +8036,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 1L) != 0)) if (((_bits & 1L) != 0))
{ {
foreach(var value in _CacheControl) foreach (var value in _CacheControl)
{ {
if (value != null) if (value != null)
{ {
@ -8045,8 +8051,9 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (_rawConnection != null) if (_rawConnection != null)
{ {
output.CopyFrom(_rawConnection, 0, _rawConnection.Length); output.CopyFrom(_rawConnection, 0, _rawConnection.Length);
} else }
foreach(var value in _Connection) else
foreach (var value in _Connection)
{ {
if (value != null) if (value != null)
{ {
@ -8061,8 +8068,9 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (_rawDate != null) if (_rawDate != null)
{ {
output.CopyFrom(_rawDate, 0, _rawDate.Length); output.CopyFrom(_rawDate, 0, _rawDate.Length);
} else }
foreach(var value in _Date) else
foreach (var value in _Date)
{ {
if (value != null) if (value != null)
{ {
@ -8074,7 +8082,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 8L) != 0)) if (((_bits & 8L) != 0))
{ {
foreach(var value in _KeepAlive) foreach (var value in _KeepAlive)
{ {
if (value != null) if (value != null)
{ {
@ -8086,7 +8094,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 16L) != 0)) if (((_bits & 16L) != 0))
{ {
foreach(var value in _Pragma) foreach (var value in _Pragma)
{ {
if (value != null) if (value != null)
{ {
@ -8098,7 +8106,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 32L) != 0)) if (((_bits & 32L) != 0))
{ {
foreach(var value in _Trailer) foreach (var value in _Trailer)
{ {
if (value != null) if (value != null)
{ {
@ -8113,8 +8121,9 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (_rawTransferEncoding != null) if (_rawTransferEncoding != null)
{ {
output.CopyFrom(_rawTransferEncoding, 0, _rawTransferEncoding.Length); output.CopyFrom(_rawTransferEncoding, 0, _rawTransferEncoding.Length);
} else }
foreach(var value in _TransferEncoding) else
foreach (var value in _TransferEncoding)
{ {
if (value != null) if (value != null)
{ {
@ -8126,7 +8135,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 128L) != 0)) if (((_bits & 128L) != 0))
{ {
foreach(var value in _Upgrade) foreach (var value in _Upgrade)
{ {
if (value != null) if (value != null)
{ {
@ -8138,7 +8147,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 256L) != 0)) if (((_bits & 256L) != 0))
{ {
foreach(var value in _Via) foreach (var value in _Via)
{ {
if (value != null) if (value != null)
{ {
@ -8150,7 +8159,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 512L) != 0)) if (((_bits & 512L) != 0))
{ {
foreach(var value in _Warning) foreach (var value in _Warning)
{ {
if (value != null) if (value != null)
{ {
@ -8162,7 +8171,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 1024L) != 0)) if (((_bits & 1024L) != 0))
{ {
foreach(var value in _Allow) foreach (var value in _Allow)
{ {
if (value != null) if (value != null)
{ {
@ -8177,8 +8186,9 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (_rawContentLength != null) if (_rawContentLength != null)
{ {
output.CopyFrom(_rawContentLength, 0, _rawContentLength.Length); output.CopyFrom(_rawContentLength, 0, _rawContentLength.Length);
} else }
foreach(var value in _ContentLength) else
foreach (var value in _ContentLength)
{ {
if (value != null) if (value != null)
{ {
@ -8190,7 +8200,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 4096L) != 0)) if (((_bits & 4096L) != 0))
{ {
foreach(var value in _ContentType) foreach (var value in _ContentType)
{ {
if (value != null) if (value != null)
{ {
@ -8202,7 +8212,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 8192L) != 0)) if (((_bits & 8192L) != 0))
{ {
foreach(var value in _ContentEncoding) foreach (var value in _ContentEncoding)
{ {
if (value != null) if (value != null)
{ {
@ -8214,7 +8224,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 16384L) != 0)) if (((_bits & 16384L) != 0))
{ {
foreach(var value in _ContentLanguage) foreach (var value in _ContentLanguage)
{ {
if (value != null) if (value != null)
{ {
@ -8226,7 +8236,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 32768L) != 0)) if (((_bits & 32768L) != 0))
{ {
foreach(var value in _ContentLocation) foreach (var value in _ContentLocation)
{ {
if (value != null) if (value != null)
{ {
@ -8238,7 +8248,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 65536L) != 0)) if (((_bits & 65536L) != 0))
{ {
foreach(var value in _ContentMD5) foreach (var value in _ContentMD5)
{ {
if (value != null) if (value != null)
{ {
@ -8250,7 +8260,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 131072L) != 0)) if (((_bits & 131072L) != 0))
{ {
foreach(var value in _ContentRange) foreach (var value in _ContentRange)
{ {
if (value != null) if (value != null)
{ {
@ -8262,7 +8272,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 262144L) != 0)) if (((_bits & 262144L) != 0))
{ {
foreach(var value in _Expires) foreach (var value in _Expires)
{ {
if (value != null) if (value != null)
{ {
@ -8274,7 +8284,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 524288L) != 0)) if (((_bits & 524288L) != 0))
{ {
foreach(var value in _LastModified) foreach (var value in _LastModified)
{ {
if (value != null) if (value != null)
{ {
@ -8286,7 +8296,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 1048576L) != 0)) if (((_bits & 1048576L) != 0))
{ {
foreach(var value in _AcceptRanges) foreach (var value in _AcceptRanges)
{ {
if (value != null) if (value != null)
{ {
@ -8298,7 +8308,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 2097152L) != 0)) if (((_bits & 2097152L) != 0))
{ {
foreach(var value in _Age) foreach (var value in _Age)
{ {
if (value != null) if (value != null)
{ {
@ -8310,7 +8320,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 4194304L) != 0)) if (((_bits & 4194304L) != 0))
{ {
foreach(var value in _ETag) foreach (var value in _ETag)
{ {
if (value != null) if (value != null)
{ {
@ -8322,7 +8332,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 8388608L) != 0)) if (((_bits & 8388608L) != 0))
{ {
foreach(var value in _Location) foreach (var value in _Location)
{ {
if (value != null) if (value != null)
{ {
@ -8334,7 +8344,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 16777216L) != 0)) if (((_bits & 16777216L) != 0))
{ {
foreach(var value in _ProxyAutheticate) foreach (var value in _ProxyAutheticate)
{ {
if (value != null) if (value != null)
{ {
@ -8346,7 +8356,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 33554432L) != 0)) if (((_bits & 33554432L) != 0))
{ {
foreach(var value in _RetryAfter) foreach (var value in _RetryAfter)
{ {
if (value != null) if (value != null)
{ {
@ -8361,8 +8371,9 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (_rawServer != null) if (_rawServer != null)
{ {
output.CopyFrom(_rawServer, 0, _rawServer.Length); output.CopyFrom(_rawServer, 0, _rawServer.Length);
} else }
foreach(var value in _Server) else
foreach (var value in _Server)
{ {
if (value != null) if (value != null)
{ {
@ -8374,7 +8385,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 134217728L) != 0)) if (((_bits & 134217728L) != 0))
{ {
foreach(var value in _SetCookie) foreach (var value in _SetCookie)
{ {
if (value != null) if (value != null)
{ {
@ -8386,7 +8397,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 268435456L) != 0)) if (((_bits & 268435456L) != 0))
{ {
foreach(var value in _Vary) foreach (var value in _Vary)
{ {
if (value != null) if (value != null)
{ {
@ -8398,7 +8409,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (((_bits & 536870912L) != 0)) if (((_bits & 536870912L) != 0))
{ {
foreach(var value in _WWWAuthenticate) foreach (var value in _WWWAuthenticate)
{ {
if (value != null) if (value != null)
{ {
@ -8411,8 +8422,13 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
} }
public unsafe void Append(byte[] keyBytes, int keyOffset, int keyLength, string value) public unsafe void Append(byte[] keyBytes, int keyOffset, int keyLength, string value)
{ {
fixed(byte* ptr = keyBytes) { var pUB = ptr + keyOffset; var pUL = (ulong*)pUB; var pUI = (uint*)pUB; var pUS = (ushort*)pUB; fixed (byte* ptr = &keyBytes[keyOffset])
switch(keyLength) {
var pUB = ptr;
var pUL = (ulong*)pUB;
var pUI = (uint*)pUB;
var pUS = (ushort*)pUB;
switch (keyLength)
{ {
case 13: case 13:
{ {
@ -8890,7 +8906,8 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
} }
} }
break; break;
}} }
}
var key = System.Text.Encoding.ASCII.GetString(keyBytes, keyOffset, keyLength); var key = System.Text.Encoding.ASCII.GetString(keyBytes, keyOffset, keyLength);
StringValues existing; StringValues existing;
Unknown.TryGetValue(key, out existing); Unknown.TryGetValue(key, out existing);

View File

@ -133,9 +133,9 @@ namespace Microsoft.AspNet.Server.Kestrel.Infrastructure
} }
else if (_block.End - _index >= sizeof(long)) else if (_block.End - _index >= sizeof(long))
{ {
fixed (byte* ptr = _block.Array) fixed (byte* ptr = &_block.Array[_index])
{ {
return *(long*)(ptr + _index); return *(long*)(ptr);
} }
} }
else if (_block.Next == null) else if (_block.Next == null)
@ -153,15 +153,15 @@ namespace Microsoft.AspNet.Server.Kestrel.Infrastructure
} }
long blockLong; long blockLong;
fixed (byte* ptr = _block.Array) fixed (byte* ptr = &_block.Array[_block.End - sizeof(long)])
{ {
blockLong = *(long*)(ptr + _block.End - sizeof(long)); blockLong = *(long*)(ptr);
} }
long nextLong; long nextLong;
fixed (byte* ptr = _block.Next.Array) fixed (byte* ptr = &_block.Next.Array[_block.Next.Start])
{ {
nextLong = *(long*)(ptr + _block.Next.Start); nextLong = *(long*)(ptr );
} }
return (blockLong >> (sizeof(long) - blockBytes) * 8) | (nextLong << (sizeof(long) - nextBytes) * 8); return (blockLong >> (sizeof(long) - blockBytes) * 8) | (nextLong << (sizeof(long) - nextBytes) * 8);
@ -667,9 +667,10 @@ namespace Microsoft.AspNet.Server.Kestrel.Infrastructure
bytesLeftInBlockMinusSpan = bytesLeftInBlock - 3; bytesLeftInBlockMinusSpan = bytesLeftInBlock - 3;
} }
fixed (byte* pOutput = block.Data.Array) fixed (byte* pOutput = &block.Data.Array[block.End])
{ {
var output = pOutput + block.End; //this line is needed to allow output be an register var
var output = pOutput;
var copied = 0; var copied = 0;
for (; input < inputEndMinusSpan && copied < bytesLeftInBlockMinusSpan; copied += 4) for (; input < inputEndMinusSpan && copied < bytesLeftInBlockMinusSpan; copied += 4)

View File

@ -63,7 +63,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Infrastructure
var bytes = Encoding.ASCII.GetBytes(str); var bytes = Encoding.ASCII.GetBytes(str);
fixed (byte* ptr = bytes) fixed (byte* ptr = &bytes[0])
{ {
return *(long*)ptr; return *(long*)ptr;
} }

View File

@ -239,7 +239,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
}} }}
protected override StringValues GetValueFast(string key) protected override StringValues GetValueFast(string key)
{{ {{
switch(key.Length) switch (key.Length)
{{{Each(loop.HeadersByLength, byLength => $@" {{{Each(loop.HeadersByLength, byLength => $@"
case {byLength.Key}: case {byLength.Key}:
{{{Each(byLength, header => $@" {{{Each(byLength, header => $@"
@ -265,7 +265,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
}} }}
protected override bool TryGetValueFast(string key, out StringValues value) protected override bool TryGetValueFast(string key, out StringValues value)
{{ {{
switch(key.Length) switch (key.Length)
{{{Each(loop.HeadersByLength, byLength => $@" {{{Each(loop.HeadersByLength, byLength => $@"
case {byLength.Key}: case {byLength.Key}:
{{{Each(byLength, header => $@" {{{Each(byLength, header => $@"
@ -290,7 +290,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
}} }}
protected override void SetValueFast(string key, StringValues value) protected override void SetValueFast(string key, StringValues value)
{{ {{
switch(key.Length) switch (key.Length)
{{{Each(loop.HeadersByLength, byLength => $@" {{{Each(loop.HeadersByLength, byLength => $@"
case {byLength.Key}: case {byLength.Key}:
{{{Each(byLength, header => $@" {{{Each(byLength, header => $@"
@ -308,7 +308,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
}} }}
protected override void AddValueFast(string key, StringValues value) protected override void AddValueFast(string key, StringValues value)
{{ {{
switch(key.Length) switch (key.Length)
{{{Each(loop.HeadersByLength, byLength => $@" {{{Each(loop.HeadersByLength, byLength => $@"
case {byLength.Key}: case {byLength.Key}:
{{{Each(byLength, header => $@" {{{Each(byLength, header => $@"
@ -330,7 +330,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
}} }}
protected override bool RemoveFast(string key) protected override bool RemoveFast(string key)
{{ {{
switch(key.Length) switch (key.Length)
{{{Each(loop.HeadersByLength, byLength => $@" {{{Each(loop.HeadersByLength, byLength => $@"
case {byLength.Key}: case {byLength.Key}:
{{{Each(byLength, header => $@" {{{Each(byLength, header => $@"
@ -388,8 +388,9 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (_raw{header.Identifier} != null) if (_raw{header.Identifier} != null)
{{ {{
output.CopyFrom(_raw{header.Identifier}, 0, _raw{header.Identifier}.Length); output.CopyFrom(_raw{header.Identifier}, 0, _raw{header.Identifier}.Length);
}} else ")} }}
foreach(var value in _{header.Identifier}) else ")}
foreach (var value in _{header.Identifier})
{{ {{
if (value != null) if (value != null)
{{ {{
@ -402,8 +403,13 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
}}" : "")} }}" : "")}
public unsafe void Append(byte[] keyBytes, int keyOffset, int keyLength, string value) public unsafe void Append(byte[] keyBytes, int keyOffset, int keyLength, string value)
{{ {{
fixed(byte* ptr = keyBytes) {{ var pUB = ptr + keyOffset; var pUL = (ulong*)pUB; var pUI = (uint*)pUB; var pUS = (ushort*)pUB; fixed (byte* ptr = &keyBytes[keyOffset])
switch(keyLength) {{
var pUB = ptr;
var pUL = (ulong*)pUB;
var pUI = (uint*)pUB;
var pUS = (ushort*)pUB;
switch (keyLength)
{{{Each(loop.HeadersByLength, byLength => $@" {{{Each(loop.HeadersByLength, byLength => $@"
case {byLength.Key}: case {byLength.Key}:
{{{Each(byLength, header => $@" {{{Each(byLength, header => $@"
@ -423,7 +429,8 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
}} }}
")}}} ")}}}
break; break;
")}}}}} ")}}}
}}
var key = System.Text.Encoding.ASCII.GetString(keyBytes, keyOffset, keyLength); var key = System.Text.Encoding.ASCII.GetString(keyBytes, keyOffset, keyLength);
StringValues existing; StringValues existing;
Unknown.TryGetValue(key, out existing); Unknown.TryGetValue(key, out existing);
@ -462,8 +469,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
}} }}
}} }}
}} }}
")}}} ")}}}";
";
} }
public virtual void AfterCompile(AfterCompileContext context) public virtual void AfterCompile(AfterCompileContext context)
{ {