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

@ -3939,7 +3939,12 @@ 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])
{
var pUB = ptr;
var pUL = (ulong*)pUB;
var pUI = (uint*)pUB;
var pUS = (ushort*)pUB;
switch (keyLength) 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);
@ -8045,7 +8051,8 @@ 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 }
else
foreach (var value in _Connection) foreach (var value in _Connection)
{ {
if (value != null) if (value != null)
@ -8061,7 +8068,8 @@ 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 }
else
foreach (var value in _Date) foreach (var value in _Date)
{ {
if (value != null) if (value != null)
@ -8113,7 +8121,8 @@ 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 }
else
foreach (var value in _TransferEncoding) foreach (var value in _TransferEncoding)
{ {
if (value != null) if (value != null)
@ -8177,7 +8186,8 @@ 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 }
else
foreach (var value in _ContentLength) foreach (var value in _ContentLength)
{ {
if (value != null) if (value != null)
@ -8361,7 +8371,8 @@ 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 }
else
foreach (var value in _Server) foreach (var value in _Server)
{ {
if (value != null) if (value != null)
@ -8411,7 +8422,12 @@ 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])
{
var pUB = ptr;
var pUL = (ulong*)pUB;
var pUI = (uint*)pUB;
var pUS = (ushort*)pUB;
switch (keyLength) 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

@ -388,7 +388,8 @@ 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 ")} }}
else ")}
foreach (var value in _{header.Identifier}) foreach (var value in _{header.Identifier})
{{ {{
if (value != null) if (value != null)
@ -402,7 +403,12 @@ 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])
{{
var pUB = ptr;
var pUL = (ulong*)pUB;
var pUI = (uint*)pUB;
var pUS = (ushort*)pUB;
switch (keyLength) switch (keyLength)
{{{Each(loop.HeadersByLength, byLength => $@" {{{Each(loop.HeadersByLength, byLength => $@"
case {byLength.Key}: case {byLength.Key}:
@ -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)
{ {