From 5b65f3f1558a36a36198339317e9abdcaa41fc81 Mon Sep 17 00:00:00 2001 From: Harald Csaszar Date: Wed, 24 Jun 2020 11:44:10 +0200 Subject: [PATCH] [csharp] Avoiding reallocations in Animation ctor by adding all HashSet entries at once. Closes #1709. --- spine-csharp/src/Animation.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/spine-csharp/src/Animation.cs b/spine-csharp/src/Animation.cs index e0107fb84..70c1b33ee 100644 --- a/spine-csharp/src/Animation.cs +++ b/spine-csharp/src/Animation.cs @@ -43,9 +43,13 @@ namespace Spine { public Animation (string name, ExposedList 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(); - 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(propertyIDs); this.name = name; this.timelines = timelines; this.duration = duration;