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

Freeze some collections for faster accessing #2023

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
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
5 changes: 3 additions & 2 deletions OpenDreamRuntime/Objects/DreamObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,12 @@ public bool IsSubtypeOf(TreeEntry ancestor) {
}

#region Variables

public virtual bool IsSaved(string name) {
return ObjectDefinition.Variables.ContainsKey(name)
&& !ObjectDefinition.GlobalVariables.ContainsKey(name)
&& !(ObjectDefinition.ConstVariables is not null && ObjectDefinition.ConstVariables.Contains(name))
&& !(ObjectDefinition.TmpVariables is not null && ObjectDefinition.TmpVariables.Contains(name));
&& !(ObjectDefinition.ConstVariables.Contains(name))
&& !(ObjectDefinition.TmpVariables.Contains(name));
}

public bool HasVariable(string name) {
Expand Down
26 changes: 14 additions & 12 deletions OpenDreamRuntime/Objects/DreamObjectDefinition.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using System.Collections.Frozen;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using OpenDreamRuntime.Procs;
using OpenDreamRuntime.Rendering;
Expand Down Expand Up @@ -51,12 +52,15 @@ public bool NoConstructors {

// Maps variables from their name to their initial value.
public readonly Dictionary<string, DreamValue> Variables = new();

// Maps /static variables from name to their index in the global variable table.
public readonly Dictionary<string, int> GlobalVariables = new();
public FrozenDictionary<string, int> GlobalVariables = FrozenDictionary<string, int>.Empty;

// Contains hashes of variables that are tagged /const.
public HashSet<string>? ConstVariables = null;
public FrozenSet<string> ConstVariables = FrozenSet<string>.Empty;

// Contains hashes of variables that are tagged /tmp.
public HashSet<string>? TmpVariables = null;
public FrozenSet<string> TmpVariables = FrozenSet<string>.Empty;

public DreamObjectDefinition(DreamObjectDefinition copyFrom) {
DreamManager = copyFrom.DreamManager;
Expand All @@ -79,9 +83,9 @@ public DreamObjectDefinition(DreamObjectDefinition copyFrom) {
InitializationProc = copyFrom.InitializationProc;

Variables = new Dictionary<string, DreamValue>(copyFrom.Variables);
GlobalVariables = new Dictionary<string, int>(copyFrom.GlobalVariables);
ConstVariables = copyFrom.ConstVariables is not null ? new HashSet<string>(copyFrom.ConstVariables) : null;
TmpVariables = copyFrom.TmpVariables is not null ? new HashSet<string>(copyFrom.TmpVariables) : null;
GlobalVariables = copyFrom.GlobalVariables;
ConstVariables = copyFrom.ConstVariables;
TmpVariables = copyFrom.TmpVariables;
Procs = new Dictionary<string, int>(copyFrom.Procs);
OverridingProcs = new Dictionary<string, int>(copyFrom.OverridingProcs);
if (copyFrom.Verbs != null)
Expand Down Expand Up @@ -113,11 +117,9 @@ public DreamObjectDefinition(DreamManager dreamManager, DreamObjectTree objectTr
if (Parent.Verbs != null)
Verbs = new List<int>(Parent.Verbs);
if (Parent != ObjectTree.Root.ObjectDefinition) // Don't include root-level globals
GlobalVariables = new Dictionary<string, int>(Parent.GlobalVariables);
if (Parent.ConstVariables != null)
ConstVariables = new HashSet<string>(Parent.ConstVariables);
if (Parent.TmpVariables != null)
TmpVariables = new HashSet<string>(Parent.TmpVariables);
GlobalVariables = Parent.GlobalVariables;
ConstVariables = Parent.ConstVariables;
TmpVariables = Parent.TmpVariables;
}
}

Expand Down
64 changes: 41 additions & 23 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 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 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 @@ -265,16 +268,20 @@
private void LoadTypesFromJson(DreamTypeJson[] types, ProcDefinitionJson[]? procs, int[]? globalProcs) {
Types = new TreeEntry[types.Length];

var pathToTypes = new Dictionary<string, TreeEntry>(types.Length);

//First pass: Create types and set them up for initialization
Types[0] = Root;
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;
pathToTypes[path] = type;
}

_pathToType = pathToTypes.ToFrozenDictionary();

World = GetTreeEntry("/world");
List = GetTreeEntry("/list");
Client = GetTreeEntry("/client");
Expand Down Expand Up @@ -307,7 +314,7 @@

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 @@ -387,23 +394,30 @@
}

if (jsonObject.GlobalVariables != null) {
Dictionary<string, int> globalVars = new Dictionary<string, int>(jsonObject.GlobalVariables.Count);
foreach (KeyValuePair<string, int> jsonGlobalVariable in jsonObject.GlobalVariables) {
objectDefinition.GlobalVariables.Add(jsonGlobalVariable.Key, jsonGlobalVariable.Value);
globalVars.Add(jsonGlobalVariable.Key, jsonGlobalVariable.Value);
}

objectDefinition.GlobalVariables = globalVars.ToFrozenDictionary();
}

if (jsonObject.ConstVariables != null) {
objectDefinition.ConstVariables ??= new();
HashSet<string> constVars = new HashSet<string>(jsonObject.ConstVariables.Count);
foreach (string jsonConstVariable in jsonObject.ConstVariables) {
objectDefinition.ConstVariables.Add(jsonConstVariable);
constVars.Add(jsonConstVariable);
}

objectDefinition.ConstVariables = constVars.ToFrozenSet();
}

if(jsonObject.TmpVariables != null) {
objectDefinition.TmpVariables ??= new();
HashSet<string> tmpVars = new HashSet<string>(jsonObject.TmpVariables.Count);
foreach (string jsonTmpVariable in jsonObject.TmpVariables) {
objectDefinition.TmpVariables.Add(jsonTmpVariable);
tmpVars.Add(jsonTmpVariable);
}

objectDefinition.TmpVariables = tmpVars.ToFrozenSet();
}
}

Expand All @@ -426,13 +440,15 @@
}

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 All @@ -454,14 +470,14 @@

internal void SetGlobalNativeProc(NativeProc.HandlerFn func) {
var (name, defaultArgumentValues, argumentNames) = NativeProc.GetNativeInfo(func);
var proc = new NativeProc(_globalProcIds[name], Root, name, argumentNames, defaultArgumentValues, func, _dreamManager, _atomManager, _dreamMapManager, _dreamResourceManager, _walkManager, this);
var proc = new NativeProc(_globalProcIds![name], Root, name, argumentNames, defaultArgumentValues, func, _dreamManager, _atomManager, _dreamMapManager, _dreamResourceManager, _walkManager, this);

Check warning

Code scanning / InspectCode

Redundant nullable warning suppression expression Warning

The nullable warning suppression expression is redundant

Check warning

Code scanning / InspectCode

Possible null reference argument for a parameter. Warning

Possible null reference argument for parameter 'defaultArgumentValues' in 'OpenDreamRuntime.Procs.NativeProc.NativeProc'

Procs[proc.Id] = proc;
}

public void SetGlobalNativeProc(Func<AsyncNativeProc.State, Task<DreamValue>> func) {
var (name, defaultArgumentValues, argumentNames) = NativeProc.GetNativeInfo(func);
var proc = new AsyncNativeProc(_globalProcIds[name], Root, name, argumentNames, defaultArgumentValues, func);
var proc = new AsyncNativeProc(_globalProcIds![name], Root, name, argumentNames, defaultArgumentValues, func);

Check warning

Code scanning / InspectCode

Redundant nullable warning suppression expression Warning

The nullable warning suppression expression is redundant

Check warning

Code scanning / InspectCode

Possible null reference argument for a parameter. Warning

Possible null reference argument for parameter 'defaultArgumentValues' in 'OpenDreamRuntime.Procs.AsyncNativeProc.AsyncNativeProc'

Procs[proc.Id] = proc;
}
Expand All @@ -482,11 +498,13 @@
/// 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 +517,7 @@
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
2 changes: 1 addition & 1 deletion OpenDreamRuntime/Objects/Types/DreamList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ public DreamGlobalVars(DreamObjectDefinition listDef) : base(listDef, 0) {

public override List<DreamValue> GetValues() {
var root = _objectTree.Root.ObjectDefinition;
List<DreamValue> values = new List<DreamValue>(root.GlobalVariables.Keys.Count - 1);
List<DreamValue> values = new List<DreamValue>(root.GlobalVariables.Keys.Length - 1);
// Skip world
foreach (var key in root.GlobalVariables.Keys.Skip(1)) {
values.Add(new DreamValue(key));
Expand Down
4 changes: 2 additions & 2 deletions OpenDreamRuntime/Procs/DMOpcodeHandlers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2227,8 +2227,8 @@ public static ProcStatus IsSaved(DMProcState state) {
}

if (objectDefinition.GlobalVariables.ContainsKey(property)
|| (objectDefinition.ConstVariables is not null && objectDefinition.ConstVariables.Contains(property))
|| (objectDefinition.TmpVariables is not null && objectDefinition.TmpVariables.Contains(property))) {
|| (objectDefinition.ConstVariables.Contains(property))
|| (objectDefinition.TmpVariables.Contains(property))) {
state.Push(new DreamValue(0));
} else {
state.Push(new DreamValue(1));
Expand Down
Loading