Skip to content

Commit 57fa6d4

Browse files
jsm174freezy
authored andcommitted
unit: update SwitchLampUnit to cache based on json hash
1 parent 9746825 commit 57fa6d4

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

Runtime/Nodes/Lamps/SwitchLampUnit.cs

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,7 @@ public int idCount
7272
[DoNotSerialize]
7373
public List<ValueInput> LampIdValues { get; private set; }
7474

75-
private List<LampIdValue> _lampIdValueList;
76-
private int _hash;
75+
private Dictionary<int, LampIdValue> _lampIdValueCache = new Dictionary<int, LampIdValue>();
7776

7877
protected override void Definition()
7978
{
@@ -91,6 +90,8 @@ protected override void Definition()
9190
Requirement(valueInput, InputTrigger);
9291
}
9392

93+
_lampIdValueCache.Clear();
94+
9495
Succession(InputTrigger, OutputTrigger);
9596
}
9697

@@ -104,7 +105,13 @@ private ControlOutput Process(Flow flow)
104105
var value = flow.GetValue<int>(Value);
105106

106107
foreach (var lampIdValue in LampIdValues) {
107-
var obj = LampIdValue.FromJson(flow.GetValue<string>(lampIdValue));
108+
var json = flow.GetValue<string>(lampIdValue);
109+
110+
if (!_lampIdValueCache.ContainsKey(json.GetHashCode())) {
111+
_lampIdValueCache[json.GetHashCode()] = LampIdValue.FromJson(json);
112+
}
113+
114+
var obj = _lampIdValueCache[json.GetHashCode()];
108115
Gle.SetLamp(obj.id, obj.value == value ? 255f : 0f);
109116
}
110117

0 commit comments

Comments
 (0)