[csharp] Avoiding reallocations in Animation ctor by adding all HashSet entries at once. Closes #1709.

This commit is contained in:
Harald Csaszar 2020-06-24 11:44:10 +02:00
parent 46a0ffce50
commit 5b65f3f155

View File

@ -43,9 +43,13 @@ namespace Spine {
public Animation (string name, ExposedList<Timeline> timelines, float duration) {
if (name == null) throw new ArgumentNullException("name", "name cannot be null.");
if (timelines == null) throw new ArgumentNullException("timelines", "timelines cannot be null.");
this.timelineIds = new HashSet<int>();
foreach (Timeline timeline in timelines)
timelineIds.Add(timeline.PropertyId);
// Note: avoiding reallocations by adding all hash set entries at
// once (EnsureCapacity() is only available in newer .Net versions).
int[] propertyIDs = new int[timelines.Count];
for (int i = 0; i < timelines.Count; ++i) {
propertyIDs[i] = timelines.Items[i].PropertyId;
}
this.timelineIds = new HashSet<int>(propertyIDs);
this.name = name;
this.timelines = timelines;
this.duration = duration;