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 3 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))
Copy link
Member

Choose a reason for hiding this comment

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

how does this change improve performance?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Since the immediate action after the ContainsKey is to mutate the value at that key, using TryGetValue avoids a redundant dictionary lookup

C# lint CA1854

Copy link
Member

Choose a reason for hiding this comment

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

okay, seems like a pretty safe change, though under profiling does it make an actual difference here?

{
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
2 changes: 1 addition & 1 deletion src/DynamoCore/Graph/Workspaces/HomeWorkspaceModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -897,7 +897,7 @@ internal IList<string> GetOrphanedSerializablesAndClearHistoricalTraceData()

if (Nodes.All(n => n.GUID != 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
39 changes: 25 additions & 14 deletions src/DynamoCore/Models/DynamoModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1260,22 +1260,30 @@ 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;

var ws = maybeWs as HomeWorkspaceModel;
if (ws == null)
continue;

// Get the orphaned serializables to this workspace
var wsOrphans = ws.GetOrphanedSerializablesAndClearHistoricalTraceData().ToList();
var wsOrphans = (List<string>)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 +1295,26 @@ 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 (!nodeToWorkspaceMap.TryGetValue(nodeGuid, out var nodeSpace))
mjkkirschner marked this conversation as resolved.
Show resolved Hide resolved
continue;

//var nodeSpace =
bendikberg marked this conversation as resolved.
Show resolved Hide resolved
// Workspaces.FirstOrDefault(
// ws =>
// ws.Nodes.FirstOrDefault(n => n.GUID == nodeGuid)
// != null);

if (nodeSpace == null) continue;
//if (nodeSpace == null) 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
21 changes: 11 additions & 10 deletions src/Engine/ProtoCore/Lang/CallSite.cs
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,20 @@ public bool Contains(string data)
public List<string> RecursiveGetNestedData()
{
List<string> ret = new List<string>();
RecursiveFillWithNestedData(ret);
return ret;
}

public void RecursiveFillWithNestedData(List<string> listToFill)
bendikberg marked this conversation as resolved.
Show resolved Hide resolved
bendikberg marked this conversation as resolved.
Show resolved Hide resolved
{
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 +475,11 @@ private static string CompressSerializedTraceData(string json)
/// </summary>
public IList<string> GetOrphanedSerializables()
{
var result = new List<string>();

if (!beforeFirstRunSerializables.Any())
return result;
if (beforeFirstRunSerializables.Count == 0)
return new List<string>();
bendikberg marked this conversation as resolved.
Show resolved Hide resolved

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