Remove unnecessary fixed blocks

This commit is contained in:
Ben Adams 2016-04-30 10:13:50 +01:00
parent 2aa10a64a2
commit 3a424f6aba
3 changed files with 78 additions and 102 deletions

View File

@ -18,6 +18,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Infrastructure
/// </summary> /// </summary>
public readonly IntPtr DataArrayPtr; public readonly IntPtr DataArrayPtr;
internal unsafe readonly byte* DataFixedPtr;
/// <summary> /// <summary>
/// The array segment describing the range of memory this block is tracking. The caller which has leased this block may only read and /// The array segment describing the range of memory this block is tracking. The caller which has leased this block may only read and
/// modify the memory in this range. /// modify the memory in this range.
@ -27,9 +29,10 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Infrastructure
/// <summary> /// <summary>
/// This object cannot be instantiated outside of the static Create method /// This object cannot be instantiated outside of the static Create method
/// </summary> /// </summary>
protected MemoryPoolBlock(IntPtr dataArrayPtr) unsafe protected MemoryPoolBlock(IntPtr dataArrayPtr)
{ {
DataArrayPtr = dataArrayPtr; DataArrayPtr = dataArrayPtr;
DataFixedPtr = (byte*)dataArrayPtr.ToPointer();
} }
/// <summary> /// <summary>

View File

@ -175,10 +175,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Infrastructure
} }
else if (_block.End - _index >= sizeof(long)) else if (_block.End - _index >= sizeof(long))
{ {
fixed (byte* ptr = &_block.Array[_index]) return *(long*)(_block.DataFixedPtr + _index);
{
return *(long*)(ptr);
}
} }
else if (_block.Next == null) else if (_block.Next == null)
{ {
@ -194,17 +191,9 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Infrastructure
return -1; return -1;
} }
long blockLong; var blockLong = *(long*)(_block.DataFixedPtr + _block.End - sizeof(long));
fixed (byte* ptr = &_block.Array[_block.End - sizeof(long)])
{
blockLong = *(long*)(ptr);
}
long nextLong; var nextLong = *(long*)(_block.Next.DataFixedPtr + _block.Next.Start);
fixed (byte* ptr = &_block.Next.Array[_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);
} }
@ -266,22 +255,20 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Infrastructure
#if !DEBUG #if !DEBUG
} }
#endif #endif
fixed (byte* ptr = &block.Array[index])
var pCurrent = (block.DataFixedPtr + index);
var pEnd = pCurrent + following;
do
{ {
var pCurrent = ptr; if (*pCurrent == byte0)
var pEnd = pCurrent + following;
do
{ {
if (*pCurrent == byte0) _block = block;
{ _index = index;
_block = block; return byte0;
_index = index; }
return byte0; pCurrent++;
} index++;
pCurrent++; } while (pCurrent < pEnd);
index++;
} while (pCurrent < pEnd);
}
following = 0; following = 0;
break; break;
@ -367,28 +354,25 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Infrastructure
#if !DEBUG #if !DEBUG
} }
#endif #endif
fixed (byte* ptr = &block.Array[index]) var pCurrent = (block.DataFixedPtr + index);
var pEnd = pCurrent + following;
do
{ {
var pCurrent = ptr; if (*pCurrent == byte0)
var pEnd = pCurrent + following;
do
{ {
if (*pCurrent == byte0) _block = block;
{ _index = index;
_block = block; return byte0;
_index = index; }
return byte0; if (*pCurrent == byte1)
} {
if (*pCurrent == byte1) _block = block;
{ _index = index;
_block = block; return byte1;
_index = index; }
return byte1; pCurrent++;
} index++;
pCurrent++; } while (pCurrent != pEnd);
index++;
} while (pCurrent != pEnd);
}
following = 0; following = 0;
break; break;
@ -502,34 +486,31 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Infrastructure
#if !DEBUG #if !DEBUG
} }
#endif #endif
fixed (byte* ptr = &block.Array[index]) var pCurrent = (block.DataFixedPtr + index);
var pEnd = pCurrent + following;
do
{ {
var pCurrent = ptr; if (*pCurrent == byte0)
var pEnd = pCurrent + following;
do
{ {
if (*pCurrent == byte0) _block = block;
{ _index = index;
_block = block; return byte0;
_index = index; }
return byte0; if (*pCurrent == byte1)
} {
if (*pCurrent == byte1) _block = block;
{ _index = index;
_block = block; return byte1;
_index = index; }
return byte1; if (*pCurrent == byte2)
} {
if (*pCurrent == byte2) _block = block;
{ _index = index;
_block = block; return byte2;
_index = index; }
return byte2; pCurrent++;
} index++;
pCurrent++; } while (pCurrent != pEnd);
index++;
} while (pCurrent != pEnd);
}
following = 0; following = 0;
break; break;
@ -808,30 +789,25 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Infrastructure
bytesLeftInBlockMinusSpan = bytesLeftInBlock - 3; bytesLeftInBlockMinusSpan = bytesLeftInBlock - 3;
} }
fixed (byte* pOutput = &block.Data.Array[block.End]) var output = (block.DataFixedPtr + block.End);
var copied = 0;
for (; input < inputEndMinusSpan && copied < bytesLeftInBlockMinusSpan; copied += 4)
{ {
//this line is needed to allow output be an register var *(output) = (byte)*(input);
var output = pOutput; *(output + 1) = (byte)*(input + 1);
*(output + 2) = (byte)*(input + 2);
var copied = 0; *(output + 3) = (byte)*(input + 3);
for (; input < inputEndMinusSpan && copied < bytesLeftInBlockMinusSpan; copied += 4) output += 4;
{ input += 4;
*(output) = (byte)*(input);
*(output + 1) = (byte)*(input + 1);
*(output + 2) = (byte)*(input + 2);
*(output + 3) = (byte)*(input + 3);
output += 4;
input += 4;
}
for (; input < inputEnd && copied < bytesLeftInBlock; copied++)
{
*(output++) = (byte)*(input++);
}
blockIndex += copied;
bytesLeftInBlockMinusSpan -= copied;
bytesLeftInBlock -= copied;
} }
for (; input < inputEnd && copied < bytesLeftInBlock; copied++)
{
*(output++) = (byte)*(input++);
}
blockIndex += copied;
bytesLeftInBlockMinusSpan -= copied;
bytesLeftInBlock -= copied;
} }
} }

View File

@ -116,9 +116,7 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Infrastructure
if (following > 0) if (following > 0)
{ {
fixed (byte* blockStart = block.Array) var input = block.DataFixedPtr + inputOffset;
{
var input = blockStart + inputOffset;
var i = 0; var i = 0;
while (i < following - 11) while (i < following - 11)
{ {
@ -167,9 +165,8 @@ namespace Microsoft.AspNetCore.Server.Kestrel.Infrastructure
output++; output++;
input++; input++;
} }
remaining -= following; remaining -= following;
}
} }
if (remaining == 0) if (remaining == 0)