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

Improve trace reconciliation performance #15725

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions src/DynamoCore/Engine/EngineController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -533,14 +533,14 @@ internal void ReconcileTraceDataAndNotify()
var callsiteToOrphanMap = new Dictionary<Guid, List<string>>();
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))
mjkkirschner marked this conversation as resolved.
Show resolved Hide resolved
{
callsiteToOrphanMap[cs.CallSiteID].AddRange(orphanedSerializables);
serializablesForCallsite.AddRange(orphanedSerializables);
}
else
{
callsiteToOrphanMap.Add(cs.CallSiteID, orphanedSerializables);
callsiteToOrphanMap.Add(cs.CallSiteID, orphanedSerializables.ToList());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so orphanedSerializables is already a List<string> -

var result = new List<string>();
though it's declared here as an IList - I assume that this calls the List constructor passing in the existing list, have you verified there is any benefit to moving this ToList call around?

If it's really such a big performance impact you could probably change this data structure to be a dict of IList

}
}

Expand Down
7 changes: 4 additions & 3 deletions src/DynamoCore/Graph/Workspaces/HomeWorkspaceModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,7 @@ private void OnPreviewGraphCompleted(AsyncTask asyncTask)
/// trace data but do not exist in the current CallSite data.
/// </summary>
/// <returns></returns>
internal IList<string> GetOrphanedSerializablesAndClearHistoricalTraceData()
internal List<string> GetOrphanedSerializablesAndClearHistoricalTraceData()
{
var orphans = new List<string>();

Expand All @@ -891,13 +891,14 @@ internal IList<string> 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));
bendikberg marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
40 changes: 25 additions & 15 deletions src/DynamoCore/Models/DynamoModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1260,22 +1260,35 @@ private void EngineController_TraceReconcliationComplete(TraceReconciliationEven
// This dictionary gets redistributed into a dictionary keyed by the workspace id.

var workspaceOrphanMap = new Dictionary<Guid, List<string>>();
var nodeToWorkspaceMap = new Dictionary<Guid, WorkspaceModel>();

foreach (var ws in Workspaces.OfType<HomeWorkspaceModel>())
foreach (var maybeWs in Workspaces)
bendikberg marked this conversation as resolved.
Show resolved Hide resolved
{
foreach (var node in maybeWs.Nodes)
bendikberg marked this conversation as resolved.
Show resolved Hide resolved
{
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))
bendikberg marked this conversation as resolved.
Show resolved Hide resolved
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);
}
}

Expand All @@ -1287,23 +1300,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))
mjkkirschner marked this conversation as resolved.
Show resolved Hide resolved
{
continue;
}

// Add the node's orphaned serializables to the workspace
// orphan map.
if (workspaceOrphanMap.ContainsKey(nodeSpace.Guid))
bendikberg marked this conversation as resolved.
Show resolved Hide resolved
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());
}
}

Expand Down
25 changes: 16 additions & 9 deletions src/Engine/ProtoCore/Lang/CallSite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,24 @@ public bool Contains(string data)
public List<string> RecursiveGetNestedData()
{
List<string> ret = new List<string>();
RecursiveFillWithNestedData(ret);
return ret;
}

internal void RecursiveFillWithNestedData(List<string> 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;
}
}

Expand Down Expand Up @@ -472,13 +479,13 @@ private static string CompressSerializedTraceData(string json)
/// </summary>
public IList<string> GetOrphanedSerializables()
{
var result = new List<string>();

if (!beforeFirstRunSerializables.Any())
return result;
{
return new List<string>();
}

var currentSerializables = traceData.SelectMany(td => td.RecursiveGetNestedData());
result.AddRange(beforeFirstRunSerializables.Where(hs => !currentSerializables.Contains(hs)).ToList());
var currentSerializables = traceData.SelectMany(td => td.RecursiveGetNestedData()).ToHashSet();
bendikberg marked this conversation as resolved.
Show resolved Hide resolved
var result = beforeFirstRunSerializables.Where(hs => !currentSerializables.Contains(hs)).ToList();

// Clear the historical serializable to avoid
// them being used again.
Expand Down
20 changes: 3 additions & 17 deletions src/NodeServices/TraceSupport.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Runtime.Serialization;

namespace DynamoServices
{
Expand Down Expand Up @@ -70,15 +69,9 @@ internal static void ClearAllKnownTLSKeys()
/// <returns></returns>
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;
}

/// <summary>
Expand All @@ -88,14 +81,7 @@ public static string GetTraceData(string key)
/// <param name="value"></param>
public static void SetTraceData(string key, string value)
{
if (LocalStorageSlot.ContainsKey(key))
{
LocalStorageSlot[key] = value;
}
else
{
LocalStorageSlot.Add(key, value);
}
LocalStorageSlot[key] = value;
}
}
}
Loading