From 3a898a0996031eaae795f32ae2bf2272e480f28c Mon Sep 17 00:00:00 2001
From: "harald.martini@student.tugraz.at" <harald.martini@student.tugraz.at>
Date: Mon, 2 Aug 2021 17:39:38 +0200
Subject: [PATCH] Cache attributes of ModalResultField in Dictionary

(cherry picked from commit 1197335fcf569aa9937c8f1360caa7b4da84b8c9)
---
 .../Models/Simulation/Data/ModalResultField.cs    | 15 +++++++++++++--
 1 file changed, 13 insertions(+), 2 deletions(-)

diff --git a/VectoCore/VectoCore/Models/Simulation/Data/ModalResultField.cs b/VectoCore/VectoCore/Models/Simulation/Data/ModalResultField.cs
index 5d70de6d7b..1de50e5597 100644
--- a/VectoCore/VectoCore/Models/Simulation/Data/ModalResultField.cs
+++ b/VectoCore/VectoCore/Models/Simulation/Data/ModalResultField.cs
@@ -30,6 +30,7 @@
 */
 
 using System;
+using System.Collections.Concurrent;
 using System.Reflection;
 using System.Text.RegularExpressions;
 using TUGraz.VectoCommon.Utils;
@@ -473,6 +474,8 @@ namespace TUGraz.VectoCore.Models.Simulation.Data
 
 	public static class ModalResultFieldExtensionMethods
 	{
+		private static ConcurrentDictionary<ModalResultField, ModalResultFieldAttribute> _attributeDictionary =
+			new ConcurrentDictionary<ModalResultField, ModalResultFieldAttribute>();
 		public static string GetName(this ModalResultField field)
 		{
 			return GetAttribute(field).Name ?? field.ToString();
@@ -480,7 +483,8 @@ namespace TUGraz.VectoCore.Models.Simulation.Data
 
 		public static string GetCaption(this ModalResultField field)
 		{
-			return GetAttribute(field).Caption ?? GetAttribute(field).Name ?? field.ToString();
+			var attribute = GetAttribute(field);
+			return attribute.Caption ?? attribute.Name ?? field.ToString();
 		}
 
 		public static string GetShortCaption(this ModalResultField field)
@@ -496,7 +500,14 @@ namespace TUGraz.VectoCore.Models.Simulation.Data
 
 		public static ModalResultFieldAttribute GetAttribute(this ModalResultField field)
 		{
-			return (ModalResultFieldAttribute)Attribute.GetCustomAttribute(ForValue(field), typeof(ModalResultFieldAttribute));
+			var attributeCached = _attributeDictionary.TryGetValue(field, out var attribute);
+			if (attributeCached) {
+				return attribute;
+			} else {
+				attribute = (ModalResultFieldAttribute)Attribute.GetCustomAttribute(ForValue(field), typeof(ModalResultFieldAttribute));
+				_attributeDictionary.TryAdd(field, attribute);
+			}
+			return attribute;
 		}
 
 		private static MemberInfo ForValue(ModalResultField field)
-- 
GitLab