Skip to content

Commit dfd2c1f

Browse files
committed
Optimise chunk array access order
Should help with cpu cache hits Cache some index calculations
1 parent 016cb3b commit dfd2c1f

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

Diff for: src/org/jmc/Chunk.java

+14-11
Original file line numberDiff line numberDiff line change
@@ -307,19 +307,20 @@ public Blocks getBlocks()
307307
if(tagBiomes!=null && tagBiomes.data.length > 0) {
308308
int ymin = getYMin();
309309
int ymax = getYMax();
310-
for(int x = 0; x < 16; x++) {
310+
for(int y = 0; y < ymax - ymin; y++) {
311+
int sectionIdx = sectionsBlocks.getSectionIndex(y);
312+
SectionBlocks section = sectionsBlocks.sections.get(sectionIdx);
313+
if (section == null) continue;
314+
int yInSection = y - sectionIdx * 16;
311315
for (int z = 0; z < 16; z++) {
312-
for (int y = 0; y < ymax - ymin; y++) {
316+
for (int x = 0; x < 16; x++) {
313317
int biome;
314318
if (chunkVer >= 2203) {// >= 19w36a
315319
biome = tagBiomes.data[x/4 + (z/4)*4 + (y/4)*4*4];
316320
} else {
317321
biome = tagBiomes.data[x+z*16];
318322
}
319-
int sectionIdx = sectionsBlocks.getSectionIndex(y);
320-
SectionBlocks section = sectionsBlocks.sections.get(sectionIdx);
321-
if (section == null) continue;
322-
section.biomes[section.getIndex(x, y - sectionIdx * 16, z)] = IDConvert.convertBiome(biome);
323+
section.biomes[section.getIndex(x, yInSection, z)] = IDConvert.convertBiome(biome);
323324
}
324325
}
325326
}
@@ -567,12 +568,14 @@ boolean fillBiomes(TAG_Compound section) {
567568

568569
String biomeName = ((TAG_String) tagBiomePalette.elements[(int) biomePid]).value;
569570
NamespaceID biome = NamespaceID.fromString(biomeName);
571+
int baseInd = ((i%4)*4) + ((i/4)*16*4)%(16*16) + ((i/(4*4))*16*16*4);
570572
//Copy biome into 4x4x4 cube
571-
for (int x = 0; x < 4; x++) {
572-
for (int y = 0; y < 4; y++) {
573-
for (int z = 0; z< 4; z++) {
574-
int baseInd = ((i%4)*4) + ((i/4)*16*4)%(16*16) + ((i/(4*4))*16*16*4);
575-
int index = baseInd + (x + z*16 + y*16*16);
573+
for (int y = 0; y < 4; y++) {
574+
int yOffset = y * 16 * 16;
575+
for (int z = 0; z < 4; z++) {
576+
int zOffset = z * 16;
577+
for (int x = 0; x < 4; x++) {
578+
int index = baseInd + x + zOffset + yOffset;
576579
biomes[index] = biome;
577580
}
578581
}

0 commit comments

Comments
 (0)