From c9685e9155a742a437140f41ae353fb5d4a14d94 Mon Sep 17 00:00:00 2001 From: Trice Helix <81759026+TriceHelix@users.noreply.github.com> Date: Sun, 12 Mar 2023 20:31:00 +0100 Subject: [PATCH] Fix NullReferenceException - Initialized "culledNodes" field to avoid rare NullReferenceException on line 440 - Slight performance boost using cached HashSets instead of allocating new lists DrawNodes() call, also O(1) lookup time for graphs with many selected/culled nodes --- Scripts/Editor/NodeEditorGUI.cs | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Scripts/Editor/NodeEditorGUI.cs b/Scripts/Editor/NodeEditorGUI.cs index 35b2e2a..8f3a574 100755 --- a/Scripts/Editor/NodeEditorGUI.cs +++ b/Scripts/Editor/NodeEditorGUI.cs @@ -1,4 +1,4 @@ -using System; +using System; using System.Collections.Generic; using System.Linq; using UnityEditor; @@ -12,8 +12,8 @@ namespace XNodeEditor { /// Contains GUI methods public partial class NodeEditorWindow { public NodeGraphEditor graphEditor; - private List selectionCache; - private List culledNodes; + private readonly HashSet selectionCache = new(); + private readonly HashSet culledNodes = new(); /// 19 if docked, 22 if not private int topPadding { get { return isDocked() ? 19 : 22; } } /// Executed after all other window GUI. Useful if Zoom is ruining your day. Automatically resets after being run. @@ -399,8 +399,13 @@ namespace XNodeEditor { private void DrawNodes() { Event e = Event.current; + if (e.type == EventType.Layout) { - selectionCache = new List(Selection.objects); + selectionCache.Clear(); + var objs = Selection.objects; + selectionCache.EnsureCapacity(objs.Length); + foreach (var obj in objs) + selectionCache.Add(obj); } System.Reflection.MethodInfo onValidate = null; @@ -432,7 +437,7 @@ namespace XNodeEditor { List removeEntries = new List(); - if (e.type == EventType.Layout) culledNodes = new List(); + if (e.type == EventType.Layout) culledNodes.Clear(); for (int n = 0; n < graph.nodes.Count; n++) { // Skip null nodes. The user could be in the process of renaming scripts, so removing them at this point is not advisable. if (graph.nodes[n] == null) continue;