From 0df69e04fdb6674fcc7b4c1c9434021d241799d9 Mon Sep 17 00:00:00 2001 From: ZimM Date: Sat, 28 Feb 2015 03:12:27 +0200 Subject: [PATCH] removed string comparisons and reduced noticeable method call overhead in Skin --- spine-csharp/src/Skin.cs | 46 +++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/spine-csharp/src/Skin.cs b/spine-csharp/src/Skin.cs index 7b4c57b15..e7f8d8c06 100644 --- a/spine-csharp/src/Skin.cs +++ b/spine-csharp/src/Skin.cs @@ -35,8 +35,8 @@ namespace Spine { /// Stores attachments by slot index and attachment name. public class Skin { internal String name; - private Dictionary, Attachment> attachments = - new Dictionary, Attachment>(AttachmentComparer.Instance); + private Dictionary attachments = + new Dictionary(AttachmentKeyTupleComparer.Instance); public String Name { get { return name; } } @@ -47,26 +47,26 @@ namespace Spine { public void AddAttachment (int slotIndex, String name, Attachment attachment) { if (attachment == null) throw new ArgumentNullException("attachment cannot be null."); - attachments[new KeyValuePair(slotIndex, name)] = attachment; + attachments[new AttachmentKeyTuple(slotIndex, name)] = attachment; } /// May be null. public Attachment GetAttachment (int slotIndex, String name) { Attachment attachment; - attachments.TryGetValue(new KeyValuePair(slotIndex, name), out attachment); + attachments.TryGetValue(new AttachmentKeyTuple(slotIndex, name), out attachment); return attachment; } public void FindNamesForSlot (int slotIndex, List names) { if (names == null) throw new ArgumentNullException("names cannot be null."); - foreach (KeyValuePair key in attachments.Keys) - if (key.Key == slotIndex) names.Add(key.Value); + foreach (AttachmentKeyTuple key in attachments.Keys) + if (key.SlotIndex == slotIndex) names.Add(key.Name); } public void FindAttachmentsForSlot (int slotIndex, List attachments) { if (attachments == null) throw new ArgumentNullException("attachments cannot be null."); - foreach (KeyValuePair, Attachment> entry in this.attachments) - if (entry.Key.Key == slotIndex) attachments.Add(entry.Value); + foreach (KeyValuePair entry in this.attachments) + if (entry.Key.SlotIndex == slotIndex) attachments.Add(entry.Value); } override public String ToString () { @@ -75,27 +75,39 @@ namespace Spine { /// Attach all attachments from this skin if the corresponding attachment from the old skin is currently attached. internal void AttachAll (Skeleton skeleton, Skin oldSkin) { - foreach (KeyValuePair, Attachment> entry in oldSkin.attachments) { - int slotIndex = entry.Key.Key; + foreach (KeyValuePair entry in oldSkin.attachments) { + int slotIndex = entry.Key.SlotIndex; Slot slot = skeleton.slots.Items[slotIndex]; if (slot.attachment == entry.Value) { - Attachment attachment = GetAttachment(slotIndex, entry.Key.Value); + Attachment attachment = GetAttachment(slotIndex, entry.Key.Name); if (attachment != null) slot.Attachment = attachment; } } } // Avoids boxing in the dictionary. - private class AttachmentComparer : IEqualityComparer> { - internal static readonly AttachmentComparer Instance = new AttachmentComparer(); + private class AttachmentKeyTupleComparer : IEqualityComparer { + internal static readonly AttachmentKeyTupleComparer Instance = new AttachmentKeyTupleComparer(); - bool IEqualityComparer>.Equals (KeyValuePair o1, KeyValuePair o2) { - return o1.Key == o2.Key && o1.Value == o2.Value; + bool IEqualityComparer.Equals (AttachmentKeyTuple o1, AttachmentKeyTuple o2) { + return o1.SlotIndex == o2.SlotIndex && o1.NameHashCode == o2.NameHashCode; } - int IEqualityComparer>.GetHashCode (KeyValuePair o) { - return o.Key; + int IEqualityComparer.GetHashCode (AttachmentKeyTuple o) { + return o.SlotIndex; } } + + private class AttachmentKeyTuple { + public readonly int SlotIndex; + public readonly string Name; + public readonly int NameHashCode; + + public AttachmentKeyTuple(int slotIndex, string name) { + SlotIndex = slotIndex; + Name = name; + NameHashCode = Name.GetHashCode(); + } + } } }