Skip to content

Instantly share code, notes, and snippets.

@Vercidium
Created December 7, 2019 01:08
Show Gist options
  • Save Vercidium/53365fc0bec6bd480b1a1a0f8f86f673 to your computer and use it in GitHub Desktop.
Save Vercidium/53365fc0bec6bd480b1a1a0f8f86f673 to your computer and use it in GitHub Desktop.
/* Changes made:
* Using chunkXCounter and chunkZCounter to check when we have moved into a new chunk,
* rather than (x % Constants.ChunkSize) * Constants.ChunkSize each loop. This removes all %, / and * operators
* Comparisons against 0 in loops where possible
* Using .Reset() on the block, rather than getting a reference and modifying index and health on two different lines
* Previous benchmark: 100 runs in 5315ms.
* New benchmark: 100 runs in 4755ms
* */
byte[] fileData = r.ReadBytes(bytesRemaining);
var b = 0;
int chunkX = 0;
int chunkXCounter = 0;
// We have to subtract here as the first run of the loop will increment xMod
int xMod = -Constants.ChunkSize;
for (int x = 0; x < Constants.MapSizeX; x++)
{
// Keep a counter to check when we have moved into a new chunk
if (chunkXCounter == 32)
{
chunkX++;
// If moved into a new chunk, reset the access
xMod = 0;
chunkXCounter = 1;
}
else
{
// If in the same chunk, increment the access
xMod += Constants.ChunkSize;
chunkXCounter++;
}
int xzAccess = xMod - Constants.ChunkSizeSquared;
int chunkZ = 0;
int chunkZCounter = 0;
for (int z = 0; z < Constants.MapSizeZ; z++)
{
// Keep a counter to check when we have moved into a new chunk
if (chunkZCounter == 32)
{
chunkZ++;
// If moved into a new chunk, reset the access
xzAccess = xMod;
chunkZCounter = 1;
}
else
{
// If in the same chunk, increment the access
xzAccess += Constants.ChunkSizeSquared;
chunkZCounter++;
}
// Map-Coordinate Y, tracks our vertical position in the column
int mapY = 0;
// Get the amount of block types in this column
byte columnCount = fileData[b++];
// Comparison against 0 is faster
for (int i = columnCount; i > 0; i--)
{
// Get the block type and how tall the column is
byte type = fileData[b++];
byte amount = fileData[b++];
// If it's an empty column, there's no need to modify the chunk data.
if (type == 0)
{
mapY += amount;
continue;
}
int chunkY = mapY / Constants.ChunkSize;
// Get the chunk and initialise it if needed
ChunkBase c = m.chunks[chunkX, chunkY, chunkZ];
if (c == null)
c = m.InitChunk(chunkX, chunkY, chunkZ);
// Chunk-Coordinate Y
int yAccess = mapY % Constants.ChunkSize;
// Add each block in the column section
// Comparison againt 0 is faster
for (int j = amount; j > 0; j--)
{
// If we have moved up into a new chunk
if (yAccess == 32)
{
chunkY++;
c = m.chunks[chunkX, chunkY, chunkZ];
if (c == null)
c = m.InitChunk(chunkX, chunkY, chunkZ);
yAccess = 0;
}
// Get a reference to the block and update it
c.data[xzAccess + yAccess].Reset(type, 100);
yAccess++;
}
mapY += amount;
}
// Update the height map
m.maxY[x, z] = (byte)mapY;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment