From 8852ebc0b3a8ee828c9f623493449c8a73cd9257 Mon Sep 17 00:00:00 2001 From: ike709 Date: Mon, 14 Oct 2024 21:26:30 -0500 Subject: [PATCH] Don't unnecessarily allocate lists for TreeEntry.InheritingTypes --- OpenDreamRuntime/Objects/DreamObjectTree.cs | 26 ++++++++++++--------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/OpenDreamRuntime/Objects/DreamObjectTree.cs b/OpenDreamRuntime/Objects/DreamObjectTree.cs index 85d1f59f95..4d61d38e96 100644 --- a/OpenDreamRuntime/Objects/DreamObjectTree.cs +++ b/OpenDreamRuntime/Objects/DreamObjectTree.cs @@ -125,13 +125,15 @@ public bool TryGetGlobalProc(string name, [NotNullWhen(true)] out DreamProc? glo public IEnumerable GetAllDescendants(TreeEntry treeEntry) { yield return treeEntry; - foreach (int typeId in treeEntry.InheritingTypes) { - TreeEntry type = Types[typeId]; - IEnumerator typeChildren = GetAllDescendants(type).GetEnumerator(); + if (treeEntry.InheritingTypes is not null) { + foreach (int typeId in treeEntry.InheritingTypes) { + TreeEntry type = Types[typeId]; + IEnumerator typeChildren = GetAllDescendants(type).GetEnumerator(); - while (typeChildren.MoveNext()) yield return typeChildren.Current; + while (typeChildren.MoveNext()) yield return typeChildren.Current; - typeChildren.Dispose(); + typeChildren.Dispose(); + } } } @@ -311,7 +313,7 @@ private void LoadTypesFromJson(DreamTypeJson[] types, ProcDefinitionJson[]? proc if (jsonType.Parent != null) { TreeEntry parent = Types[jsonType.Parent.Value]; - + parent.InheritingTypes ??= new List(1); parent.InheritingTypes.Add(i); type.ParentEntry = parent; } @@ -488,11 +490,13 @@ public void SetNativeProc(TreeEntry type, Func private IEnumerable TraversePostOrder(TreeEntry from) { - foreach (int typeId in from.InheritingTypes) { - TreeEntry type = Types[typeId]; - using IEnumerator typeChildren = TraversePostOrder(type).GetEnumerator(); + if (from.InheritingTypes is not null) { + foreach (int typeId in from.InheritingTypes) { + TreeEntry type = Types[typeId]; + using IEnumerator typeChildren = TraversePostOrder(type).GetEnumerator(); - while (typeChildren.MoveNext()) yield return typeChildren.Current; + while (typeChildren.MoveNext()) yield return typeChildren.Current; + } } yield return from; @@ -505,7 +509,7 @@ public sealed class TreeEntry { public readonly int Id; public DreamObjectDefinition ObjectDefinition; public TreeEntry ParentEntry; - public readonly List InheritingTypes = new(); + public List? InheritingTypes; /// /// This node's index in the inheritance tree based on a depth-first search