From 9e2c2490eb0658eb31d3b1a6358aed78e496b602 Mon Sep 17 00:00:00 2001 From: frosch Date: Sat, 6 Aug 2011 13:55:52 +0000 Subject: (svn r22724) -Codechange: Reduce number of realloc calls when loading VarAct2s. --- src/newgrf.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/newgrf.cpp b/src/newgrf.cpp index 514d6dfdf..59dc65aad 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -4004,18 +4004,16 @@ static void NewSpriteGroup(ByteReader *buf) case 2: group->size = DSG_SIZE_DWORD; varsize = 4; break; } + static SmallVector 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 @@ static void NewSpriteGroup(ByteReader *buf) /* Continue reading var adjusts while bit 5 is set. */ } while (HasBit(varadjust, 5)); + group->num_adjusts = adjusts.Length(); + group->adjusts = MallocT(group->num_adjusts); + MemCpyT(group->adjusts, adjusts.Begin(), group->num_adjusts); + group->num_ranges = buf->ReadByte(); if (group->num_ranges > 0) group->ranges = CallocT(group->num_ranges); -- cgit v1.2.3-54-g00ecf