Merge branch 'anderman/faster-unsafe' into dev

This commit is contained in:
Stephen Halter 2016-01-05 12:33:14 -08:00
commit b1a4e8cd1e
4 changed files with 1385 additions and 1361 deletions

View File

@ -175,9 +175,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)
@ -195,15 +195,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);
@ -770,9 +770,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

@ -66,7 +66,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;
} }
@ -355,4 +355,4 @@ namespace Microsoft.AspNet.Server.Kestrel.Infrastructure
return false; return false;
} }
} }
} }

View File

@ -256,7 +256,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 => $@"
@ -282,7 +282,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 => $@"
@ -307,7 +307,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 => $@"
@ -325,7 +325,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 => $@"
@ -347,7 +347,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 => $@"
@ -405,42 +405,49 @@ 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)
{{ {{
output.CopyFrom(_headerBytes, {header.BytesOffset}, {header.BytesCount}); if (value != null)
output.CopyFromAscii(value); {{
output.CopyFrom(_headerBytes, {header.BytesOffset}, {header.BytesCount});
output.CopyFromAscii(value);
}}
}} }}
}}
}} }}
")} ")}
}}" : "")} }}" : "")}
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) {{
{{{Each(loop.HeadersByLength, byLength => $@" var pUB = ptr;
case {byLength.Key}: var pUL = (ulong*)pUB;
{{{Each(byLength, header => $@" var pUI = (uint*)pUB;
if ({header.EqualIgnoreCaseBytes()}) var pUS = (ushort*)pUB;
{{ switch (keyLength)
if ({header.TestBit()}) {{{Each(loop.HeadersByLength, byLength => $@"
case {byLength.Key}:
{{{Each(byLength, header => $@"
if ({header.EqualIgnoreCaseBytes()})
{{ {{
_{header.Identifier} = AppendValue(_{header.Identifier}, value); if ({header.TestBit()})
{{
_{header.Identifier} = AppendValue(_{header.Identifier}, value);
}}
else
{{
{header.SetBit()};
_{header.Identifier} = new StringValues(value);{(header.EnhancedSetter == false ? "" : $@"
_raw{header.Identifier} = null;")}
}}
return;
}} }}
else ")}}}
{{ break;
{header.SetBit()}; ")}}}
_{header.Identifier} = new StringValues(value);{(header.EnhancedSetter == false ? "" : $@" }}
_raw{header.Identifier} = null;")}
}}
return;
}}
")}}}
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);
@ -479,8 +486,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
}} }}
}} }}
}} }}
")}}} ")}}}";
";
} }
public virtual void AfterCompile(AfterCompileContext context) public virtual void AfterCompile(AfterCompileContext context)
{ {