TileAtlas: Slightly optimize out some allocation

User vector instead of list for colums.s
This commit is contained in:
Jonas Kulla 2014-01-16 19:17:46 +01:00
parent b0a41a23e0
commit f39d1239bb
1 changed files with 24 additions and 20 deletions

View File

@ -21,8 +21,6 @@
#include "tileatlas.h" #include "tileatlas.h"
#include <list>
namespace TileAtlas namespace TileAtlas
{ {
@ -37,8 +35,7 @@ struct Column
{} {}
}; };
// FIXME: this can be optimized to a vector typedef std::vector<Column> ColumnVec;
typedef std::list<Column> ColumnList;
/* Autotile area width */ /* Autotile area width */
static const int atAreaW = 96*4; static const int atAreaW = 96*4;
@ -79,9 +76,10 @@ Vec2i minSize(int tilesetH, int maxAtlasSize)
return Vec2i(-1, -1); return Vec2i(-1, -1);
} }
static ColumnList calcSrcCols(int tilesetH) static ColumnVec calcSrcCols(int tilesetH)
{ {
ColumnList cols; ColumnVec cols;
cols.reserve(2);
cols.push_back(Column(0, 0, tilesetH)); cols.push_back(Column(0, 0, tilesetH));
cols.push_back(Column(tsLaneW, 0, tilesetH)); cols.push_back(Column(tsLaneW, 0, tilesetH));
@ -89,9 +87,10 @@ static ColumnList calcSrcCols(int tilesetH)
return cols; return cols;
} }
static ColumnList calcDstCols(int atlasW, int atlasH) static ColumnVec calcDstCols(int atlasW, int atlasH)
{ {
ColumnList cols; ColumnVec cols;
cols.reserve(3);
/* Columns below the autotile area */ /* Columns below the autotile area */
const int underAt = atlasH - atAreaH; const int underAt = atlasH - atAreaH;
@ -110,19 +109,21 @@ static ColumnList calcDstCols(int atlasW, int atlasH)
return cols; return cols;
} }
static BlitList calcBlitsInt(ColumnList &srcCols, ColumnList &dstCols) static BlitVec calcBlitsInt(ColumnVec &srcCols, ColumnVec &dstCols)
{ {
BlitList blits; BlitVec blits;
while (!srcCols.empty()) /* Using signed indices here is safer, as we
* might decrement dstI while it is zero. */
int dstI = 0;
for (size_t srcI = 0; srcI < srcCols.size(); ++srcI)
{ {
Column srcCol = srcCols.front(); Column &srcCol = srcCols[srcI];
srcCols.pop_front();
while (!dstCols.empty() && srcCol.h > 0) for (; dstI < (int) dstCols.size() && srcCol.h > 0; ++dstI)
{ {
Column dstCol = dstCols.front(); Column &dstCol = dstCols[dstI];
dstCols.pop_front();
if (srcCol.h > dstCol.h) if (srcCol.h > dstCol.h)
{ {
@ -141,7 +142,10 @@ static BlitList calcBlitsInt(ColumnList &srcCols, ColumnList &dstCols)
dstCol.y += srcCol.h; dstCol.y += srcCol.h;
dstCol.h -= srcCol.h; dstCol.h -= srcCol.h;
dstCols.push_front(dstCol);
/* Queue this column up again for processing */
--dstI;
srcCol.h = 0; srcCol.h = 0;
} }
else else
@ -156,10 +160,10 @@ static BlitList calcBlitsInt(ColumnList &srcCols, ColumnList &dstCols)
return blits; return blits;
} }
BlitList calcBlits(int tilesetH, const Vec2i &atlasSize) BlitVec calcBlits(int tilesetH, const Vec2i &atlasSize)
{ {
ColumnList srcCols = calcSrcCols(tilesetH); ColumnVec srcCols = calcSrcCols(tilesetH);
ColumnList dstCols = calcDstCols(atlasSize.x, atlasSize.y); ColumnVec dstCols = calcDstCols(atlasSize.x, atlasSize.y);
return calcBlitsInt(srcCols, dstCols); return calcBlitsInt(srcCols, dstCols);
} }