Skip to content

Commit

Permalink
Don't unnecessarily allocate lists for TreeEntry.InheritingTypes
Browse files Browse the repository at this point in the history
  • Loading branch information
ike709 committed Oct 15, 2024
1 parent 9ee8585 commit 8852ebc
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions OpenDreamRuntime/Objects/DreamObjectTree.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,13 +125,15 @@ public bool TryGetGlobalProc(string name, [NotNullWhen(true)] out DreamProc? glo
public IEnumerable<TreeEntry> GetAllDescendants(TreeEntry treeEntry) {
yield return treeEntry;

foreach (int typeId in treeEntry.InheritingTypes) {
TreeEntry type = Types[typeId];
IEnumerator<TreeEntry> typeChildren = GetAllDescendants(type).GetEnumerator();
if (treeEntry.InheritingTypes is not null) {
foreach (int typeId in treeEntry.InheritingTypes) {
TreeEntry type = Types[typeId];
IEnumerator<TreeEntry> typeChildren = GetAllDescendants(type).GetEnumerator();

while (typeChildren.MoveNext()) yield return typeChildren.Current;
while (typeChildren.MoveNext()) yield return typeChildren.Current;

typeChildren.Dispose();
typeChildren.Dispose();
}
}
}

Expand Down Expand Up @@ -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<int>(1);
parent.InheritingTypes.Add(i);
type.ParentEntry = parent;
}
Expand Down Expand Up @@ -488,11 +490,13 @@ public void SetNativeProc(TreeEntry type, Func<AsyncNativeProc.State, Task<Dream
/// Enumerate the inheritance tree in post-order
/// </summary>
private IEnumerable<TreeEntry> TraversePostOrder(TreeEntry from) {
foreach (int typeId in from.InheritingTypes) {
TreeEntry type = Types[typeId];
using IEnumerator<TreeEntry> typeChildren = TraversePostOrder(type).GetEnumerator();
if (from.InheritingTypes is not null) {
foreach (int typeId in from.InheritingTypes) {
TreeEntry type = Types[typeId];
using IEnumerator<TreeEntry> typeChildren = TraversePostOrder(type).GetEnumerator();

while (typeChildren.MoveNext()) yield return typeChildren.Current;
while (typeChildren.MoveNext()) yield return typeChildren.Current;
}
}

yield return from;
Expand All @@ -505,7 +509,7 @@ public sealed class TreeEntry {
public readonly int Id;
public DreamObjectDefinition ObjectDefinition;
public TreeEntry ParentEntry;
public readonly List<int> InheritingTypes = new();
public List<int>? InheritingTypes;

/// <summary>
/// This node's index in the inheritance tree based on a depth-first search<br/>
Expand Down

0 comments on commit 8852ebc

Please sign in to comment.