Browse Source

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

Harald Csaszar 5 years ago
parent
commit
5b65f3f155
1 changed files with 7 additions and 3 deletions
  1. 7 3
      spine-csharp/src/Animation.cs

+ 7 - 3
spine-csharp/src/Animation.cs

@@ -43,9 +43,13 @@ namespace Spine {
 		public Animation (string name, ExposedList<Timeline> timelines, float duration) {
 		public Animation (string name, ExposedList<Timeline> timelines, float duration) {
 			if (name == null) throw new ArgumentNullException("name", "name cannot be null.");
 			if (name == null) throw new ArgumentNullException("name", "name cannot be null.");
 			if (timelines == null) throw new ArgumentNullException("timelines", "timelines 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.name = name;
 			this.timelines = timelines;
 			this.timelines = timelines;
 			this.duration = duration;
 			this.duration = duration;