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

Commit

Permalink
Avoid local init
Browse files Browse the repository at this point in the history
  • Loading branch information
benaadams committed Nov 1, 2015
1 parent 23d55b2 commit 3ae6670
Showing 1 changed file with 53 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -488,53 +488,73 @@ public int GetLength(MemoryPoolIterator end)
}
}

public unsafe string GetAsciiString(MemoryPoolIterator end)
private static unsafe string SingleBlockAsciiString(byte[] input, int offset, int length)
{
if (IsDefault || end.IsDefault)
// avoid declaring other local vars, or doing work with stackalloc
// to prevent the .locals init cil flag , see: https://github.com/dotnet/coreclr/issues/1279
char* output = stackalloc char[length];

return SingleBlockAsciiIter(output, input, offset, length);
}

private static unsafe string SingleBlockAsciiIter(char* output, byte[] input, int offset, int length)
{
// avoid declaring other local vars, or doing work with stackalloc
// to prevent the .locals init cil flag , see: https://github.com/dotnet/coreclr/issues/1279
for (var i = 0; i < length; i++)
{
return default(string);
output[i] = (char)input[i + offset];
}
return new string(output, 0, length);
}

if (end._block == _block)
{
var length = end._index - _index;
char* output = stackalloc char[length];
var input = _block.Array;
private static unsafe string MultiBlockAsciiString(MemoryPoolBlock block, int offset, int length)
{
// avoid declaring other local vars, or doing work with stackalloc
// to prevent the .locals init cil flag , see: https://github.com/dotnet/coreclr/issues/1279
char* output = stackalloc char[length];

for (var i = 0; i < length; i++)
{
output[i] = (char)input[i + _index];
}
return MultiBlockAsciiIter(output, block, offset, length);
}

return new string(output, 0, length);
}
else
private static unsafe string MultiBlockAsciiIter(char* output, MemoryPoolBlock block, int offset, int length)
{
// avoid declaring other local vars, or doing work with stackalloc
// to prevent the .locals init cil flag , see: https://github.com/dotnet/coreclr/issues/1279
while (length > 0)
{
var length = GetLength(end);
char* output = stackalloc char[length];

var block = _block;
var index = _index;
var remaining = length;
var following = block.End - offset;
var input = block.Array;

while (remaining > 0)
for (var i = 0; i < following; i++)
{
var following = block.End - index;
var input = _block.Array;
output[i] = (char)input[i + offset];
}

for (var i = 0; i < following; i++)
{
output[i] = (char)input[i + index];
}
length -= following;

remaining -= following;
block = block.Next;
offset = block.Start;
}

block = block.Next;
index = block.Start;
}
return new string(output, 0, length);
}

return new string(output, 0, length);
public string GetAsciiString(MemoryPoolIterator end)
{
// avoid declaring other local vars, or doing work with stackalloc
// to prevent the .locals init cil flag , see: https://github.com/dotnet/coreclr/issues/1279
if (IsDefault || end.IsDefault)
{
return default(string);
}

if (end._block == _block)
{
return SingleBlockAsciiString(_block.Array, _index, end._index - _index);
}

return MultiBlockAsciiString(_block, _index, GetLength(end));
}

public string GetUtf8String(MemoryPoolIterator end)
Expand Down

0 comments on commit 3ae6670

Please sign in to comment.