Skip to content
This repository was archived by the owner on Dec 18, 2018. It is now read-only.

Commit fde6cbb

Browse files
committed
Remove unnecessary fixed blocks
1 parent f34deba commit fde6cbb

File tree

3 files changed

+78
-102
lines changed

3 files changed

+78
-102
lines changed

src/Microsoft.AspNetCore.Server.Kestrel/Infrastructure/MemoryPoolBlock.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ public class MemoryPoolBlock
1818
/// </summary>
1919
public readonly IntPtr DataArrayPtr;
2020

21+
internal unsafe readonly byte* DataFixedPtr;
22+
2123
/// <summary>
2224
/// The array segment describing the range of memory this block is tracking. The caller which has leased this block may only read and
2325
/// modify the memory in this range.
@@ -27,9 +29,10 @@ public class MemoryPoolBlock
2729
/// <summary>
2830
/// This object cannot be instantiated outside of the static Create method
2931
/// </summary>
30-
protected MemoryPoolBlock(IntPtr dataArrayPtr)
32+
unsafe protected MemoryPoolBlock(IntPtr dataArrayPtr)
3133
{
3234
DataArrayPtr = dataArrayPtr;
35+
DataFixedPtr = (byte*)dataArrayPtr.ToPointer();
3336
}
3437

3538
/// <summary>

src/Microsoft.AspNetCore.Server.Kestrel/Infrastructure/MemoryPoolIterator.cs

Lines changed: 72 additions & 96 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,7 @@ public unsafe long PeekLong()
175175
}
176176
else if (_block.End - _index >= sizeof(long))
177177
{
178-
fixed (byte* ptr = &_block.Array[_index])
179-
{
180-
return *(long*)(ptr);
181-
}
178+
return *(long*)(_block.DataFixedPtr + _index);
182179
}
183180
else if (_block.Next == null)
184181
{
@@ -194,17 +191,9 @@ public unsafe long PeekLong()
194191
return -1;
195192
}
196193

197-
long blockLong;
198-
fixed (byte* ptr = &_block.Array[_block.End - sizeof(long)])
199-
{
200-
blockLong = *(long*)(ptr);
201-
}
194+
var blockLong = *(long*)(_block.DataFixedPtr + _block.End - sizeof(long));
202195

203-
long nextLong;
204-
fixed (byte* ptr = &_block.Next.Array[_block.Next.Start])
205-
{
206-
nextLong = *(long*)(ptr);
207-
}
196+
var nextLong = *(long*)(_block.Next.DataFixedPtr + _block.Next.Start);
208197

209198
return (blockLong >> (sizeof(long) - blockBytes) * 8) | (nextLong << (sizeof(long) - nextBytes) * 8);
210199
}
@@ -266,22 +255,20 @@ public unsafe int Seek(ref Vector<byte> byte0Vector)
266255
#if !DEBUG
267256
}
268257
#endif
269-
fixed (byte* ptr = &block.Array[index])
258+
259+
var pCurrent = (block.DataFixedPtr + index);
260+
var pEnd = pCurrent + following;
261+
do
270262
{
271-
var pCurrent = ptr;
272-
var pEnd = pCurrent + following;
273-
do
263+
if (*pCurrent == byte0)
274264
{
275-
if (*pCurrent == byte0)
276-
{
277-
_block = block;
278-
_index = index;
279-
return byte0;
280-
}
281-
pCurrent++;
282-
index++;
283-
} while (pCurrent < pEnd);
284-
}
265+
_block = block;
266+
_index = index;
267+
return byte0;
268+
}
269+
pCurrent++;
270+
index++;
271+
} while (pCurrent < pEnd);
285272

286273
following = 0;
287274
break;
@@ -367,28 +354,25 @@ public unsafe int Seek(ref Vector<byte> byte0Vector, ref Vector<byte> byte1Vecto
367354
#if !DEBUG
368355
}
369356
#endif
370-
fixed (byte* ptr = &block.Array[index])
357+
var pCurrent = (block.DataFixedPtr + index);
358+
var pEnd = pCurrent + following;
359+
do
371360
{
372-
var pCurrent = ptr;
373-
var pEnd = pCurrent + following;
374-
do
361+
if (*pCurrent == byte0)
375362
{
376-
if (*pCurrent == byte0)
377-
{
378-
_block = block;
379-
_index = index;
380-
return byte0;
381-
}
382-
if (*pCurrent == byte1)
383-
{
384-
_block = block;
385-
_index = index;
386-
return byte1;
387-
}
388-
pCurrent++;
389-
index++;
390-
} while (pCurrent != pEnd);
391-
}
363+
_block = block;
364+
_index = index;
365+
return byte0;
366+
}
367+
if (*pCurrent == byte1)
368+
{
369+
_block = block;
370+
_index = index;
371+
return byte1;
372+
}
373+
pCurrent++;
374+
index++;
375+
} while (pCurrent != pEnd);
392376

393377
following = 0;
394378
break;
@@ -502,34 +486,31 @@ public unsafe int Seek(ref Vector<byte> byte0Vector, ref Vector<byte> byte1Vecto
502486
#if !DEBUG
503487
}
504488
#endif
505-
fixed (byte* ptr = &block.Array[index])
489+
var pCurrent = (block.DataFixedPtr + index);
490+
var pEnd = pCurrent + following;
491+
do
506492
{
507-
var pCurrent = ptr;
508-
var pEnd = pCurrent + following;
509-
do
493+
if (*pCurrent == byte0)
510494
{
511-
if (*pCurrent == byte0)
512-
{
513-
_block = block;
514-
_index = index;
515-
return byte0;
516-
}
517-
if (*pCurrent == byte1)
518-
{
519-
_block = block;
520-
_index = index;
521-
return byte1;
522-
}
523-
if (*pCurrent == byte2)
524-
{
525-
_block = block;
526-
_index = index;
527-
return byte2;
528-
}
529-
pCurrent++;
530-
index++;
531-
} while (pCurrent != pEnd);
532-
}
495+
_block = block;
496+
_index = index;
497+
return byte0;
498+
}
499+
if (*pCurrent == byte1)
500+
{
501+
_block = block;
502+
_index = index;
503+
return byte1;
504+
}
505+
if (*pCurrent == byte2)
506+
{
507+
_block = block;
508+
_index = index;
509+
return byte2;
510+
}
511+
pCurrent++;
512+
index++;
513+
} while (pCurrent != pEnd);
533514

534515
following = 0;
535516
break;
@@ -808,30 +789,25 @@ public unsafe void CopyFromAscii(string data)
808789
bytesLeftInBlockMinusSpan = bytesLeftInBlock - 3;
809790
}
810791

811-
fixed (byte* pOutput = &block.Data.Array[block.End])
792+
var output = (block.DataFixedPtr + block.End);
793+
var copied = 0;
794+
for (; input < inputEndMinusSpan && copied < bytesLeftInBlockMinusSpan; copied += 4)
812795
{
813-
//this line is needed to allow output be an register var
814-
var output = pOutput;
815-
816-
var copied = 0;
817-
for (; input < inputEndMinusSpan && copied < bytesLeftInBlockMinusSpan; copied += 4)
818-
{
819-
*(output) = (byte)*(input);
820-
*(output + 1) = (byte)*(input + 1);
821-
*(output + 2) = (byte)*(input + 2);
822-
*(output + 3) = (byte)*(input + 3);
823-
output += 4;
824-
input += 4;
825-
}
826-
for (; input < inputEnd && copied < bytesLeftInBlock; copied++)
827-
{
828-
*(output++) = (byte)*(input++);
829-
}
830-
831-
blockIndex += copied;
832-
bytesLeftInBlockMinusSpan -= copied;
833-
bytesLeftInBlock -= copied;
796+
*(output) = (byte)*(input);
797+
*(output + 1) = (byte)*(input + 1);
798+
*(output + 2) = (byte)*(input + 2);
799+
*(output + 3) = (byte)*(input + 3);
800+
output += 4;
801+
input += 4;
834802
}
803+
for (; input < inputEnd && copied < bytesLeftInBlock; copied++)
804+
{
805+
*(output++) = (byte)*(input++);
806+
}
807+
808+
blockIndex += copied;
809+
bytesLeftInBlockMinusSpan -= copied;
810+
bytesLeftInBlock -= copied;
835811
}
836812
}
837813

src/Microsoft.AspNetCore.Server.Kestrel/Infrastructure/MemoryPoolIteratorExtensions.cs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,9 +116,7 @@ public unsafe static string GetAsciiString(this MemoryPoolIterator start, Memory
116116

117117
if (following > 0)
118118
{
119-
fixed (byte* blockStart = block.Array)
120-
{
121-
var input = blockStart + inputOffset;
119+
var input = block.DataFixedPtr + inputOffset;
122120
var i = 0;
123121
while (i < following - 11)
124122
{
@@ -167,9 +165,8 @@ public unsafe static string GetAsciiString(this MemoryPoolIterator start, Memory
167165
output++;
168166
input++;
169167
}
170-
168+
171169
remaining -= following;
172-
}
173170
}
174171

175172
if (remaining == 0)

0 commit comments

Comments
 (0)