소스 검색

Avoid boxing in Skin dictionary.

closes #156
NathanSweet 12 년 전
부모
커밋
e5aca584a2
1개의 변경된 파일15개의 추가작업 그리고 1개의 파일을 삭제
  1. 15 1
      spine-csharp/src/Skin.cs

+ 15 - 1
spine-csharp/src/Skin.cs

@@ -38,9 +38,10 @@ namespace Spine {
 	/// <summary>Stores attachments by slot index and attachment name.</summary>
 	public class Skin {
 		internal String name;
+		private Dictionary<KeyValuePair<int, String>, Attachment> attachments =
+			new Dictionary<KeyValuePair<int, String>, Attachment>(AttachmentComparer.Instance);
 
 		public String Name { get { return name; } }
-		private Dictionary<KeyValuePair<int, String>, Attachment> attachments = new Dictionary<KeyValuePair<int, String>, Attachment>();
 
 		public Skin (String name) {
 			if (name == null) throw new ArgumentNullException("name cannot be null.");
@@ -86,5 +87,18 @@ namespace Spine {
 				}
 			}
 		}
+
+		// Avoids boxing in the dictionary.
+		private class AttachmentComparer : IEqualityComparer<KeyValuePair<int, String>> {
+			internal static readonly AttachmentComparer Instance = new AttachmentComparer();
+
+			bool IEqualityComparer<KeyValuePair<int, string>>.Equals (KeyValuePair<int, string> o1, KeyValuePair<int, string> o2) {
+				return o1.Key == o2.Key && o1.Value == o2.Value;
+			}
+
+			int IEqualityComparer<KeyValuePair<int, string>>.GetHashCode (KeyValuePair<int, string> o) {
+				return o.Key;
+			}
+		}
 	}
 }