Improve lookups of nodes by key in MiniYaml.

When handling the Nodes collection in MiniYaml, individual nodes are located via one of two methods:

// Lookup a single key with linear search.
var node = yaml.Nodes.FirstOrDefault(n => n.Key == "SomeKey");

// Convert to dictionary, expecting many key lookups.
var dict = nodes.ToDictionary();

// Lookup a single key in the dictionary.
var node = dict["SomeKey"];

To simplify lookup of individual keys via linear search, provide helper methods NodeWithKeyOrDefault and NodeWithKey. These helpers do the equivalent of Single{OrDefault} searches. Whilst this requires checking the whole list, it provides a useful correctness check. Two duplicated keys in TS yaml are fixed as a result. We can also optimize the helpers to not use LINQ, avoiding allocation of the delegate to search for a key.

Adjust existing code to use either lnear searches or dictionary lookups based on whether it will be resolving many keys. Resolving few keys can be done with linear searches to avoid building a dictionary. Resolving many keys should be done with a dictionary to avoid quaradtic runtime from repeated linear searches.
This commit is contained in:
RoosterDragon
2023-07-07 08:31:29 +01:00
committed by Matthias Mailänder
parent 0ab7caedd9
commit b7e0ed9b87
60 changed files with 196 additions and 196 deletions

View File

@@ -47,7 +47,7 @@ namespace OpenRA.Mods.Common.Graphics
IReadOnlyDictionary<string, ISpriteSequence> ISpriteSequenceLoader.ParseSequences(ModData modData, string tileset, SpriteCache cache, MiniYamlNode imageNode)
{
var sequences = new Dictionary<string, ISpriteSequence>();
var node = imageNode.Value.Nodes.SingleOrDefault(n => n.Key == "Defaults");
var node = imageNode.Value.NodeWithKeyOrDefault("Defaults");
var defaults = node?.Value ?? NoData;
imageNode = imageNode.WithValue(imageNode.Value.WithNodes(imageNode.Value.Nodes.Remove(node)));
@@ -262,7 +262,7 @@ namespace OpenRA.Mods.Common.Graphics
protected static T LoadField<T>(string key, T fallback, MiniYaml data, MiniYaml defaults = null)
{
var node = data.Nodes.FirstOrDefault(n => n.Key == key) ?? defaults?.Nodes.FirstOrDefault(n => n.Key == key);
var node = data.NodeWithKeyOrDefault(key) ?? defaults?.NodeWithKeyOrDefault(key);
if (node == null)
return fallback;
@@ -276,7 +276,7 @@ namespace OpenRA.Mods.Common.Graphics
protected static T LoadField<T>(SpriteSequenceField<T> field, MiniYaml data, MiniYaml defaults, out MiniYamlNode.SourceLocation location)
{
var node = data.Nodes.FirstOrDefault(n => n.Key == field.Key) ?? defaults?.Nodes.FirstOrDefault(n => n.Key == field.Key);
var node = data.NodeWithKeyOrDefault(field.Key) ?? defaults?.NodeWithKeyOrDefault(field.Key);
if (node == null)
{
location = default;
@@ -414,7 +414,7 @@ namespace OpenRA.Mods.Common.Graphics
var offset = LoadField(Offset, data, defaults);
var blendMode = LoadField(BlendMode, data, defaults);
var combineNode = data.Nodes.FirstOrDefault(n => n.Key == Combine.Key);
var combineNode = data.NodeWithKeyOrDefault(Combine.Key);
if (combineNode != null)
{
for (var i = 0; i < combineNode.Value.Nodes.Length; i++)