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

Commit 17d282b

Browse files
committed
Only calculate end offset once
1 parent f858a10 commit 17d282b

File tree

6 files changed

+19
-16
lines changed

6 files changed

+19
-16
lines changed

src/Microsoft.AspNet.Server.Kestrel/Http/SocketInput.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,10 @@ public IncomingBuffer IncomingStart(int minimumSize)
6262
{
6363
lock (_sync)
6464
{
65-
if (_tail != null && minimumSize <= _tail.Data.Offset + _tail.Data.Count - _tail.End)
65+
if (_tail != null && minimumSize <= _tail.BlockEndOffset - _tail.End)
6666
{
6767
_pinned = _tail;
68-
var data = new ArraySegment<byte>(_pinned.Data.Array, _pinned.End, _pinned.Data.Offset + _pinned.Data.Count - _pinned.End);
68+
var data = new ArraySegment<byte>(_pinned.Data.Array, _pinned.End, _pinned.BlockEndOffset - _pinned.End);
6969
var dataPtr = _pinned.Pin() + _pinned.End;
7070
return new IncomingBuffer
7171
{

src/Microsoft.AspNet.Server.Kestrel/Http/SocketOutput.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -423,7 +423,7 @@ private static void BytesBetween(MemoryPoolIterator2 start, MemoryPoolIterator2
423423
return;
424424
}
425425

426-
bytes = start.Block.Data.Offset + start.Block.Data.Count - start.Index;
426+
bytes = start.Block.BlockEndOffset - start.Index;
427427
buffers = 1;
428428

429429
for (var block = start.Block.Next; block != end.Block; block = block.Next)

src/Microsoft.AspNet.Server.Kestrel/Infrastructure/MemoryPoolBlock2.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ protected MemoryPoolBlock2()
5252
/// </summary>
5353
public byte[] Array => Data.Array;
5454

55+
/// <summary>
56+
/// Fixed end offset of the block
57+
/// </summary>
58+
public int BlockEndOffset { get; private set; }
59+
5560
/// <summary>
5661
/// The Start represents the offset into Array where the range of "active" bytes begins. At the point when the block is leased
5762
/// the Start is guaranteed to be equal to Array.Offset. The value of Start may be assigned anywhere between Data.Offset and
@@ -144,6 +149,7 @@ public static MemoryPoolBlock2 Create(
144149
Slab = slab,
145150
Start = data.Offset,
146151
End = data.Offset,
152+
BlockEndOffset = data.Offset + data.Count
147153
};
148154
}
149155

src/Microsoft.AspNet.Server.Kestrel/Infrastructure/MemoryPoolIterator2.cs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ public unsafe void CopyFrom(byte[] data, int offset, int count)
593593
{
594594
var block = _block;
595595
var blockIndex = _index;
596-
var bytesLeftInBlock = block.Data.Offset + block.Data.Count - blockIndex;
596+
var bytesLeftInBlock = block.BlockEndOffset - blockIndex;
597597

598598
if (bytesLeftInBlock >= count)
599599
{
@@ -603,15 +603,11 @@ public unsafe void CopyFrom(byte[] data, int offset, int count)
603603
return;
604604
}
605605

606-
var pool = block.Pool;
607-
608-
while (count > 0)
606+
do
609607
{
610608
if (bytesLeftInBlock == 0)
611609
{
612-
block.End = blockIndex;
613-
614-
var nextBlock = pool.Lease();
610+
var nextBlock = block.Pool.Lease();
615611
blockIndex = nextBlock.Data.Offset;
616612
bytesLeftInBlock = nextBlock.Data.Count;
617613
block.Next = nextBlock;
@@ -622,9 +618,10 @@ public unsafe void CopyFrom(byte[] data, int offset, int count)
622618
{
623619
count -= bytesLeftInBlock;
624620
Buffer.BlockCopy(data, offset, block.Array, blockIndex, bytesLeftInBlock);
625-
blockIndex += bytesLeftInBlock;
626621
offset += bytesLeftInBlock;
622+
block.End = blockIndex + bytesLeftInBlock;
627623
bytesLeftInBlock = 0;
624+
continue;
628625
}
629626
else
630627
{
@@ -634,7 +631,7 @@ public unsafe void CopyFrom(byte[] data, int offset, int count)
634631
_block = block;
635632
return;
636633
}
637-
}
634+
} while (true);
638635
}
639636

640637
public unsafe void CopyFromAscii(string data)
@@ -649,7 +646,7 @@ public unsafe void CopyFromAscii(string data)
649646
var blockIndex = _index;
650647
var length = data.Length;
651648

652-
var bytesLeftInBlock = block.Data.Offset + block.Data.Count - blockIndex;
649+
var bytesLeftInBlock = block.BlockEndOffset - blockIndex;
653650
var bytesLeftInBlockMinusSpan = bytesLeftInBlock - 3;
654651

655652
fixed (char* pData = data)

src/Microsoft.AspNet.Server.Kestrel/Networking/UvWriteReq.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public unsafe void Write(
6666
for (var index = 0; index < nBuffers; index++)
6767
{
6868
var blockStart = block == start.Block ? start.Index : block.Data.Offset;
69-
var blockEnd = block == end.Block ? end.Index : block.Data.Offset + block.Data.Count;
69+
var blockEnd = block == end.Block ? end.Index : block.BlockEndOffset;
7070

7171
// create and pin each segment being written
7272
pBuffers[index] = Libuv.buf_init(

test/Microsoft.AspNet.Server.KestrelTests/SocketOutputTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -308,11 +308,11 @@ public void ProducingStartAndProducingCompleteCanBeUsedDirectly()
308308

309309
// block 1
310310
var start = socketOutput.ProducingStart();
311-
start.Block.End = start.Block.Data.Offset + start.Block.Data.Count;
311+
start.Block.End = start.Block.BlockEndOffset;
312312

313313
// block 2
314314
var block2 = memory.Lease();
315-
block2.End = block2.Data.Offset + block2.Data.Count;
315+
block2.End = block2.BlockEndOffset;
316316
start.Block.Next = block2;
317317

318318
var end = new MemoryPoolIterator2(block2, block2.End);

0 commit comments

Comments
 (0)