diff --git a/src/DynamoCore/Engine/EngineController.cs b/src/DynamoCore/Engine/EngineController.cs index eb5ec23b71a..76ef559eb1b 100644 --- a/src/DynamoCore/Engine/EngineController.cs +++ b/src/DynamoCore/Engine/EngineController.cs @@ -534,14 +534,14 @@ internal void ReconcileTraceDataAndNotify() var callsiteToOrphanMap = new Dictionary>(); foreach (var cs in liveRunnerServices.RuntimeCore.RuntimeData.CallsiteCache.Values) { - var orphanedSerializables = cs.GetOrphanedSerializables().ToList(); - if (callsiteToOrphanMap.ContainsKey(cs.CallSiteID)) + var orphanedSerializables = cs.GetOrphanedSerializables(); + if (callsiteToOrphanMap.TryGetValue(cs.CallSiteID, out var serializablesForCallsite)) { - callsiteToOrphanMap[cs.CallSiteID].AddRange(orphanedSerializables); + serializablesForCallsite.AddRange(orphanedSerializables); } else { - callsiteToOrphanMap.Add(cs.CallSiteID, orphanedSerializables); + callsiteToOrphanMap.Add(cs.CallSiteID, orphanedSerializables.ToList()); } } diff --git a/src/DynamoCore/Graph/Workspaces/HomeWorkspaceModel.cs b/src/DynamoCore/Graph/Workspaces/HomeWorkspaceModel.cs index 753a27c3da2..eb3f763027c 100644 --- a/src/DynamoCore/Graph/Workspaces/HomeWorkspaceModel.cs +++ b/src/DynamoCore/Graph/Workspaces/HomeWorkspaceModel.cs @@ -880,7 +880,7 @@ private void OnPreviewGraphCompleted(AsyncTask asyncTask) /// trace data but do not exist in the current CallSite data. /// /// - internal IList GetOrphanedSerializablesAndClearHistoricalTraceData() + internal List GetOrphanedSerializablesAndClearHistoricalTraceData() { var orphans = new List(); @@ -891,13 +891,14 @@ internal IList GetOrphanedSerializablesAndClearHistoricalTraceData() // then add the serializables for that guid to the list of // orphans. + var nodeLookup = Nodes.Select(n => n.GUID).ToHashSet(); foreach (var nodeData in historicalTraceData) { var nodeGuid = nodeData.Key; - if (Nodes.All(n => n.GUID != nodeGuid)) + if (!nodeLookup.Contains(nodeGuid)) { - orphans.AddRange(nodeData.Value.SelectMany(CallSite.GetAllSerializablesFromSingleRunTraceData).ToList()); + orphans.AddRange(nodeData.Value.SelectMany(CallSite.GetAllSerializablesFromSingleRunTraceData)); } } diff --git a/src/DynamoCore/Models/DynamoModel.cs b/src/DynamoCore/Models/DynamoModel.cs index 1e45473b1a9..85c2ba1d6d8 100644 --- a/src/DynamoCore/Models/DynamoModel.cs +++ b/src/DynamoCore/Models/DynamoModel.cs @@ -1276,22 +1276,35 @@ private void EngineController_TraceReconcliationComplete(TraceReconciliationEven // This dictionary gets redistributed into a dictionary keyed by the workspace id. var workspaceOrphanMap = new Dictionary>(); + var nodeToWorkspaceMap = new Dictionary(); - foreach (var ws in Workspaces.OfType()) + foreach (var maybeWs in Workspaces) { + foreach (var node in maybeWs.Nodes) + { + nodeToWorkspaceMap[node.GUID] = maybeWs; + } + + if (maybeWs is not HomeWorkspaceModel ws) + { + continue; + } + // Get the orphaned serializables to this workspace - var wsOrphans = ws.GetOrphanedSerializablesAndClearHistoricalTraceData().ToList(); + var wsOrphans = ws.GetOrphanedSerializablesAndClearHistoricalTraceData(); if (!wsOrphans.Any()) + { continue; + } - if (!workspaceOrphanMap.ContainsKey(ws.Guid)) + if (workspaceOrphanMap.TryGetValue(ws.Guid, out var workspaceOrphans)) { - workspaceOrphanMap.Add(ws.Guid, wsOrphans); + workspaceOrphans.AddRange(wsOrphans); } else { - workspaceOrphanMap[ws.Guid].AddRange(wsOrphans); + workspaceOrphanMap.Add(ws.Guid, wsOrphans); } } @@ -1303,23 +1316,20 @@ private void EngineController_TraceReconcliationComplete(TraceReconciliationEven // TODO: MAGN-7314 // Find the owning workspace for a node. - var nodeSpace = - Workspaces.FirstOrDefault( - ws => - ws.Nodes.FirstOrDefault(n => n.GUID == nodeGuid) - != null); - - if (nodeSpace == null) continue; + if (!nodeToWorkspaceMap.TryGetValue(nodeGuid, out var nodeSpace)) + { + continue; + } // Add the node's orphaned serializables to the workspace // orphan map. - if (workspaceOrphanMap.ContainsKey(nodeSpace.Guid)) + if (workspaceOrphanMap.TryGetValue(nodeSpace.Guid, out var workspaceOrphans)) { - workspaceOrphanMap[nodeSpace.Guid].AddRange(kvp.Value); + workspaceOrphans.AddRange(kvp.Value); } else { - workspaceOrphanMap.Add(nodeSpace.Guid, kvp.Value); + workspaceOrphanMap.Add(nodeSpace.Guid, kvp.Value.ToList()); } } diff --git a/src/Engine/ProtoCore/Lang/CallSite.cs b/src/Engine/ProtoCore/Lang/CallSite.cs index ea85ed6f996..db237faa6c3 100644 --- a/src/Engine/ProtoCore/Lang/CallSite.cs +++ b/src/Engine/ProtoCore/Lang/CallSite.cs @@ -155,17 +155,24 @@ public bool Contains(string data) public List RecursiveGetNestedData() { List ret = new List(); + RecursiveFillWithNestedData(ret); + return ret; + } + internal void RecursiveFillWithNestedData(List listToFill) + { if (HasData) - ret.Add(Data); + { + listToFill.Add(Data); + } if (HasNestedData) { foreach (SingleRunTraceData srtd in NestedData) - ret.AddRange(srtd.RecursiveGetNestedData()); + { + srtd.RecursiveFillWithNestedData(listToFill); + } } - - return ret; } } @@ -472,13 +479,13 @@ private static string CompressSerializedTraceData(string json) /// public IList GetOrphanedSerializables() { - var result = new List(); - if (!beforeFirstRunSerializables.Any()) - return result; + { + return new List(); + } - var currentSerializables = traceData.SelectMany(td => td.RecursiveGetNestedData()); - result.AddRange(beforeFirstRunSerializables.Where(hs => !currentSerializables.Contains(hs)).ToList()); + var currentSerializables = traceData.SelectMany(td => td.RecursiveGetNestedData()).ToHashSet(); + var result = beforeFirstRunSerializables.Where(hs => !currentSerializables.Contains(hs)).ToList(); // Clear the historical serializable to avoid // them being used again. diff --git a/src/NodeServices/TraceSupport.cs b/src/NodeServices/TraceSupport.cs index ff9e170f70f..6e48bdc6acc 100644 --- a/src/NodeServices/TraceSupport.cs +++ b/src/NodeServices/TraceSupport.cs @@ -1,6 +1,5 @@ using System; using System.Collections.Generic; -using System.Runtime.Serialization; namespace DynamoServices { @@ -70,15 +69,9 @@ internal static void ClearAllKnownTLSKeys() /// public static string GetTraceData(string key) { - string data; - if (!LocalStorageSlot.TryGetValue(key, out data)) - { - return null; - } - else - { + if (LocalStorageSlot.TryGetValue(key, out string data)) return data; - } + return null; } /// @@ -88,14 +81,7 @@ public static string GetTraceData(string key) /// public static void SetTraceData(string key, string value) { - if (LocalStorageSlot.ContainsKey(key)) - { - LocalStorageSlot[key] = value; - } - else - { - LocalStorageSlot.Add(key, value); - } + LocalStorageSlot[key] = value; } } }