Avoid string allocations in MiniYaml parsing.

- Stream lines in as memory rather than needing to realise a string for each line, via a new method in StreamExts.
- Use span to avoid string allocations during parsing until we want to realise the node itself, in MiniYaml.FromLines.
- Change several callsites to use the streaming extension method rather than string method where possible.
This commit is contained in:
RoosterDragon
2021-10-10 12:41:54 +01:00
committed by abcdefg30
parent 270c566570
commit 0f01df5474
9 changed files with 165 additions and 42 deletions

View File

@@ -0,0 +1,73 @@
#region Copyright & License Information
/*
* Copyright 2007-2021 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.IO;
using System.Linq;
using System.Text;
using NUnit.Framework;
namespace OpenRA.Test
{
[TestFixture]
class StreamExtsTests
{
[TestCase(TestName = "ReadAllLines is equivalent to ReadAllLinesAsMemory")]
public void ReadAllLines()
{
foreach (var source in new[]
{
"abc",
"abc\n",
"abc\r\n",
"abc\ndef",
"abc\r\ndef",
"abc\n\n\n",
"abc\r\n\r\n\r\n",
"abc\n\n\ndef",
"abc\r\n\r\n\r\ndef",
"abc\ndef\nghi\n",
"abc\r\ndef\r\nghi\r\n",
"abc\ndef\nghi\njkl",
"abc\r\ndef\r\nghi\r\njkl",
new string('a', 126),
new string('a', 126) + '\n',
new string('a', 126) + "\r\n",
new string('a', 126) + "b",
new string('a', 126) + "\nb",
new string('a', 126) + "\r\nb",
new string('a', 127),
new string('a', 127) + '\n',
new string('a', 127) + "\r\n",
new string('a', 127) + "b",
new string('a', 127) + "\nb",
new string('a', 127) + "\r\nb",
new string('a', 128),
new string('a', 128) + '\n',
new string('a', 128) + "\r\n",
new string('a', 128) + "b",
new string('a', 128) + "\nb",
new string('a', 128) + "\r\nb",
new string('a', 129),
new string('a', 129) + '\n',
new string('a', 129) + "\r\n",
new string('a', 129) + "b",
new string('a', 129) + "\nb",
new string('a', 129) + "\r\nb",
})
{
var bytes = Encoding.UTF8.GetBytes(source);
var lines = new MemoryStream(bytes).ReadAllLines().ToArray();
var linesAsMemory = new MemoryStream(bytes).ReadAllLinesAsMemory().Select(l => l.ToString()).ToArray();
Assert.That(linesAsMemory, Is.EquivalentTo(lines));
}
}
}
}