Skip to content

Commit d717ec8

Browse files
authored
perf(light): resolve the opposite face once in the chunk propagator (#32)
Same change as in LightPropagator, on the loop that walks a whole chunk. Measurement follows in the pull request.
1 parent 2349cfa commit d717ec8

1 file changed

Lines changed: 27 additions & 2 deletions

File tree

‎falco-light/src/main/java/net/onelitefeather/falco/light/ChunkLightPropagator.java‎

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,30 @@ public final class ChunkLightPropagator {
3636
private static final BlockFace[] FACES = BlockFace.values();
3737
private static final int MASK = LightNibbles.DIMENSION - 1;
3838

39+
/**
40+
* The opposite of every face, in the order of {@link #FACES}.
41+
* <p>
42+
* {@code BlockFace#opposite()} is a switch over the enum, and the inner loop calls it once per
43+
* face per queued position — six times for every block the light reaches. Resolving it once at
44+
* class load turns that into an array read.
45+
* </p>
46+
*/
47+
private static final BlockFace[] OPPOSITES = opposites();
48+
49+
/**
50+
* Resolves the opposite of every face once.
51+
*
52+
* @return the opposite of every face, indexed like {@link #FACES}
53+
*/
54+
private static BlockFace[] opposites() {
55+
BlockFace[] opposites = new BlockFace[FACES.length];
56+
57+
for (int index = 0; index < FACES.length; index++) {
58+
opposites[index] = FACES[index].opposite();
59+
}
60+
return opposites;
61+
}
62+
3963
private byte[] levels;
4064
private int[] queue;
4165

@@ -120,15 +144,16 @@ private List<LightNibbles> search(List<SectionOpacity> sections, int height, int
120144
int y = index >> 8;
121145
int next = level - 1;
122146

123-
for (BlockFace face : FACES) {
147+
for (int faceIndex = 0; faceIndex < FACES.length; faceIndex++) {
148+
BlockFace face = FACES[faceIndex];
124149
int neighbourX = x + face.offsetX();
125150
int neighbourY = y + face.offsetY();
126151
int neighbourZ = z + face.offsetZ();
127152

128153
if (isOutside(neighbourX, neighbourY, neighbourZ, height)) {
129154
continue;
130155
}
131-
if (blocksFace(sections, neighbourX, neighbourY, neighbourZ, face.opposite())) {
156+
if (blocksFace(sections, neighbourX, neighbourY, neighbourZ, OPPOSITES[faceIndex])) {
132157
continue;
133158
}
134159

0 commit comments

Comments
 (0)