1
0
mirror of https://github.com/Siccity/xNode.git synced 2025-12-20 01:06:01 +08:00

reuse staticPorts dictionary to reduce number of allocations of memory, clear after every use and ensure capacity when we know we are going to add many entries

This commit is contained in:
Simon Rodriguez 2022-11-02 09:10:15 +01:00
parent a077ca136b
commit 5201808eff

View File

@ -9,6 +9,7 @@ namespace XNode {
private static PortDataCache portDataCache; private static PortDataCache portDataCache;
private static Dictionary<System.Type, Dictionary<string, string>> formerlySerializedAsCache; private static Dictionary<System.Type, Dictionary<string, string>> formerlySerializedAsCache;
private static Dictionary<System.Type, string> typeQualifiedNameCache; private static Dictionary<System.Type, string> typeQualifiedNameCache;
private static Dictionary<string, NodePort> staticPorts;
private static bool Initialized { get { return portDataCache != null; } } private static bool Initialized { get { return portDataCache != null; } }
public static string GetTypeQualifiedName(System.Type type) { public static string GetTypeQualifiedName(System.Type type) {
@ -26,7 +27,6 @@ namespace XNode {
public static void UpdatePorts(Node node, Dictionary<string, NodePort> ports) { public static void UpdatePorts(Node node, Dictionary<string, NodePort> ports) {
if (!Initialized) BuildCache(); if (!Initialized) BuildCache();
Dictionary<string, NodePort> staticPorts = new Dictionary<string, NodePort>();
Dictionary<string, List<NodePort>> removedPorts = new Dictionary<string, List<NodePort>>(); Dictionary<string, List<NodePort>> removedPorts = new Dictionary<string, List<NodePort>>();
System.Type nodeType = node.GetType(); System.Type nodeType = node.GetType();
@ -37,6 +37,7 @@ namespace XNode {
List<NodePort> typePortCache; List<NodePort> typePortCache;
if (portDataCache.TryGetValue(nodeType, out typePortCache)) { if (portDataCache.TryGetValue(nodeType, out typePortCache)) {
staticPorts.EnsureCapacity(typePortCache.Count);
for (int i = 0; i < typePortCache.Count; i++) { for (int i = 0; i < typePortCache.Count; i++) {
staticPorts.Add(typePortCache[i].fieldName, portDataCache[nodeType][i]); staticPorts.Add(typePortCache[i].fieldName, portDataCache[nodeType][i]);
} }
@ -105,6 +106,8 @@ namespace XNode {
listPort.connectionType = backingPort.connectionType; listPort.connectionType = backingPort.connectionType;
listPort.typeConstraint = backingPort.typeConstraint; listPort.typeConstraint = backingPort.typeConstraint;
} }
staticPorts.Clear();
} }
/// <summary> /// <summary>
@ -145,6 +148,7 @@ namespace XNode {
/// <summary> Cache node types </summary> /// <summary> Cache node types </summary>
private static void BuildCache() { private static void BuildCache() {
portDataCache = new PortDataCache(); portDataCache = new PortDataCache();
staticPorts = new Dictionary<string, NodePort>();
System.Type baseType = typeof(Node); System.Type baseType = typeof(Node);
List<System.Type> nodeTypes = new List<System.Type>(); List<System.Type> nodeTypes = new List<System.Type>();
System.Reflection.Assembly[] assemblies = System.AppDomain.CurrentDomain.GetAssemblies(); System.Reflection.Assembly[] assemblies = System.AppDomain.CurrentDomain.GetAssemblies();