removed string comparisons and reduced noticeable method call overhead in Skin

This commit is contained in:
ZimM 2015-02-28 03:12:27 +02:00
parent 08695c07a3
commit 0df69e04fd

View File

@ -35,8 +35,8 @@ namespace Spine {
/// <summary>Stores attachments by slot index and attachment name.</summary> /// <summary>Stores attachments by slot index and attachment name.</summary>
public class Skin { public class Skin {
internal String name; internal String name;
private Dictionary<KeyValuePair<int, String>, Attachment> attachments = private Dictionary<AttachmentKeyTuple, Attachment> attachments =
new Dictionary<KeyValuePair<int, String>, Attachment>(AttachmentComparer.Instance); new Dictionary<AttachmentKeyTuple, Attachment>(AttachmentKeyTupleComparer.Instance);
public String Name { get { return name; } } public String Name { get { return name; } }
@ -47,26 +47,26 @@ namespace Spine {
public void AddAttachment (int slotIndex, String name, Attachment attachment) { public void AddAttachment (int slotIndex, String name, Attachment attachment) {
if (attachment == null) throw new ArgumentNullException("attachment cannot be null."); if (attachment == null) throw new ArgumentNullException("attachment cannot be null.");
attachments[new KeyValuePair<int, String>(slotIndex, name)] = attachment; attachments[new AttachmentKeyTuple(slotIndex, name)] = attachment;
} }
/// <returns>May be null.</returns> /// <returns>May be null.</returns>
public Attachment GetAttachment (int slotIndex, String name) { public Attachment GetAttachment (int slotIndex, String name) {
Attachment attachment; Attachment attachment;
attachments.TryGetValue(new KeyValuePair<int, String>(slotIndex, name), out attachment); attachments.TryGetValue(new AttachmentKeyTuple(slotIndex, name), out attachment);
return attachment; return attachment;
} }
public void FindNamesForSlot (int slotIndex, List<String> names) { public void FindNamesForSlot (int slotIndex, List<String> names) {
if (names == null) throw new ArgumentNullException("names cannot be null."); if (names == null) throw new ArgumentNullException("names cannot be null.");
foreach (KeyValuePair<int, String> key in attachments.Keys) foreach (AttachmentKeyTuple key in attachments.Keys)
if (key.Key == slotIndex) names.Add(key.Value); if (key.SlotIndex == slotIndex) names.Add(key.Name);
} }
public void FindAttachmentsForSlot (int slotIndex, List<Attachment> attachments) { public void FindAttachmentsForSlot (int slotIndex, List<Attachment> attachments) {
if (attachments == null) throw new ArgumentNullException("attachments cannot be null."); if (attachments == null) throw new ArgumentNullException("attachments cannot be null.");
foreach (KeyValuePair<KeyValuePair<int, String>, Attachment> entry in this.attachments) foreach (KeyValuePair<AttachmentKeyTuple, Attachment> entry in this.attachments)
if (entry.Key.Key == slotIndex) attachments.Add(entry.Value); if (entry.Key.SlotIndex == slotIndex) attachments.Add(entry.Value);
} }
override public String ToString () { override public String ToString () {
@ -75,27 +75,39 @@ namespace Spine {
/// <summary>Attach all attachments from this skin if the corresponding attachment from the old skin is currently attached.</summary> /// <summary>Attach all attachments from this skin if the corresponding attachment from the old skin is currently attached.</summary>
internal void AttachAll (Skeleton skeleton, Skin oldSkin) { internal void AttachAll (Skeleton skeleton, Skin oldSkin) {
foreach (KeyValuePair<KeyValuePair<int, String>, Attachment> entry in oldSkin.attachments) { foreach (KeyValuePair<AttachmentKeyTuple, Attachment> entry in oldSkin.attachments) {
int slotIndex = entry.Key.Key; int slotIndex = entry.Key.SlotIndex;
Slot slot = skeleton.slots.Items[slotIndex]; Slot slot = skeleton.slots.Items[slotIndex];
if (slot.attachment == entry.Value) { 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; if (attachment != null) slot.Attachment = attachment;
} }
} }
} }
// Avoids boxing in the dictionary. // Avoids boxing in the dictionary.
private class AttachmentComparer : IEqualityComparer<KeyValuePair<int, String>> { private class AttachmentKeyTupleComparer : IEqualityComparer<AttachmentKeyTuple> {
internal static readonly AttachmentComparer Instance = new AttachmentComparer(); internal static readonly AttachmentKeyTupleComparer Instance = new AttachmentKeyTupleComparer();
bool IEqualityComparer<KeyValuePair<int, string>>.Equals (KeyValuePair<int, string> o1, KeyValuePair<int, string> o2) { bool IEqualityComparer<AttachmentKeyTuple>.Equals (AttachmentKeyTuple o1, AttachmentKeyTuple o2) {
return o1.Key == o2.Key && o1.Value == o2.Value; return o1.SlotIndex == o2.SlotIndex && o1.NameHashCode == o2.NameHashCode;
} }
int IEqualityComparer<KeyValuePair<int, string>>.GetHashCode (KeyValuePair<int, string> o) { int IEqualityComparer<AttachmentKeyTuple>.GetHashCode (AttachmentKeyTuple o) {
return o.Key; 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();
}
}
} }
} }