Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize some collections #2031

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 26 additions & 16 deletions OpenDreamRuntime/Objects/DreamObjectTree.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections.Frozen;
using System.Diagnostics.CodeAnalysis;
using System.Text.Json;
using System.Threading.Tasks;
Expand Down Expand Up @@ -47,8 +48,8 @@ public sealed class DreamObjectTree {
public TreeEntry Obj { get; private set; }
public TreeEntry Mob { get; private set; }

private readonly Dictionary<string, TreeEntry> _pathToType = new();
private Dictionary<string, int> _globalProcIds;
private FrozenDictionary<string, TreeEntry> _pathToType = FrozenDictionary<string, TreeEntry>.Empty;
private FrozenDictionary<string, int> _globalProcIds = FrozenDictionary<string, int>.Empty;

[Dependency] private readonly AtomManager _atomManager = default!;
[Dependency] private readonly DreamManager _dreamManager = default!;
Expand Down Expand Up @@ -124,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 @@ -267,14 +270,17 @@ private void LoadTypesFromJson(DreamTypeJson[] types, ProcDefinitionJson[]? proc

//First pass: Create types and set them up for initialization
Types[0] = Root;
var pathToType = new Dictionary<string, TreeEntry>(types.Length);
for (int i = 1; i < Types.Length; i++) {
var path = types[i].Path;
var type = new TreeEntry(path, i);

Types[i] = type;
_pathToType[path] = type;
pathToType[path] = type;
}

_pathToType = pathToType.ToFrozenDictionary();

World = GetTreeEntry("/world");
List = GetTreeEntry("/list");
Client = GetTreeEntry("/client");
Expand Down Expand Up @@ -307,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 @@ -426,13 +432,15 @@ private void LoadProcsFromJson(ProcDefinitionJson[]? jsonProcs, int[]? jsonGloba
}

if (jsonGlobalProcs != null) {
_globalProcIds = new(jsonGlobalProcs.Length);
Dictionary<string, int> globalProcIds = new(jsonGlobalProcs.Length);

foreach (var procId in jsonGlobalProcs) {
var proc = Procs[procId];

_globalProcIds.Add(proc.Name, procId);
globalProcIds.Add(proc.Name, procId);
}

_globalProcIds = globalProcIds.ToFrozenDictionary();
}
}

Expand Down Expand Up @@ -482,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 @@ -499,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
Loading