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

Switched Dict.ContainsKey out with Dict.TryGetValue for performance gain.

This commit is contained in:
Thor Brigsted 2018-09-09 18:25:41 +02:00
parent c424dfa4d7
commit ff97cc4494
7 changed files with 46 additions and 33 deletions

View File

@ -63,13 +63,15 @@ namespace XNodeEditor {
public virtual int GetWidth() { public virtual int GetWidth() {
Type type = target.GetType(); Type type = target.GetType();
if (NodeEditorWindow.nodeWidth.ContainsKey(type)) return NodeEditorWindow.nodeWidth[type]; int width;
if (NodeEditorWindow.nodeWidth.TryGetValue(type, out width)) return width;
else return 208; else return 208;
} }
public virtual Color GetTint() { public virtual Color GetTint() {
Type type = target.GetType(); Type type = target.GetType();
if (NodeEditorWindow.nodeTint.ContainsKey(type)) return NodeEditorWindow.nodeTint[type]; Color color;
if (NodeEditorWindow.nodeTint.TryGetValue(type, out color)) return color;
else return Color.white; else return Color.white;
} }
@ -98,5 +100,4 @@ namespace XNodeEditor {
} }
} }
} }
} }

View File

@ -90,8 +90,8 @@ namespace XNodeEditor {
// Offset portConnectionPoints instantly if a node is dragged so they aren't delayed by a frame. // Offset portConnectionPoints instantly if a node is dragged so they aren't delayed by a frame.
Vector2 offset = node.position - initial; Vector2 offset = node.position - initial;
if (offset.sqrMagnitude > 0){ if (offset.sqrMagnitude > 0) {
foreach (XNode.NodePort output in node.Outputs){ foreach (XNode.NodePort output in node.Outputs) {
Rect rect; Rect rect;
if (portConnectionPoints.TryGetValue(output, out rect)) { if (portConnectionPoints.TryGetValue(output, out rect)) {
rect.position += offset; rect.position += offset;
@ -381,9 +381,8 @@ namespace XNodeEditor {
XNode.NodePort inputPort = port.direction == XNode.NodePort.IO.Input ? port : port.GetConnection(c); XNode.NodePort inputPort = port.direction == XNode.NodePort.IO.Input ? port : port.GetConnection(c);
XNode.NodePort outputPort = port.direction == XNode.NodePort.IO.Output ? port : port.GetConnection(c); XNode.NodePort outputPort = port.direction == XNode.NodePort.IO.Output ? port : port.GetConnection(c);
if (substitutes.ContainsKey(inputPort.node) && substitutes.ContainsKey(outputPort.node)) { XNode.Node newNodeIn, newNodeOut;
XNode.Node newNodeIn = substitutes[inputPort.node]; if (substitutes.TryGetValue(inputPort.node, out newNodeIn) && substitutes.TryGetValue(outputPort.node, out newNodeOut)) {
XNode.Node newNodeOut = substitutes[outputPort.node];
newNodeIn.UpdateStaticPorts(); newNodeIn.UpdateStaticPorts();
newNodeOut.UpdateStaticPorts(); newNodeOut.UpdateStaticPorts();
inputPort = newNodeIn.GetInputPort(inputPort.fieldName); inputPort = newNodeIn.GetInputPort(inputPort.fieldName);
@ -402,9 +401,10 @@ namespace XNodeEditor {
if (IsDraggingPort) { if (IsDraggingPort) {
Color col = NodeEditorPreferences.GetTypeColor(draggedOutput.ValueType); Color col = NodeEditorPreferences.GetTypeColor(draggedOutput.ValueType);
if (!_portConnectionPoints.ContainsKey(draggedOutput)) return; Rect fromRect;
if (!_portConnectionPoints.TryGetValue(draggedOutput, out fromRect)) return;
Vector2 from = fromRect.center;
col.a = 0.6f; col.a = 0.6f;
Vector2 from = _portConnectionPoints[draggedOutput].center;
Vector2 to = Vector2.zero; Vector2 to = Vector2.zero;
for (int i = 0; i < draggedOutputReroutes.Count; i++) { for (int i = 0; i < draggedOutputReroutes.Count; i++) {
to = draggedOutputReroutes[i]; to = draggedOutputReroutes[i];
@ -435,8 +435,10 @@ namespace XNodeEditor {
Vector2 mousePos = Event.current.mousePosition; Vector2 mousePos = Event.current.mousePosition;
//Get node position //Get node position
Vector2 nodePos = GridToWindowPosition(node.position); Vector2 nodePos = GridToWindowPosition(node.position);
float width = 200; float width;
if (nodeSizes.ContainsKey(node)) width = nodeSizes[node].x; Vector2 size;
if (nodeSizes.TryGetValue(node, out size)) width = size.x;
else width = 200;
Rect windowRect = new Rect(nodePos, new Vector2(width / zoom, 30 / zoom)); Rect windowRect = new Rect(nodePos, new Vector2(width / zoom, 30 / zoom));
return windowRect.Contains(mousePos); return windowRect.Contains(mousePos);
} }

View File

@ -16,14 +16,15 @@ namespace XNodeEditor.Internal {
public static T GetEditor(K target) { public static T GetEditor(K target) {
if (target == null) return null; if (target == null) return null;
if (!editors.ContainsKey(target)) { T editor;
if (!editors.TryGetValue(target, out editor)) {
Type type = target.GetType(); Type type = target.GetType();
Type editorType = GetEditorType(type); Type editorType = GetEditorType(type);
editors.Add(target, Activator.CreateInstance(editorType) as T); editor = Activator.CreateInstance(editorType) as T;
editors[target].target = target; editor.target = target;
editors[target].serializedObject = new SerializedObject(target); editor.serializedObject = new SerializedObject(target);
editors.Add(target, editor);
} }
T editor = editors[target];
if (editor.target == null) editor.target = target; if (editor.target == null) editor.target = target;
if (editor.serializedObject == null) editor.serializedObject = new SerializedObject(target); if (editor.serializedObject == null) editor.serializedObject = new SerializedObject(target);
return editor; return editor;
@ -32,7 +33,8 @@ namespace XNodeEditor.Internal {
private static Type GetEditorType(Type type) { private static Type GetEditorType(Type type) {
if (type == null) return null; if (type == null) return null;
if (editorTypes == null) CacheCustomEditors(); if (editorTypes == null) CacheCustomEditors();
if (editorTypes.ContainsKey(type)) return editorTypes[type]; Type result;
if (editorTypes.TryGetValue(type, out result)) return result;
//If type isn't found, try base type //If type isn't found, try base type
return GetEditorType(type.BaseType); return GetEditorType(type.BaseType);
} }

View File

@ -226,7 +226,8 @@ namespace XNodeEditor {
// Draw full connections and output > reroute // Draw full connections and output > reroute
foreach (XNode.NodePort output in node.Outputs) { foreach (XNode.NodePort output in node.Outputs) {
//Needs cleanup. Null checks are ugly //Needs cleanup. Null checks are ugly
if (!portConnectionPoints.ContainsKey(output)) continue; Rect fromRect;
if (!_portConnectionPoints.TryGetValue(output, out fromRect)) continue;
Color connectionColor = graphEditor.GetTypeColor(output.ValueType); Color connectionColor = graphEditor.GetTypeColor(output.ValueType);
@ -236,9 +237,10 @@ namespace XNodeEditor {
// Error handling // Error handling
if (input == null) continue; //If a script has been updated and the port doesn't exist, it is removed and null is returned. If this happens, return. if (input == null) continue; //If a script has been updated and the port doesn't exist, it is removed and null is returned. If this happens, return.
if (!input.IsConnectedTo(output)) input.Connect(output); if (!input.IsConnectedTo(output)) input.Connect(output);
if (!_portConnectionPoints.ContainsKey(input)) continue; Rect toRect;
if (!_portConnectionPoints.TryGetValue(input, out toRect)) continue;
Vector2 from = _portConnectionPoints[output].center; Vector2 from = fromRect.center;
Vector2 to = Vector2.zero; Vector2 to = Vector2.zero;
List<Vector2> reroutePoints = output.GetReroutePoints(k); List<Vector2> reroutePoints = output.GetReroutePoints(k);
// Loop through reroute points and draw the path // Loop through reroute points and draw the path
@ -247,7 +249,8 @@ namespace XNodeEditor {
DrawConnection(from, to, connectionColor); DrawConnection(from, to, connectionColor);
from = to; from = to;
} }
to = _portConnectionPoints[input].center; to = toRect.center;
DrawConnection(from, to, connectionColor); DrawConnection(from, to, connectionColor);
// Loop through reroute points again and draw the points // Loop through reroute points again and draw the points

View File

@ -93,7 +93,8 @@ namespace XNodeEditor {
rect.size = new Vector2(16, 16); rect.size = new Vector2(16, 16);
Color backgroundColor = new Color32(90, 97, 105, 255); Color backgroundColor = new Color32(90, 97, 105, 255);
if (NodeEditorWindow.nodeTint.ContainsKey(port.node.GetType())) backgroundColor *= NodeEditorWindow.nodeTint[port.node.GetType()]; Color tint;
if (NodeEditorWindow.nodeTint.TryGetValue(port.node.GetType(), out tint)) backgroundColor *= tint;
Color col = NodeEditorWindow.current.graphEditor.GetTypeColor(port.ValueType); Color col = NodeEditorWindow.current.graphEditor.GetTypeColor(port.ValueType);
DrawPortHandle(rect, backgroundColor, col); DrawPortHandle(rect, backgroundColor, col);
@ -135,7 +136,8 @@ namespace XNodeEditor {
rect.size = new Vector2(16, 16); rect.size = new Vector2(16, 16);
Color backgroundColor = new Color32(90, 97, 105, 255); Color backgroundColor = new Color32(90, 97, 105, 255);
if (NodeEditorWindow.nodeTint.ContainsKey(port.node.GetType())) backgroundColor *= NodeEditorWindow.nodeTint[port.node.GetType()]; Color tint;
if (NodeEditorWindow.nodeTint.TryGetValue(port.node.GetType(), out tint)) backgroundColor *= tint;
Color col = NodeEditorWindow.current.graphEditor.GetTypeColor(port.ValueType); Color col = NodeEditorWindow.current.graphEditor.GetTypeColor(port.ValueType);
DrawPortHandle(rect, backgroundColor, col); DrawPortHandle(rect, backgroundColor, col);
@ -163,7 +165,8 @@ namespace XNodeEditor {
rect.size = new Vector2(16, 16); rect.size = new Vector2(16, 16);
Color backgroundColor = new Color32(90, 97, 105, 255); Color backgroundColor = new Color32(90, 97, 105, 255);
if (NodeEditorWindow.nodeTint.ContainsKey(port.node.GetType())) backgroundColor *= NodeEditorWindow.nodeTint[port.node.GetType()]; Color tint;
if (NodeEditorWindow.nodeTint.TryGetValue(port.node.GetType(), out tint)) backgroundColor *= tint;
Color col = NodeEditorWindow.current.graphEditor.GetTypeColor(port.ValueType); Color col = NodeEditorWindow.current.graphEditor.GetTypeColor(port.ValueType);
DrawPortHandle(rect, backgroundColor, col); DrawPortHandle(rect, backgroundColor, col);

View File

@ -153,7 +153,8 @@ namespace XNode {
/// <summary> Returns port which matches fieldName </summary> /// <summary> Returns port which matches fieldName </summary>
public NodePort GetPort(string fieldName) { public NodePort GetPort(string fieldName) {
if (ports.ContainsKey(fieldName)) return ports[fieldName]; NodePort port;
if (ports.TryGetValue(fieldName, out port)) return port;
else return null; else return null;
} }

View File

@ -16,9 +16,10 @@ namespace XNode {
Dictionary<string, NodePort> staticPorts = new Dictionary<string, NodePort>(); Dictionary<string, NodePort> staticPorts = new Dictionary<string, NodePort>();
System.Type nodeType = node.GetType(); System.Type nodeType = node.GetType();
if (portDataCache.ContainsKey(nodeType)) { List<NodePort> typePortCache;
for (int i = 0; i < portDataCache[nodeType].Count; i++) { if (portDataCache.TryGetValue(nodeType, out typePortCache)) {
staticPorts.Add(portDataCache[nodeType][i].fieldName, portDataCache[nodeType][i]); for (int i = 0; i < typePortCache.Count; i++) {
staticPorts.Add(typePortCache[i].fieldName, portDataCache[nodeType][i]);
} }
} }
@ -26,8 +27,8 @@ namespace XNode {
// Loop through current node ports // Loop through current node ports
foreach (NodePort port in ports.Values.ToList()) { foreach (NodePort port in ports.Values.ToList()) {
// If port still exists, check it it has been changed // If port still exists, check it it has been changed
if (staticPorts.ContainsKey(port.fieldName)) { NodePort staticPort;
NodePort staticPort = staticPorts[port.fieldName]; if (staticPorts.TryGetValue(port.fieldName, out staticPort)) {
// If port exists but with wrong settings, remove it. Re-add it later. // If port exists but with wrong settings, remove it. Re-add it later.
if (port.connectionType != staticPort.connectionType || port.IsDynamic || port.direction != staticPort.direction) ports.Remove(port.fieldName); if (port.connectionType != staticPort.connectionType || port.IsDynamic || port.direction != staticPort.direction) ports.Remove(port.fieldName);
else port.ValueType = staticPort.ValueType; else port.ValueType = staticPort.ValueType;