Skip to content

Commit

Permalink
avoids a few alloc
Browse files Browse the repository at this point in the history
  • Loading branch information
jakubmisek committed Aug 14, 2024
1 parent 83f887b commit 9e77131
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions src/Peachpie.Runtime/PhpString.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ public string ToString(Encoding encoding)
if (chunks != null)
{
return (chunks is object[] objs)
? ChunkToString(encoding, objs, _chunksCount)
? ChunkToString(encoding, objs.AsSpan(0, _chunksCount))
: ChunkToString(encoding, chunks);
}
else
Expand All @@ -1099,19 +1099,23 @@ public string ToString(Encoding encoding)
}
}

static string ChunkToString(Encoding encoding, object[] chunks, int count)
static string ChunkToString(Encoding encoding, ReadOnlySpan<object> chunks)
{
if (count == 1)
if (chunks.Length == 1)
{
return ChunkToString(encoding, chunks[0]);
}
else if (chunks.Length == 2)
{
return ChunkToString(encoding, chunks[0]) + ChunkToString(encoding, chunks[1]);
}
else
{
var builder = ObjectPools.GetStringBuilder();

for (int i = 0; i < count; i++)
foreach (var chunk in chunks)
{
builder.Append(ChunkToString(encoding, chunks[i]));
builder.Append(ChunkToString(encoding, chunk));
}

return ObjectPools.GetStringAndReturn(builder);
Expand Down

0 comments on commit 9e77131

Please sign in to comment.