From f220a9f200ace78ec963f072adc98e1c15c8049a Mon Sep 17 00:00:00 2001 From: Ben Adams Date: Sat, 26 Dec 2015 07:38:08 +0000 Subject: [PATCH] Faster Peek --- .../Infrastructure/MemoryPoolIterator2.cs | 35 +++++++++---------- 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/MemoryPoolIterator2.cs b/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/MemoryPoolIterator2.cs index 08c4a75a35..207abfba52 100644 --- a/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/MemoryPoolIterator2.cs +++ b/src/Microsoft.AspNet.Server.Kestrel/Infrastructure/MemoryPoolIterator2.cs @@ -92,28 +92,22 @@ namespace Microsoft.AspNet.Server.Kestrel.Infrastructure public int Peek() { - if (_block == null) - { - return -1; - } - else if (_index < _block.End) - { - return _block.Array[_index]; - } - else if (_block.Next == null) + var block = _block; + if (block == null) { return -1; } - var block = _block.Next; - var index = block.Start; - while (true) + var index = _index; + + if (index < block.End) { - if (index < block.End) - { - return block.Array[index]; - } - else if (block.Next == null) + return block.Array[index]; + } + + do + { + if (block.Next == null) { return -1; } @@ -122,7 +116,12 @@ namespace Microsoft.AspNet.Server.Kestrel.Infrastructure block = block.Next; index = block.Start; } - } + + if (index < block.End) + { + return block.Array[index]; + } + } while (true); } public unsafe long PeekLong()