changeset 17919:0e5500ee26c0 draft

(svn r22724) -Codechange: Reduce number of realloc calls when loading VarAct2s.
author frosch <frosch@openttd.org>
date Sat, 06 Aug 2011 13:55:52 +0000
parents 05fc8b355294
children e6f389ce78a7
files src/newgrf.cpp
diffstat 1 files changed, 9 insertions(+), 7 deletions(-) [+]
line wrap: on
line diff
--- a/src/newgrf.cpp
+++ b/src/newgrf.cpp
@@ -4004,18 +4004,16 @@
 				case 2: group->size = DSG_SIZE_DWORD; varsize = 4; break;
 			}
 
+			static SmallVector<DeterministicSpriteGroupAdjust, 16> adjusts;
+			adjusts.Clear();
+
 			/* Loop through the var adjusts. Unfortunately we don't know how many we have
 			 * from the outset, so we shall have to keep reallocing. */
 			do {
-				DeterministicSpriteGroupAdjust *adjust;
-
-				group->num_adjusts++;
-				group->adjusts = ReallocT(group->adjusts, group->num_adjusts);
-
-				adjust = &group->adjusts[group->num_adjusts - 1];
+				DeterministicSpriteGroupAdjust *adjust = adjusts.Append();
 
 				/* The first var adjust doesn't have an operation specified, so we set it to add. */
-				adjust->operation = group->num_adjusts == 1 ? DSGA_OP_ADD : (DeterministicSpriteGroupAdjustOperation)buf->ReadByte();
+				adjust->operation = adjusts.Length() == 1 ? DSGA_OP_ADD : (DeterministicSpriteGroupAdjustOperation)buf->ReadByte();
 				adjust->variable  = buf->ReadByte();
 				if (adjust->variable == 0x7E) {
 					/* Link subroutine group */
@@ -4040,6 +4038,10 @@
 				/* Continue reading var adjusts while bit 5 is set. */
 			} while (HasBit(varadjust, 5));
 
+			group->num_adjusts = adjusts.Length();
+			group->adjusts = MallocT<DeterministicSpriteGroupAdjust>(group->num_adjusts);
+			MemCpyT(group->adjusts, adjusts.Begin(), group->num_adjusts);
+
 			group->num_ranges = buf->ReadByte();
 			if (group->num_ranges > 0) group->ranges = CallocT<DeterministicSpriteGroupRange>(group->num_ranges);