Skip to content

Commit

Permalink
Removed dynamic allocations from layer functions
Browse files Browse the repository at this point in the history
smaller memory footprint and better cache usage
  • Loading branch information
Cubitect committed Sep 4, 2021
1 parent b94c12e commit cf14767
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 145 deletions.
6 changes: 3 additions & 3 deletions finders.c
Original file line number Diff line number Diff line change
Expand Up @@ -1745,7 +1745,7 @@ int isViableStructurePos(int structureType, int mc, LayerStack *g,
L_feature:
if (mc < MC_1_16)
{
l = &g->layers[L_VORONOI_ZOOM_1];
l = &g->layers[L_VORONOI_1];
biomeX = (chunkX << 4) + 9;
biomeZ = (chunkZ << 4) + 9;
}
Expand Down Expand Up @@ -1787,7 +1787,7 @@ int isViableStructurePos(int structureType, int mc, LayerStack *g,
goto L_not_viable;
if (mc < MC_1_16)
{
l = &g->layers[L_VORONOI_ZOOM_1];
l = &g->layers[L_VORONOI_1];
biomeX = (chunkX << 4) + 9;
biomeZ = (chunkZ << 4) + 9;
}
Expand Down Expand Up @@ -1852,7 +1852,7 @@ int isViableStructurePos(int structureType, int mc, LayerStack *g,
if (!isDeepOcean(ids[0]))
goto L_not_viable;
if (mc >= MC_1_13)
l = &g->layers[L13_OCEAN_MIX_4];
l = &g->layers[L_OCEAN_MIX_4];
else
l = &g->layers[L_RIVER_MIX_4];
biomeX = (chunkX << 4) + 8; // areBiomesViable expects block positions
Expand Down
44 changes: 24 additions & 20 deletions generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ void setupGeneratorLargeBiomes(LayerStack *g, int mc, int largeBiomes)

if (mc <= MC_1_12)
{
p = setupLayer(g, L_VORONOI_1, mapVoronoi114, mc, 4, 7, 10, p, 0);
p = setupLayer(g, L_VORONOI_1, mapVoronoi114, mc, 4, 3, 10, p, 0);
}
else
{
Expand All @@ -241,9 +241,9 @@ void setupGeneratorLargeBiomes(LayerStack *g, int mc, int largeBiomes)
g->layers+L_RIVER_MIX_4, g->layers+L_ZOOM_4_OCEAN);

if (mc <= MC_1_14)
p = setupLayer(g, L_VORONOI_1, mapVoronoi114, mc, 4, 7, 10, p, 0);
p = setupLayer(g, L_VORONOI_1, mapVoronoi114, mc, 4, 3, 10, p, 0);
else
p = setupLayer(g, L_VORONOI_1, mapVoronoi, mc, 4, 7, LAYER_INIT_SHA, p, 0);
p = setupLayer(g, L_VORONOI_1, mapVoronoi, mc, 4, 3, LAYER_INIT_SHA, p, 0);
}

g->entry_1 = p;
Expand Down Expand Up @@ -277,11 +277,18 @@ void setupGenerator(LayerStack *g, int mc)
/* Recursively calculates the minimum buffer size required to generate an area
* of the specified size from the current layer onwards.
*/
static void getMaxArea(const Layer *layer, int areaX, int areaZ, int *maxX, int *maxZ)
static void getMaxArea(
const Layer *layer, int areaX, int areaZ, int *maxX, int *maxZ, size_t *siz)
{
if (layer == NULL)
return;

areaX += layer->edge;
areaZ += layer->edge;

if (areaX > *maxX) *maxX = areaX;
if (areaZ > *maxZ) *maxZ = areaZ;

if (layer->zoom == 2)
{
areaX >>= 1;
Expand All @@ -293,31 +300,28 @@ static void getMaxArea(const Layer *layer, int areaX, int areaZ, int *maxX, int
areaZ >>= 2;
}

areaX += layer->edge;
areaZ += layer->edge;

if (areaX > *maxX) *maxX = areaX;
if (areaZ > *maxZ) *maxZ = areaZ;
// multi-layers and zoom-layers use a temporary copy of their parent area
if (layer->p2 || layer->zoom != 1)
*siz += areaX * areaZ;

getMaxArea(layer->p, areaX, areaZ, maxX, maxZ);
getMaxArea(layer->p2, areaX, areaZ, maxX, maxZ);
getMaxArea(layer->p, areaX, areaZ, maxX, maxZ, siz);
if (layer->p2)
getMaxArea(layer->p2, areaX, areaZ, maxX, maxZ, siz);
}

int calcRequiredBuf(const Layer *layer, int areaX, int areaZ)
size_t calcRequiredBuf(const Layer *layer, int areaX, int areaZ)
{
int maxX = areaX, maxZ = areaZ;
getMaxArea(layer, areaX, areaZ, &maxX, &maxZ);

return maxX * maxZ;
size_t bufsiz = 0;
getMaxArea(layer, areaX, areaZ, &maxX, &maxZ, &bufsiz);
return bufsiz + maxX * (size_t)maxZ;
}

int *allocCache(const Layer *layer, int sizeX, int sizeZ)
{
int size = calcRequiredBuf(layer, sizeX, sizeZ);

int *ret = (int *) malloc(sizeof(*ret)*size);
memset(ret, 0, sizeof(*ret)*size);

size_t bytes = calcRequiredBuf(layer, sizeX, sizeZ) * sizeof(int);
int *ret = (int *) malloc(bytes);
memset(ret, 0, bytes);
return ret;
}

Expand Down
2 changes: 1 addition & 1 deletion generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void setupGeneratorLargeBiomes(LayerStack *g, int mc, int largeBiomes);
/* Calculates the minimum size of the buffers required to generate an area of
* dimensions 'sizeX' by 'sizeZ' at the specified layer.
*/
int calcRequiredBuf(const Layer *layer, int areaX, int areaZ);
size_t calcRequiredBuf(const Layer *layer, int areaX, int areaZ);

/* Allocates an amount of memory required to generate an area of dimensions
* 'sizeX' by 'sizeZ' for the magnification of the given layer.
Expand Down
Loading

0 comments on commit cf14767

Please sign in to comment.