From dfe55133c96ec199e21355f6fc1502104af885d6 Mon Sep 17 00:00:00 2001 From: ritorizo Date: Sun, 5 Jan 2025 20:54:10 +0100 Subject: [PATCH] saves some memory by not copying the list --- OpenDreamRuntime/Objects/Types/DreamList.cs | 24 +++++++++++---------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/OpenDreamRuntime/Objects/Types/DreamList.cs b/OpenDreamRuntime/Objects/Types/DreamList.cs index 7dd50d648a..5279f85068 100644 --- a/OpenDreamRuntime/Objects/Types/DreamList.cs +++ b/OpenDreamRuntime/Objects/Types/DreamList.cs @@ -297,12 +297,14 @@ public override DreamValue OperatorOr(DreamValue b, DMProcState state) { public override DreamValue OperatorAppend(DreamValue b) { if (b.TryGetValueAsDreamList(out var bList)) { - if (bList == this) bList = this.CreateCopy(); - foreach (DreamValue value in bList.GetValues()) { - if (bList._associativeValues?.TryGetValue(value, out var assocValue) is true) { - SetValue(value, assocValue); - } else { - AddValue(value); + var values = bList.GetValues(); + var valueCount = values.Count; // Some lists return a reference to their internal values list which could change with each loop + for (int i = 0; i < valueCount; i++) { + var value = values[i]; + AddValue(value); // Always add the value + if (bList._associativeValues?.TryGetValue(value, out var assocValue) is true) { // Ensure the associated value is correct + _associativeValues ??= new(); + _associativeValues[value] = assocValue; } } } else { @@ -575,9 +577,9 @@ public override int GetLength() { // atom's verbs list // Keeps track of an appearance's verbs (atom.verbs, mutable_appearance.verbs, etc) public sealed class VerbsList(DreamObjectTree objectTree, AtomManager atomManager, ServerVerbSystem? verbSystem, DreamObjectAtom atom) : DreamList(objectTree.List.ObjectDefinition, 0) { - public override DreamValue GetValue(DreamValue key) { + public override DreamValue GetValue(DreamValue key) { if (verbSystem == null) - return DreamValue.Null; + return DreamValue.Null; if (!key.TryGetValueAsInteger(out var index)) throw new Exception($"Invalid index into verbs list: {key}"); @@ -821,12 +823,12 @@ public override void SetValue(DreamValue key, DreamValue value, bool allowGrowth public override void AddValue(DreamValue value) { EntityUid entity; if (value.TryGetValueAsDreamObject(out var movable)) { - if(_visContents.Contains(movable)) + if (_visContents.Contains(movable)) return; // vis_contents cannot contain duplicates _visContents.Add(movable); entity = movable.Entity; } else if (value.TryGetValueAsDreamObject(out var turf)) { - if(_visContents.Contains(turf)) + if (_visContents.Contains(turf)) return; // vis_contents cannot contain duplicates _visContents.Add(turf); entity = EntityUid.Invalid; // TODO: Support turfs in vis_contents @@ -935,7 +937,7 @@ public override List GetValues() { } public override void SetValue(DreamValue key, DreamValue value, bool allowGrowth = false) { - if (!value.TryGetValueAsDreamObject(out var filterObject) &&!value.IsNull) + if (!value.TryGetValueAsDreamObject(out var filterObject) && !value.IsNull) throw new Exception($"Cannot set value of filter list to {value}"); if (!key.TryGetValueAsInteger(out var filterIndex) || filterIndex < 1) throw new Exception($"Invalid index into filter list: {key}");