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

@ -133,9 +133,9 @@ namespace Microsoft.AspNet.Server.Kestrel.Infrastructure
}
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)
@ -153,15 +153,15 @@ namespace Microsoft.AspNet.Server.Kestrel.Infrastructure
}
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;
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);
@ -667,9 +667,10 @@ namespace Microsoft.AspNet.Server.Kestrel.Infrastructure
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;
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);
fixed (byte* ptr = bytes)
fixed (byte* ptr = &bytes[0])
{
return *(long*)ptr;
}

View File

@ -239,7 +239,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
}}
protected override StringValues GetValueFast(string key)
{{
switch(key.Length)
switch (key.Length)
{{{Each(loop.HeadersByLength, byLength => $@"
case {byLength.Key}:
{{{Each(byLength, header => $@"
@ -265,7 +265,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
}}
protected override bool TryGetValueFast(string key, out StringValues value)
{{
switch(key.Length)
switch (key.Length)
{{{Each(loop.HeadersByLength, byLength => $@"
case {byLength.Key}:
{{{Each(byLength, header => $@"
@ -290,7 +290,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
}}
protected override void SetValueFast(string key, StringValues value)
{{
switch(key.Length)
switch (key.Length)
{{{Each(loop.HeadersByLength, byLength => $@"
case {byLength.Key}:
{{{Each(byLength, header => $@"
@ -308,7 +308,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
}}
protected override void AddValueFast(string key, StringValues value)
{{
switch(key.Length)
switch (key.Length)
{{{Each(loop.HeadersByLength, byLength => $@"
case {byLength.Key}:
{{{Each(byLength, header => $@"
@ -330,7 +330,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
}}
protected override bool RemoveFast(string key)
{{
switch(key.Length)
switch (key.Length)
{{{Each(loop.HeadersByLength, byLength => $@"
case {byLength.Key}:
{{{Each(byLength, header => $@"
@ -388,42 +388,49 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
if (_raw{header.Identifier} != null)
{{
output.CopyFrom(_raw{header.Identifier}, 0, _raw{header.Identifier}.Length);
}} else ")}
foreach(var value in _{header.Identifier})
{{
if (value != null)
}}
else ")}
foreach (var value in _{header.Identifier})
{{
output.CopyFrom(_headerBytes, {header.BytesOffset}, {header.BytesCount});
output.CopyFromAscii(value);
if (value != null)
{{
output.CopyFrom(_headerBytes, {header.BytesOffset}, {header.BytesCount});
output.CopyFromAscii(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;
switch(keyLength)
{{{Each(loop.HeadersByLength, byLength => $@"
case {byLength.Key}:
{{{Each(byLength, header => $@"
if ({header.EqualIgnoreCaseBytes()})
{{
if ({header.TestBit()})
fixed (byte* ptr = &keyBytes[keyOffset])
{{
var pUB = ptr;
var pUL = (ulong*)pUB;
var pUI = (uint*)pUB;
var pUS = (ushort*)pUB;
switch (keyLength)
{{{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
{{
{header.SetBit()};
_{header.Identifier} = new StringValues(value);{(header.EnhancedSetter == false ? "" : $@"
_raw{header.Identifier} = null;")}
}}
return;
}}
")}}}
break;
")}}}}}
")}}}
break;
")}}}
}}
var key = System.Text.Encoding.ASCII.GetString(keyBytes, keyOffset, keyLength);
StringValues existing;
Unknown.TryGetValue(key, out existing);
@ -462,8 +469,7 @@ namespace Microsoft.AspNet.Server.Kestrel.Http
}}
}}
}}
")}}}
";
")}}}";
}
public virtual void AfterCompile(AfterCompileContext context)
{