Skip to content

Commit

Permalink
saves some memory by not copying the list
Browse files Browse the repository at this point in the history
  • Loading branch information
ritorizo committed Jan 5, 2025
1 parent b80daf3 commit dfe5513
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions OpenDreamRuntime/Objects/Types/DreamList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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}");

Expand Down Expand Up @@ -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<DreamObjectMovable>(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<DreamObjectTurf>(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
Expand Down Expand Up @@ -935,7 +937,7 @@ public override List<DreamValue> GetValues() {
}

public override void SetValue(DreamValue key, DreamValue value, bool allowGrowth = false) {
if (!value.TryGetValueAsDreamObject<DreamObjectFilter>(out var filterObject) &&!value.IsNull)
if (!value.TryGetValueAsDreamObject<DreamObjectFilter>(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}");
Expand Down

0 comments on commit dfe5513

Please sign in to comment.