From 4f34d3edb3e30428f0df43e4ae9f151714a9e663 Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Sun, 18 Oct 2020 13:37:12 +0100 Subject: [PATCH] Improvements in MiniYaml allocation - Clone method will use the node count to create the correct capacity. - ResolveInherits will use the node count as the suggested initial capacity. - FromStream will now stream lines, rather than reading the whole file into memory and then splitting into lines. --- OpenRA.Game/MiniYaml.cs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/OpenRA.Game/MiniYaml.cs b/OpenRA.Game/MiniYaml.cs index 84b93788ed..455ffe0548 100644 --- a/OpenRA.Game/MiniYaml.cs +++ b/OpenRA.Game/MiniYaml.cs @@ -99,7 +99,10 @@ namespace OpenRA public MiniYaml Clone() { - return new MiniYaml(Value, Nodes.Select(n => n.Clone()).ToList()); + var clonedNodes = new MiniYamlNodes(Nodes.Count); + foreach (var node in Nodes) + clonedNodes.Add(node.Clone()); + return new MiniYaml(Value, clonedNodes); } public Dictionary ToDictionary() @@ -290,8 +293,15 @@ namespace OpenRA public static List FromStream(Stream s, string fileName = "", bool discardCommentsAndWhitespace = true, Dictionary stringPool = null) { + IEnumerable Lines(StreamReader reader) + { + string line; + while ((line = reader.ReadLine()) != null) + yield return line; + } + using (var reader = new StreamReader(s)) - return FromString(reader.ReadToEnd(), fileName, discardCommentsAndWhitespace, stringPool); + return FromLines(Lines(reader), fileName, discardCommentsAndWhitespace, stringPool); } public static List FromString(string text, string fileName = "", bool discardCommentsAndWhitespace = true, Dictionary stringPool = null) @@ -340,7 +350,7 @@ namespace OpenRA static List ResolveInherits(string key, MiniYaml node, Dictionary tree, Dictionary inherited) { - var resolved = new List(); + var resolved = new List(node.Nodes.Count); // Inheritance is tracked from parent->child, but not from child->parentsiblings. inherited = new Dictionary(inherited);